AndroId MediaPlayer prepareAsync method

落爺英雄遲暮 提交于 2019-11-29 06:15:07
petey

You are setting the OnPreparedListener in your Activity's create method and later on, possibly, creating a new MediaPlayer in your preparePlayer() method. Any newly created MediaPlayer's wont have the prepareListener set. You should also add an errorlistener to find out whats going on.

You may want to move the setOnPreparedListener call inside the

  if (mediaPlayer == null) {
    mediaPlayer = new MediaPlayer();
}

as well as setting a setOnErrorListener like so:

if (mediaPlayer == null) {
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnErrorListener(....);
    mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            ToggleButton playButton = (ToggleButton) findViewById(R.id.playToggleButton);
            playButton.setClickable(true);
            mp.start();
        }
    });
}

Please change e.printStackTrace(); to

Log.e("tag", e.getMessage(), e);  

Hope this helps, let us know if you have any log data as this will also be useful.

Turned out, I forgot to mention the "@Override" on top of my inner method. Like this :

mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        ToggleButton playButton = (ToggleButton) findViewById(R.id.playToggleButton);
        playButton.setClickable(true);
        mp.start();
    }
});

Now everything works fine. Well... almost, since it takes forever to buffer...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!