0

I am trying to build a small app which plays a sound when we click on the button. But I am not able to play the sound. Don't know what the problem is. Please help me on this. Below is the code.

 public class MainActivity extends AppCompatActivity {

    private Button button;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaPlayer = new MediaPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.song);


        button = (Button)findViewById(R.id.mediaButtonId);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              mediaPlayer.start();

            }
        });
    }

}

Note:-Sorry guys,I thought that the problem is with my code but the app is running perfectly fine on my phone,so its the problem with my genymotion emulator.Can anyone please suggest me the solution for this.By the way,I am using Mac OSX.

Sai Sankalp
  • 451
  • 1
  • 4
  • 14

2 Answers2

1

The MediaPlayer has its own lifecycle. You can't just create the instance and then start to play. First you have to prepare it and then play it.

You can prepare your mediaplayer either sync or asynchronously.

Something along the lines of:

MediaPlayer mediaPlayer= new MediaPlayer();
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.song);

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
   mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();

Or, if you want to do it synchronously

    MediaPlayer mediaPlayer= new MediaPlayer();
    mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.song);
    try {
        mediaPlayer.prepare();
    } catch (IOException e){

    }
    mediaPlayer.start();

Just make sure you prepare it before you play it.

Media Player lifecycle: https://developer.android.com/reference/android/media/MediaPlayer.html

Rui Santos
  • 537
  • 3
  • 15
  • I have tried but I am getting ILLEGAL STATE EXCEPTION – Sai Sankalp Oct 12 '16 at 13:19
  • In stackTrace,it is showing "caused by IllegalStateException" at the line where the prepare() call is there. I have refered some sites.Some are saying that multiple prepare() calls will cause this exception. – Sai Sankalp Oct 12 '16 at 13:26
  • Are you? calling prepare() multiple times? You should only call it once after you set the data source. or create it directly with the create convenience method you used. – Rui Santos Oct 12 '16 at 13:32
  • mediaPlayer=new MediaPlayer(); mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.song); try { mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } button=(Button)findViewById(R.id.mediaButtonId); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mediaPlayer.start(); } }); – Sai Sankalp Oct 12 '16 at 13:33
  • http://stackoverflow.com/a/6061704/4215176...In this some are saying that prepare method is not required – Sai Sankalp Oct 12 '16 at 13:36
  • Try passing the activity context instead of application context in the MediaPlayer constructor – Rui Santos Oct 12 '16 at 13:37
  • I have checked whether the media player is playing my file or not using isPlaying() method.It showed true.So I think the problem is with my sound? – Sai Sankalp Oct 12 '16 at 13:59
1

You need to make sure the media player is ready before you can play it, so you set the onPreparedListener to handle this for you, like so:

MediaPlayer mp = new MediaPlayer();

mp = MediaPlayer.create(getApplicationContext(),R.raw.song);

mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mp.start();
    }
});

button = (Button)findViewById(R.id.mediaButtonId);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      mp.prepareAsync();
    }
});

There will be a slight delay from when you press the button to the sound playing this way. Another way to do it could be to disable the button until the media player has prepared and then in the onclick of the button you could just call mp.start(); when the button has been enabled.

MichaelStoddart
  • 5,571
  • 4
  • 27
  • 49
  • sorry not working.I think the problem is with my emulator.I am using genymotion.when I use isPlaying() method,it is showing true which means the code is running fine.But I am not able to hear sound – Sai Sankalp Oct 12 '16 at 15:38
  • test on a real device @SaiSankalp – MichaelStoddart Oct 12 '16 at 15:39
  • Yeah thanks I have tested it its working fine on my phone.but I am not able to understand what is the problem with my emulator @MichealStoddart – Sai Sankalp Oct 12 '16 at 15:45