这是我创建我的媒体播放器并设置它的源的方法,但是在我调用seekTo之后它就不再播放了(如果我给它一段时间--取决于音频,它在调试后-->结论)。
public void makeAudio(boolean isInit){ if(!isInit){ mediaPlayer = new MediaPlayer(); soundSource = episode.getAudio(); mediaPlayer.setDataSource(soundSource); mediaPlayer.prepareAsync(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { soundLength = mediaPlayer.getDuration(); seekBar.setMax(soundLength/1000); mediaPlayer.seekTo(progress);//the problem I guess }catch (Exception e){ e.printStackTrace(); }
它向我显示以下错误消息:
E/MediaPlayerNative: Attempt to perform seekTo in wrong state: mPlayer=0x706c23ebc0, mCurrentState=0
我想对此有任何建议,因为我想让我的mediaplayer转到进度值(我从我的意图中获得它)。
发布于 2021-09-14 21:01:46
我建议你使用ExoPlayer,它是MediaPlayer的改进版本,addListener对在线音频效果更好。
您必须添加侦听器,直到它准备就绪,然后您才能获得持续时间:
exoPlayer.addListener(new Player.Listener() { @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { if (playbackState == ExoPlayer.STATE_READY) { long realDurationMillis = exoPlayer.getDuration(); seekBar.setMax((int)realDurationMillis/1000); dialog.dismiss();