当前位置 : 主页 > 手机开发 > android >

Android:没有调用MediaPlayer onPrepared

来源:互联网 收集:自由互联 发布时间:2021-06-11
正如标题所说,未调用onPrepared()重写方法. mediaPlayer = new MediaPlayer();mediaPlayer.setOnPreparedListener(new OnPreparedListener(){ @Override public void onPrepared(MediaPlayer mp){ if (mp.equals(mediaPlayer)) isReady_mediaPla
正如标题所说,未调用onPrepared()重写方法.

mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(new OnPreparedListener(){
   @Override
   public void onPrepared(MediaPlayer mp){
      if (mp.equals(mediaPlayer)) isReady_mediaPlayer = true;

      Log.e("I", "Media player has been loaded to memory !");
   }
});

mediaPlayer = MediaPlayer.create(this, R.raw.my_sound_mp3);
这是因为您正在创建一个新的MediaPlayer对象.这个没有设置任何onPreparededListener.

在这里,您可以创建并分配MediaPlayer对象:

mediaPlayer = new MediaPlayer();

接下来,在其上设置onPreparedListener.

但是你创建并分配一个新的!此作业会覆盖您之前的行.

mediaPlayer = MediaPlayer.create(this, R.raw.my_sound_mp3);

这个没有设置onPreparedListener.

要修复,请不要使用create方法,而是使用setDataSource

AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.my_sound_mp3);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(new OnPreparedListener(){
    @Override
    public void onPrepared(MediaPlayer mp){
       if (mp.equals(mediaPlayer)) isReady_mediaPlayer = true;
       Log.e("I", "Media player has been loaded to memory !");
    }
});
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
网友评论