安卓MediaPlayer准备失败: 状态=0x1

9

我正在制作一个音频记录器,需要回放录制的声音。但是在回放音频方面遇到了很多麻烦。我知道文件存在,因为它被隔离到我的SD卡上的一个文件夹中,但由于某种原因无法回放。

这是我的代码:

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;
        }
    });

以下是来自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)

希望有人能帮助我解释和解决这个问题。我已经做了大量的研究,但是我所做的一切都没有帮助。 谢谢 :)

2
在你的代码中,mediaPlayer 对象被创建了两次,即在循环外部和 onClick 函数中。你尝试过在 getInstance 调用之前移除 mediaPlayer 对象吗? - Ganesh
请发一下你正在尝试播放的文件。 - Vasily Sochinsky
3
你的清单文件中有没有 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 这一权限声明? - Vasily Sochinsky
3个回答

7
我弄明白了: 像往常一样,愚蠢的错误。我忘记添加读取外部存储权限,我要感谢Vasily在上面评论中提供的解决方法。 另外一件事是我没有调用我的日期TextView的getText()方法,所以它没有正确设置文件名。

你能否接受这个作为有效答案呢?谢谢。 - shkschneider

4

我认为您面临的问题是由于错误的DataSource设置到您的MediaPlayer上导致的。请尝试使用以下更改的setDataSource方法。

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

我认为通过这个改变,你的代码应该可以正常工作。
编辑:请查看此帖子 MediaPlayer 准备失败 ,以获取关于在文件名前加上file://的原因的更多细节。

1
我希望下面的代码可以帮到您:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接