android - Start stop Media Player programmatically

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#a5d1b5"     android:padding="16dp"     android:orientation="vertical"     >     <Button         android:id="@+id/btn_play"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Start Playing"         />     <Button         android:id="@+id/btn_stop"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Stop"         /> </LinearLayout> 
MainActivity.java
 package com.cfsuman.me.androidcodesnippets;  import android.app.Activity; import android.content.Context; import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast;   public class MainActivity extends AppCompatActivity {     private Context mContext;     private Activity mActivity;      private Button mButtonPlay;     private Button mButtonStop;      private MediaPlayer mPlayer;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the application context         mContext = getApplicationContext();         mActivity = MainActivity.this;          // Get the widget reference from xml layout         mButtonPlay = findViewById(R.id.btn_play);         mButtonStop = findViewById(R.id.btn_stop);          // Click listener for playing button         mButtonPlay.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 // If media player another instance already running then stop it first                 stopPlaying();                  // Initialize media player                 mPlayer = MediaPlayer.create(mContext,R.raw.my_favorite_song);                  // Start the media player                 mPlayer.start();                 Toast.makeText(mContext,"Media Player is playing.",Toast.LENGTH_SHORT).show();              }         });          // Set a click listener for top playing button         mButtonStop.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 stopPlaying();             }         });     }          /*             void stop ()                 Stops playback after playback has been stopped or paused.             Throws                 IllegalStateException : if the internal player engine has not been initialized.         */          /*             void release ()                 Releases resources associated with this MediaPlayer object. It is considered good                 practice to call this method when you're done using the MediaPlayer. In particular,                 whenever an Activity of an application is paused (its onPause() method is called),                 or stopped (its onStop() method is called), this method should be                 invoked to release the MediaPlayer object, unless the application has                 a special need to keep the object around.         */      protected void stopPlaying(){         // If media player is not null then try to stop it         if(mPlayer!=null){             mPlayer.stop();             mPlayer.release();             mPlayer = null;             Toast.makeText(mContext,"Stop playing.",Toast.LENGTH_SHORT).show();         }     } } 

Komentar