0% found this document useful (0 votes)
10 views3 pages

Simple Music

The document contains a simple Android music player application implemented in Java. It features a MainActivity that allows users to play and pause songs from a predefined list using a button. The XML layout defines a button for controlling playback, and the manifest file includes necessary permissions and application details.

Uploaded by

komchilinn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Simple Music

The document contains a simple Android music player application implemented in Java. It features a MainActivity that allows users to play and pause songs from a predefined list using a button. The XML layout defines a button for controlling playback, and the manifest file includes necessary permissions and application details.

Uploaded by

komchilinn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

java

package com.kom.myapp;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;

public class MainActivity extends Activity {


private MediaPlayer mediaPlayer;
private int[] songs = {R.raw.song1, R.raw.song2, R.raw.song3};
private int currentSongIndex = 0;
private Button playPauseButton;
private boolean isPlaying = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

playPauseButton = findViewById(R.id.playPauseButton);

playPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlaying) {
pauseSong();
} else {
playSong();
}
}
});
}

private void playSong() {


if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this, songs[currentSongIndex]);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
currentSongIndex = (currentSongIndex + 1) % songs.length;
playSong();
}
});
}
mediaPlayer.start();
isPlaying = true;
playPauseButton.setText("Pause");
}

private void pauseSong() {


if (mediaPlayer != null) {
mediaPlayer.pause();
isPlaying = false;
playPauseButton.setText("Play");
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
}
}
}

xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/playPauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" />
</LinearLayout>

manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kom.myapp">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Simple Music Player">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

You might also like