Android MediaPlayer prepare失败:status = 0x1

时间:2022-10-19 18:37:28

I'm building an audio recorder that needs to play back the recorded sound. I'm running into a lot of trouble playing back the audio. I know that the file exists because I isolated it into a folder on my SD Card, but for some reason it can't play it back.

我正在制作一个录音机,需要回放录制的声音。我在回放音频时遇到了很多麻烦。我知道文件存在,因为我把它隔离在SD卡上的一个文件夹里,但是由于某种原因它不能回放。

Here is my code:

这是我的代码:

public class RecorderEditActivity extends SherlockActivity implements
    DatabaseHelper.MetadataListener {

private long position;

private Button playButton = null;
private TextView date = null;
private EditText title = null;
private EditText notes = null;
private Button saveButton = null;

private MediaPlayer mediaPlayer = null;
private boolean isPlaying = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.edit_item);

    position = getIntent().getExtras().getLong(DatabaseHelper.KEY_ROWID);

    playButton = (Button) findViewById(R.id.play);
    date = (TextView) findViewById(R.id.recording_date);
    title = (EditText) findViewById(R.id.edit_title);
    notes = (EditText) findViewById(R.id.edit_notes);
    saveButton = (Button) findViewById(R.id.save_edit_button);

    mediaPlayer = new MediaPlayer();

    DatabaseHelper.getInstance(getApplicationContext()).getMetadataAsync(
            position, this);

    playButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!isPlaying) {
                playButton.setText(R.string.stop_text);
                try {
                    mediaPlayer = new MediaPlayer();
                    mediaPlayer
                            .setDataSource("sdcard/test/"
                                    + date.toString() + ".3gp");
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            mp.stop();
                        }
                    });
                } catch (Exception e) {
                    Log.e(e.getClass().getName(), e.getMessage(), e);
                }
            } else {
                playButton.setText(R.string.play_text);
                mediaPlayer.release();
            }
            isPlaying = !isPlaying;
        }
    });

Here are the errors from LogCat:

以下是来自LogCat的错误:

03-20 00:33:40.963: E/MediaPlayer(21268): error (1, -2147483648)
03-20 00:33:40.963: E/java.io.IOException(21268): Prepare failed.: status=0x1
03-20 00:33:40.963: E/java.io.IOException(21268): java.io.IOException: Prepare failed.: status=0x1
03-20 00:33:40.963: E/java.io.IOException(21268):   at android.media.MediaPlayer.prepare(Native Method)
03-20 00:33:40.963: E/java.io.IOException(21268):   at com.packagename.soundrecorder.RecorderEditActivity$1.onClick(RecorderEditActivity.java:56)
03-20 00:33:40.963: E/java.io.IOException(21268):   at android.view.View.performClick(View.java:4204)
03-20 00:33:40.963: E/java.io.IOException(21268):   at android.view.View$PerformClick.run(View.java:17355)
03-20 00:33:40.963: E/java.io.IOException(21268):   at android.os.Handler.handleCallback(Handler.java:725)
03-20 00:33:40.963: E/java.io.IOException(21268):   at android.os.Handler.dispatchMessage(Handler.java:92)
03-20 00:33:40.963: E/java.io.IOException(21268):   at android.os.Looper.loop(Looper.java:137)
03-20 00:33:40.963: E/java.io.IOException(21268):   at android.app.ActivityThread.main(ActivityThread.java:5041)
03-20 00:33:40.963: E/java.io.IOException(21268):   at java.lang.reflect.Method.invokeNative(Native Method)
03-20 00:33:40.963: E/java.io.IOException(21268):   at java.lang.reflect.Method.invoke(Method.java:511)
03-20 00:33:40.963: E/java.io.IOException(21268):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-20 00:33:40.963: E/java.io.IOException(21268):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-20 00:33:40.963: E/java.io.IOException(21268):   at dalvik.system.NativeStart.main(Native Method)

Hopefully someone can help me interpret and solve this problem. I've done a ton of research and nothing I do helps. Thanks :)

希望有人能帮助我解释并解决这个问题。我做了大量的研究,但我所做的一切都无济于事。谢谢:)

3 个解决方案

#1


7  

I figured it out: Stupid mistakes as usual. I forgot to add the read external memory permission, and I have Vasily to thank for that fix because of your comment above. The other thing was that I didn't call getText() on my date TextView so it didn't properly set the file name.

我明白了:一如既往地犯愚蠢的错误。我忘记添加读外部内存权限了,我有Vasily来感谢这一修正,因为你上面的评论。另一件事是我没有在我的日期TextView上调用getText(),所以它没有正确设置文件名。

#2


3  

I think you are facing the issue due to an incorrect DataSource being set to your MediaPlayer. Can you please try your setDataSource with the following change

我认为您正面临这个问题,因为您的MediaPlayer设置了一个错误的数据源。您是否可以尝试您的setDataSource与以下更改

mediaPlayer.setDataSource("file://sdcard/test/"
             + date.toString() + ".3gp");

I think with this change, your code should work fine

我认为有了这个更改,您的代码应该可以正常工作

EDIT: Please check this post MediaPlayer Preparing Failed for more details on the reason behind prefixing file:// to your file name.

编辑:请检查这篇文章MediaPlayer准备失败的原因更多的前缀文件://到你的文件名。

#3


1  

I hope following code will help:

我希望下面的代码能有所帮助:

String soundPath = Environment.getExternalStorageDirectory().getPath() + "test/" + date.toString() + ".mp3";
mediaPlayer.setDataSource(soundPath);

#1


7  

I figured it out: Stupid mistakes as usual. I forgot to add the read external memory permission, and I have Vasily to thank for that fix because of your comment above. The other thing was that I didn't call getText() on my date TextView so it didn't properly set the file name.

我明白了:一如既往地犯愚蠢的错误。我忘记添加读外部内存权限了,我有Vasily来感谢这一修正,因为你上面的评论。另一件事是我没有在我的日期TextView上调用getText(),所以它没有正确设置文件名。

#2


3  

I think you are facing the issue due to an incorrect DataSource being set to your MediaPlayer. Can you please try your setDataSource with the following change

我认为您正面临这个问题,因为您的MediaPlayer设置了一个错误的数据源。您是否可以尝试您的setDataSource与以下更改

mediaPlayer.setDataSource("file://sdcard/test/"
             + date.toString() + ".3gp");

I think with this change, your code should work fine

我认为有了这个更改,您的代码应该可以正常工作

EDIT: Please check this post MediaPlayer Preparing Failed for more details on the reason behind prefixing file:// to your file name.

编辑:请检查这篇文章MediaPlayer准备失败的原因更多的前缀文件://到你的文件名。

#3


1  

I hope following code will help:

我希望下面的代码能有所帮助:

String soundPath = Environment.getExternalStorageDirectory().getPath() + "test/" + date.toString() + ".mp3";
mediaPlayer.setDataSource(soundPath);