Android MediaPlayer 在 2.1 上抛出“准备失败。:状态 = 0x1”,适用于 2.2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3761305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android MediaPlayer throwing "Prepare failed.: status=0x1" on 2.1, works on 2.2
提问by Nik Reiman
I've been really banging my head against the table trying to get the MediaPlayer class to try to play h.264-encoded videos on Android 2.1. My code is rather simple:
我一直在努力让 MediaPlayer 类尝试在 Android 2.1 上播放 h.264 编码的视频。我的代码相当简单:
AssetFileDescriptor fileDescriptor = getResources().openRawResourceFd(R.raw.my_movie);
introMoviePlayer = new MediaPlayer();
introMoviePlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getDeclaredLength());
introMoviePlayer.prepare();
This always throws an exception at prepare()
, with the text java.io.IOException: Prepare failed.: status=0x1
. I got a bit more info by using MediaPlayer.create()
with a URI, which also throws at prepare()
, which is actually called by MediaPlayer.create()
, with the message Command PLAYER_PREPARE completed with an error or info PVMFErrResourceConfiguration
.
这总是在 处引发异常prepare()
,文本为java.io.IOException: Prepare failed.: status=0x1
。我通过使用MediaPlayer.create()
URI获得了更多信息,该 URI 也抛出 at prepare()
,实际上是由 调用的MediaPlayer.create()
,带有消息Command PLAYER_PREPARE completed with an error or info PVMFErrResourceConfiguration
。
The same code works perfectly in Froyo (2.2). The videos themselves play fine in the video player app. Does anyone have perhaps a helpful hint that might help to solve this problem?
相同的代码在 Froyo (2.2) 中完美运行。视频本身在视频播放器应用程序中播放良好。有没有人有可能有助于解决这个问题的有用提示?
Edit: Still no solution -- very frustrating problem indeed. However, I have found that by creating a VideoView
and setting the URI for the raw video works. This is very puzzling, as sending the exact same URI through a MediaPlayer class will throw.
编辑:仍然没有解决方案 - 确实非常令人沮丧的问题。但是,我发现通过VideoView
为原始视频作品创建和设置 URI。这非常令人费解,因为通过 MediaPlayer 类发送完全相同的 URI 会抛出异常。
采纳答案by Ryan Heitner
I do not know if this is your issue, but I just found a solution to the problem the described by Tuszy above. I could read the file I was creating from external storage but not from Cache.
我不知道这是否是您的问题,但我刚刚找到了上面 Tuszy 描述的问题的解决方案。我可以从外部存储读取我创建的文件,但不能从缓存中读取。
The solution is that the read write permissions when writing the file are different.
解决办法是写文件时的读写权限不同。
Please see this excellent explanation in this blog I found.
请在我发现的这个博客中看到这个很好的解释。
http://blog.weston-fl.com/android-mediaplayer-prepare-throws-status0x1-error1-2147483648/
http://blog.weston-fl.com/android-mediaplayer-prepare-throws-status0x1-error1-2147483648/
回答by MadMurdok
This is my solution:
这是我的解决方案:
MediaPlayer mediaPlayer = new MediaPlayer();
FileInputStream fis = null;
try {
File directory = new File("android.resource://com.example.myapp/raw/");
fis = new FileInputStream(directory);
mediaPlayer.setDataSource(fis.getFD());
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException ignore) {
}
}
}
回答by Anthony Roe
I know that I am late here but hopefully this helps somebody else. I worked around this issue by setting up a setOnCompletionListener
callback that explicitly releases the MediaPlayer object after the media is done playing.
我知道我来晚了,但希望这对其他人有所帮助。我通过设置一个setOnCompletionListener
回调来解决这个问题,该回调在媒体播放完毕后显式释放 MediaPlayer 对象。
I can't take credit for this solution, as it was originally posted by Ronny here: How do you detect when a sound file has finished?
我不能相信这个解决方案,因为它最初是由 Ronny 在这里发布的: 你如何检测声音文件何时完成?
But since this is the first answer when I search for Android+Failed Status 0x1 here is my code that solved this problem for me:
但是因为这是我搜索 Android+Failed Status 0x1 时的第一个答案,所以我的代码为我解决了这个问题:
public void playSound(String soundPath){
MediaPlayer m = new MediaPlayer();
m.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
try {
AssetFileDescriptor descriptor = mContext.getAssets().openFd(soundPath);
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),
descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(100f, 100f);
m.setLooping(false);
m.start();
} catch (Exception e) {
//Your catch code here.
}
}
回答by Paul Lammertsma
If you are using multiple instances of MediaPlayer
, make sure that you've executed release()
on a resource before attempting to use it in a different instance.
如果您使用 的多个实例MediaPlayer
,请确保release()
在尝试在不同实例中使用资源之前已在资源上执行。
回答by ani0904071
initilize the fileName, mediaPlayer instance:
初始化 fileName、mediaPlayer 实例:
private MediaPlayer mp;
private final static String fileName = "ring";
for playing/starting audio file from res/raw directory:
从 res/raw 目录播放/启动音频文件:
try
{
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_RING); //set streaming according to ur needs
mp.setDataSource(Context, Uri.parse("android.resource://yourAppPackageName/raw/"+fileName));
mp.setLooping(true);
mp.prepare();
mp.start();
} catch (Exception e)
{
System.out.println("Unable to TunePlay: startRingtone(): "+e.toString());
}
finally stopping the audioFile:
最后停止audioFile:
try
{
if (!(mp == null) && mp.isPlaying())
{
mp.stop();
mp.release(); //its a very good practice
}
}
catch (Exception e)
{
System.out.println("Unable to TunePlay: stopRingtone():"+e.toString());
}
回答by Aleks G
I know this may be too late. I managed to use video from resources as this:
我知道这可能为时已晚。我设法使用资源中的视频,如下所示:
MediaPlayer mp = ...;
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/" + R.raw.my_movie);
mp.setDataSourceUri(this, uri);
回答by srgtuszy
I had a similar problem. I had a zip file which contained sound files which were compressed. In order to pass them on to MediaPlayer
they needed to be uncompressed. When I placed them inside application's cache directory (context.getCacheDir()
), MediaPlayer
returned the same error which you described.
我有一个类似的问题。我有一个 zip 文件,其中包含压缩的声音文件。为了将它们传递给它们,MediaPlayer
它们需要被解压缩。当我将它们放在应用程序的缓存目录 ( context.getCacheDir()
) 中时,MediaPlayer
返回了您描述的相同错误。
But when I placed them on external storage (Environment.getExternalStorageDirectory()
), everything started working.
但是当我将它们放在外部存储 ( Environment.getExternalStorageDirectory()
) 上时,一切都开始工作了。
回答by SAWAUNG
On my opinion i will ask you to check back your location of your media files<< SONG PATH or MOVIE PATH >>. I faced with this exception, i have overcome by correct "SONG PATH"
在我看来,我会要求您检查媒体文件的位置<< SONG PATH 或 MOVIE PATH >>。我遇到了这个异常,我已经通过正确的“歌曲路径”克服了
回答by Jason Rogers
first thanks for your code, the open raw ressource helped me with my problem.
首先感谢您的代码,开放的原始资源帮助我解决了我的问题。
I'm sorry I don't have a definite answer to your problem but based on the api media example in the SDK and my media player in my project I would suggest checking that the file opens correctly.
很抱歉,我对您的问题没有明确的答案,但根据 SDK 中的 api 媒体示例和我的项目中的媒体播放器,我建议检查文件是否正确打开。
otherwise maybe you need to set the display before preparing as such:
否则,您可能需要在准备之前设置显示:
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getDeclaredLength());
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
those are the only ideas I have
这些是我唯一的想法
hope it helps
希望能帮助到你
Jason
杰森
回答by erfan
I found Best and clear solution for this error : java.io.IOException: Prepare failed.: status=0x1
我找到了此错误的最佳解决方案: java.io.IOException: Prepare failed.: status=0x1
Note
笔记
if you want read from your sd card and got this error , your are not set<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
in your Manifast .
如果你想从你的 SD 卡中读取并得到这个错误,你<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
的 Manifast没有设置。
Note 2
笔记2
if your are want play from url and got that error maybe your are set setDataSource
to like this :https://www.android.com
just remove S from http
like this http://www.android.com
如果你是想从网址发挥并得到了这个错误,也许你的设置setDataSource
,以这样的:https://www.android.com
刚刚从删除小号http
这样http://www.android.com