SoFunction
Updated on 2025-04-07

Simple implementation of Android local music player

Music playback requires calling service, here, it is just a simple sorting out the playback process.

public class PlayMusicService extends Service {

 //Binding service Calling service method. @Override
 public IBinder onBind(Intent intent) {
  return null;
 }

}
<LinearLayout xmlns:andro
 xmlns:tools="/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity" >

 <EditText
  android:
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:hint="Please enter the path to the file to be played" />

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >

  <Button
   android:
   android:onClick="play"
   android:layout_width="0dip"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="Play" />
  <Button
    android:
   android:onClick="pause"
   android:layout_width="0dip"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="pause" />
  <Button
    android:
   android:onClick="stop"
   android:layout_width="0dip"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="stop" />
  <Button
    android:
   android:onClick="replay"
   android:layout_width="0dip"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="Replay" />
 </LinearLayout>

</LinearLayout>

public class MainActivity extends Activity {
 private EditText et_path;

 private MediaPlayer mediaPlayer;

 private Button bt_play,bt_pause,bt_stop,bt_replay;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_main);
 et_path = (EditText) findViewById(.et_path);
 bt_play = (Button) findViewById(.bt_play);
 bt_pause = (Button) findViewById(.bt_pause);
 bt_stop = (Button) findViewById(.bt_stop);
 bt_replay = (Button) findViewById(.bt_replay);
 }
 /**
  * Play
  * @param view
  */
 public void play(View view) {
 String filepath = et_path.getText().toString().trim();
 File file = new File(filepath);
 if(()){
  try {
  mediaPlayer = new MediaPlayer();
  (filepath);//Set the playback data source.  (AudioManager.STREAM_MUSIC);
  ();//Prepare to start playing The playback logic is that the c code is executed in a new thread.  ();
  bt_play.setEnabled(false);
  (new OnCompletionListener() {
   @Override
   public void onCompletion(MediaPlayer mp) {
   bt_play.setEnabled(true);
   }
  });
  } catch (Exception e) {
  ();
  (this, "Play failed", 0).show();
  }
 }else{
  (this, "The file does not exist, please check the path of the file", 0).show();
 }
 }
 /**
  * pause
  * @param view
  */
 public void pause(View view) {
 if("continue".equals(bt_pause.getText().toString())){
  ();
  bt_pause.setText("pause");
  return;
 }
 if(mediaPlayer!=null&&()){
  ();
  bt_pause.setText("continue");
 }
 }
 /**
  * stop
  * @param view
  */
 public void stop(View view) {
 if(mediaPlayer!=null&&()){
  ();
  ();
  mediaPlayer = null;
 }
 bt_pause.setText("pause");
 bt_play.setEnabled(true);
 }
 /**
  * Replay
  * @param view
  */
 public void replay(View view) {
 if(mediaPlayer!=null&&()){
  (0);
 }else{
  play(view);
 }
 bt_pause.setText("pause");
 }

}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.