Demo:
1) MainActivity.java:
package com.venky.musicplayerdemo;
import
android.app.Activity;
import
android.content.Context;
import
android.media.AudioManager;
import
android.media.MediaPlayer;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public class MainActivity extends Activity implements OnClickListener
{
private MediaPlayer mediaPlayer;
Button btnStop, btnPlay, btnPause;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create media player
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.one);
// call button references
btnStop = (Button) findViewById(R.id.buttonStop);
btnPlay = (Button) findViewById(R.id.buttonPlay);
btnPause = (Button) findViewById(R.id.buttonPause);
// set listeners for buttons
btnStop.setOnClickListener(this);
btnPlay.setOnClickListener(this);
btnPause.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
// TODO
Auto-generated method stub
switch (v.getId()) {
case R.id.buttonStop:
// to stop media player
mediaPlayer.stop();
// to release media player
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
// create media player
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.one);
// enable and disable media
palyer buttons
btnStop.setEnabled(false);
btnPlay.setEnabled(true);
btnPause.setEnabled(true);
break;
case R.id.buttonPlay:
// to play media player
mediaPlayer.start();
// enable and disable media
palyer buttons
btnStop.setEnabled(true);
btnPlay.setEnabled(false);
btnPause.setEnabled(true);
break;
case R.id.buttonPause:
// pause media player
mediaPlayer.pause();
// enable and disable media
palyer buttons
btnStop.setEnabled(true);
btnPlay.setEnabled(true);
btnPause.setEnabled(false);
break;
}
}
}
2) activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5F04B4"
android:orientation="vertical"
tools:context=".MainActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
>
<Button
android:id="@+id/buttonStop"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Stop"
/>
<Button
android:id="@+id/buttonPlay"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play"
/>
<Button
android:id="@+id/buttonPause"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pause"
/>
</LinearLayout>
</LinearLayout>
3) How to Add Song :
Create a New folder name is “raw” in res folder.
And simply copy and paste any of song into "raw" folder in mp3 format and
rename song name is “one”.
Then run the Project.
4)Download this code Click Here
No comments:
Post a Comment