媒体播放器延迟启动
我的媒体播放器在闲置一段时间后会延迟启动,有时还会不播放音频。
之前我使用了 setDataSource()
,但这次我使用 create()
。
作为参考,这是我正在使用的代码:
AssetFileDescriptor afd = _context.getResources().openRawResourceFd(this._soundResource.get(this._toBePlayed));
mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.setOnPreparedListener(new OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
}
});
mp.prepareAsync();
My media player delays its start when kept idle for some time, also some time it does not play the audio.
Earlier I used setDataSource()
, but this time I am using create()
.
For reference this is the code I am using:
AssetFileDescriptor afd = _context.getResources().openRawResourceFd(this._soundResource.get(this._toBePlayed));
mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.setOnPreparedListener(new OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
}
});
mp.prepareAsync();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也遇到了 start() 的延迟问题。我用 mp.seekTo(0) 在 onPrepared 中修复了它,如下所示:
I also had the delay problem for start(). I fixed it with mp.seekTo(0) in onPrepared like this:
我使用文件输入流。您没有设置AudioStreamType。以下是我设置 MediaPlayer 的方法。
tempFilePath 是文件的路径。
我还在此类中实现了 setOnPreparedListener 在我看来,这使您的代码更具可读性。
还有一个很好的参考链接:MediaPlayer 状态图
I use a FileInputStream. You didn't set the AudioStreamType. Here is how I setup my MediaPlayer.
tempFilePath is the path to the file.
I also implement setOnPreparedListener in this class which makes your code a lot more readable in my opinion.
Also Great link to reference: MediaPlayer State Diagram
我认为如果您将其更改
为
并放在
mp.prepare(); 之后,您的代码就会起作用。尝试一下。
I think if you would change
to
and put
after the mp.prepare(); your code will work. Give it a try.