Search code examples
androidmp3android-mediaplayer

How Can I stop mp3 player on menu


I create an items on menu to play mp3. But It couldnt stop. How could I stop mediaplayer when I click "stop" button. I want that clicking stop button must be stopp all playing mp3 on menu. thanks...

public class DetailActivity extends AppCompatActivity {

Toolbar mToolbar;
ImageView mFlower;
TextView mDescription,mBaslik;
Button fab;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);
    mToolbar = findViewById(R.id.toolbar);
    mToolbar.setTitle(getResources().getString(R.string.app_name));
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("MP3  --->");
   }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_settings) {

        MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.fon2);
        mediaPlayer.start();
    }
    if (id == R.id.stop) {
        mediaPlayer.stop();

        mediaPlayer.release();
       // mediaPlayer.onDestroy();
    }
    if (id == R.id.fav) {
        MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.fon2);
        mediaPlayer.start();
    }
    if (id == R.id.yor) {
        MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.fon3);
        mediaPlayer.start();
    }
    if (id == R.id.asd) {
        MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.fon1);
        mediaPlayer.start();
    }

    return super.onOptionsItemSelected(item);
}

}


Solution

  • MediaPlayer mp;
    AssetFileDescriptor descriptor;
    
    
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout./////Your activity layout////);
    
    
            if(mp != null){
                if(mp.isPlaying()){
                    mp.stop();
                }
            }
    
            try {
                descriptor = getAssets().openFd("////the name of your song.mp3////");            // song should be in assets folder
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
            audioPlayer();
        }
    
    
    
    public void audioPlayer() {
            //set up MediaPlayer
    
            mp = new MediaPlayer();
    
    
            try {
    
                mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
    
                descriptor.close();
    
                mp.prepare();
    
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
    
    public void OnPlayPressed(View view){
    
    
            if(mp.isPlaying()){
    
                mp.pause();   
    
            }else{
    
    
                mp.start();           
    
            }
    
    
        }
    
    
    
    
        public void OnStopPressed(View view){
    
    
    
            mp.stop();
    
            try {
                descriptor = getAssets().openFd("////the name of your song.mp3////");         // song should be in assets folder
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            audioPlayer();
    
        }
    

    Create 2 buttons, 1 for start/pause and one for stop. "OnPlayPressed" is the listener for the first button and "OnStopPressed" is the listener for the second button.

    Feel free to mess with this code as long as you get the logic behind it.

    Edit: In onStopPressed i reinitialise the descriptor so that you could once again press the "play" button and play your song again. This is not necessary though. You could just write "mp.stop();".