2013-03-19 138 views
9

我正在構建需要播放錄製的聲音的錄音機。播放音頻時遇到了很多麻煩。我知道該文件存在,因爲我將它隔離在SD卡上的一個文件夾中,但出於某種原因它無法播放它。Android MediaPlayer準備失敗:狀態= 0x1

這裏是我的代碼:

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'對象。你試過在'getInstance'調用之前移除'mediaPlayer'對象嗎? – Ganesh 2013-03-19 22:47:49

+0

請發佈您正在嘗試播放的文件 – 2013-03-19 23:27:16

+2

您的清單文件中是否包含? – 2013-03-19 23:34:12

回答

7

我想通了: 像往常一樣愚蠢的錯誤。 我忘了添加讀取的外部內存權限,並且由於上面的評論,我讓瓦西里感謝您的修復。 另一件事是我沒有在我的日期TextView上調用getText(),所以沒有正確設置文件名。

+0

請您接受這個作爲有效答案。謝謝。 – shkschneider 2015-04-30 12:59:37

3

我認爲你面臨的問題是由於不正確的DataSource被設置爲MediaPlayer。能否請您試試你的setDataSource有以下變化

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

我覺得這種變化,你的代碼應該工作正常

編輯:請查看這篇文章MediaPlayer Preparing Failed,詳細瞭解背後的原因前綴file://到你的文件名。

1

我希望下面的代碼將有助於:在`onClick`功能正在創建即兩次一次循環,外面再

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