MediaPlayer 使用prepareAsync的正确姿势

Posted xiyangyang8110

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MediaPlayer 使用prepareAsync的正确姿势相关的知识,希望对你有一定的参考价值。


.reset();
mediaPlayer.setDataSource(url);
// mediaPlayer.prepare();
mediaPlayer.prepareAsync();

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
@Override
public void onPrepared(MediaPlayer mp)
Log.e("wy", "onPrepared: ");
mp.start();

);

全代码

private MediaPlayer mediaPlayer = new MediaPlayer();
if (url.contains("http"))
// load = soundPool.load(url, 1);
try
mediaPlayer.reset();
mediaPlayer.setDataSource(url);
// mediaPlayer.prepare();
mediaPlayer.prepareAsync();

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
@Override
public void onPrepared(MediaPlayer mp)
Log.e("wy", "onPrepared: ");
mp.start();

);
// mediaPlayer.start();

mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener()
@Override
public boolean onError(MediaPlayer mp, int what, int extra)
Log.e("wy", "onError what: " + what + " extra: " + extra);
ToastUtils.showShortToast(context, context.getString(R.string.file_dismiss));
animations.stop();
return false;

);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
@Override
public void onCompletion(MediaPlayer mp)
Log.e("wy", "mediaPlayer 播放完成 onCompletion: ");
animations.stop();

);
catch (Exception e)
Log.e("wy", "180 onClick: " + e.getMessage());
e.printStackTrace();

else
// load = soundPool.load("https:" + url, 1);
try
mediaPlayer.reset();
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
mediaPlayer.start();
catch (Exception e)
e.printStackTrace();


以上是关于MediaPlayer 使用prepareAsync的正确姿势的主要内容,如果未能解决你的问题,请参考以下文章

Android MediaPlayer 播放音频

MediaPlayer使用以及常见问题

MediaPlayer使用以及常见问题

使用MediaPlayer播放音频-----之二

与 MediaPlayer 一起使用后如何清除 SurfaceTexture?

选择要使用 MediaPlayer 播放的音乐文件