Reputation: 21
I am trying to build a basic music player app with only play and stop buttons on it. Here is the java code
**package com.example.android.musicplayer;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button play =(Button)findViewById(R.id.play);
Button pause = (Button)findViewById(R.id.stop);
final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.song_1);
play.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
mediaPlayer.start();
}
});
pause.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view)
{
mediaPlayer.stop();
}
});
}
}**
When I click on play and stop buttons for the first time it works but after that nothing happens. Here is what looks like on the android monitor when I try to play music again after stop it.
05-31 16:05:35.320 31018-31018/com.example.android.musicplayer E/MediaPlayer: start called in state 64, mPlayer(0x9a024be0)
05-31 16:05:35.320 31018-31018/com.example.android.musicplayer E/MediaPlayer: error (-38, 0)
05-31 16:05:35.327 31018-31018/com.example.android.musicplayer E/MediaPlayer: Error (-38,0)
Upvotes: 2
Views: 2995
Reputation: 93
The Simple way to stop the Media Player is :
if(mediaPlayer!=null)
{
mediaPlayer.stop();
mediaPlayer.prepareAsync();
}
Upvotes: 2
Reputation: 13
use this when you click on pause button
if (mediaPlayer!= null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer= null;
}
Upvotes: 1