Android:没有调用MediaPlayer onPrepared

正如标题所说,未调用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();
上一篇:.Net Core 访问 appsettings.json


下一篇:.net core 配置设计