2017-07-06 208 views
0

我創造的MainActivity的onCreate一個MediaPlayer的實例()這樣的Android的MediaPlayer的未知錯誤

MediaPlayer mPlayer = MediaPlayer.create(this, Uri.fromFile(new File("/storage/emulated/0/soundrecorder/My recording #26.wav"))); 

方法它是成功創建,但我得到這個錯誤:

07-06 18:33:44.266 18366-18366/com.audiorecorder.wel.voicerecorder E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0 
07-06 18:33:44.267 18366-18366/com.audiorecorder.wel.voicerecorder E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0 

也試過,但在logcat同樣的錯誤:

MediaPlayer mp = new MediaPlayer(); 
    try { 
     mp.setDataSource(this, Uri.fromFile(new File("/storage/emulated/0/soundrecorder/My recording #26.wav"))); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    mp.prepareAsync(); 
    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
     @Override 
     public void onPrepared(MediaPlayer mp) { 
      mp.start(); 
     } 
    }); 

我已經嘗試過不同格式的不同音頻文件,但結果是同樣的錯誤。我也嘗試在stackoverflow上搜索答案,但無法解決問題。你能幫我解決嗎?

+0

你運行它在設備或模擬器? –

+0

運行在摩托羅拉Moto G上運行牛軋糖7.1.1 – Ali

+0

我看到的一個問題就是歌曲文件名中的空格,請嘗試刪除它們。它可能會解決。 –

回答

0

看來你的文件存在一些問題。用此更新:

musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

,然後按照下列步驟操作:

1. getLoaderManager().initLoader(0,null,this); 
    2. @Override 
     public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
      switch (id){ 
       case 0 : return new CursorLoader(getApplicationContext(),musicUri,null,null,null,null);   
      return new Loader<>(this); 
     } 

3. @Override 
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 
     switch (loader.getId()) { 
      case 0 : 
       if(data != null && data.moveToFirst()) { 
        songTitleColumnIndex = data.getColumnIndex(MediaStore.Audio.Media.TITLE); 
        do { 
         songTitle = data.getString(songTitleColumnIndex); 
         songsList.add(songTitle); 
        } while (data.moveToNext()); 
       } 
       break; 

所以SongTitles正在落實這是字符串類型的ArraList的的SongList。

希望這有助於。

+0

爲什麼要將歌曲標題放入數組列表中? – Ali

+0

我只是維護一個SongTitles ArrayList,我將顯示爲我的設備中存在的歌曲列表。 –

+0

它會解決錯誤嗎? – Ali

0

嘗試使用一個FileInputStream開始與FileDescriptor的而不是你的MediaPlayer:

String yourFilePath = "/wherever/your/file/is.wav"; 

MediaPlayer mPlayer = new MediaPlayer(); 

try{ 
    FileInputStream inputStream = new FileInputStream(yourFilePath); 
    mPlayer.setDataSource(inputStream.getFD()); 
    inputStream.close(); 

    mPlayer.prepare(); 
    mPlayer.start(); 

}catch (IOException e){ 
    Log.e("IOException", e.getMessage()); 
} 
+0

仍然有這個錯誤http://imgur.com/a/cbwBJ – Ali