Android MediaPlayer播放异常问题汇总

本文详细解析了MediaPlayer在不同状态下引发的错误,并提供了相应的解决策略。主要包括:在异步状态下调用stop方法导致的错误、在prepareAsync后立即调用start方法的错误、在文件加载前调用getDuration方法的错误,以及无法播放特定编码格式的MP4视频的问题。通过调整代码逻辑和视频格式,可以有效避免这些错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、关于 “MediaPlayer error (XXXX,XXXX) 

问题1:

E/MediaPlayer(19765): stop called in state 4
E/MediaPlayer(19765): error (-38, 0)

原因:是在调用prepareAsync()则以异步方式进入Prepared状态过程中即preparing状态中,调用了stop方法。

参考:http://stackoverflow.com/questions/8796956/mediaplayer-stop-called-in-state-4

问题2:

Media Player called in state 0, error (-38,0)

原因是在You're using prepareAsync, which is asynchronous. That is, you should wait for the onPrepared callback before you do anything that relies on the preparation to be complete (like calling start). That's why you get the "start called in state 4" error message (state 4 is MEDIA_PLAYER_PREPARING).

即调用 prepareAsync,在preparing过程中调用了start。

参考:http://stackoverflow.com/questions/16495276/mediaplayer-track-change-issue 

问题3:

Attempt to call getDuration without a valid mediaplayer in media player on android

原因:在preparing过程中调用了getDuration方法。

You might be calling getDuration before the file is fully loaded.

参考:http://stackoverflow.com/questions/6026288/attempt-to-call-getduration-without-a-valid-mediaplayer-in-media-player-on-andr 

问题4:如果你使用VideoView播放过MP4视频,你可能碰到过类似下面的问题:

MediaPlayer error (1, -2147483648)

如果你查阅文档,会发现1其实代表MEDIA_ERROR_UNKNOWN,不过文档对-2147483648(0x80000000)没有做什么说明,实际上它也是代表unknown error的意思。

真正的原因在于,MP4有多种编码格式,例如H.264,H.263等,而android版本较低的机器只支持部分编码。

一旦遭遇不被支持的编码格式,MediaPlayer可能就会抛出上面的错误信息。

如果你也遇到这类问题,你可以使用一些视频软件查看视频的编码格式,然后转换为普遍支持的格式。

stackoverflow上有关于此问题的讨论,下面是一些链接:

http://stackoverflow.com/questions/10849044/cant-play-mp4-video-in-my-android-app

http://superuser.com/questions/371460/command-encoding-h264-baseline-profile-level-1-with-ffmpeg-and-libx264

http://stackoverflow.com/questions/11540076/android-mediaplayer-error-1-2147483648

http://developer.android.com/guide/appendix/media-formats.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值