2012-02-23 254 views
0

在我的應用程序中,我獲得了外部存在的音頻文件列表,並使用該文件的Uri播放第一個文件。我爲它編寫了下面的代碼。使用Uri通過媒體播放器播放音頻剪輯

public class ScrSettings extends Activity{ 
private Uri uriString; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.test); 

    Button playsong = (Button) findViewById(R.id.show_me); 
    playsong.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      uriString = getFirstSongUri(); 
      Log.i("Uri is : ", uriString.toString()); 

      MediaPlayer mp = new MediaPlayer(); 
      try { 
       mp.setDataSource(ScrSettings.this, uriString); 
       mp.prepare(); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      mp.start(); 
     } 
    }); 
} 
private Uri getFirstSongUri() { 
    Cursor media = null; 
    String songTitle = ""; 
    try { 
     // query for the numbers associated with the contact 
     media = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, 
       null, null, null, null); 

     while (media.moveToNext()) { 
      songTitle = media.getString(media.getColumnIndex(Media.DISPLAY_NAME)); 
      Uri uri = Media.getContentUri(songTitle); 
      return uri; 
     } 
    } catch (Exception e) { 
     Log.e("MediaList", "Failed to get email data", e); 
    } finally { 
     if (media != null) { 
      media.close(); 
     } 
    } 
    return null; 
} 
} 

而且它還會在logcat中記錄歌曲的Uri,但它不會播放該文件,並且出現以下異常。

02-23 16:06:19.038: W/System.err(1890): java.io.IOException: setDataSource failed.: status=0x80000000 
02-23 16:06:19.058: W/System.err(1890):  at android.media.MediaPlayer.setDataSource(Native Method) 
02-23 16:06:19.058: W/System.err(1890):  at android.media.MediaPlayer.setDataSource(MediaPlayer.java:800) 
02-23 16:06:19.058: W/System.err(1890):  at android.media.MediaPlayer.setDataSource(MediaPlayer.java:725) 
02-23 16:06:19.058: W/System.err(1890):  at com.raj.poc.test.ScrSettings$1.onClick(ScrSettings.java:51) 
02-23 16:06:19.058: W/System.err(1890):  at android.view.View.performClick(View.java:2532) 
02-23 16:06:19.058: W/System.err(1890):  at android.view.View$PerformClick.run(View.java:9293) 
02-23 16:06:19.058: W/System.err(1890):  at android.os.Handler.handleCallback(Handler.java:587) 
02-23 16:06:19.058: W/System.err(1890):  at android.os.Handler.dispatchMessage(Handler.java:92) 
02-23 16:06:19.058: W/System.err(1890):  at android.os.Looper.loop(Looper.java:150) 
02-23 16:06:19.058: W/System.err(1890):  at android.app.ActivityThread.main(ActivityThread.java:4277) 
02-23 16:06:19.058: W/System.err(1890):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-23 16:06:19.058: W/System.err(1890):  at java.lang.reflect.Method.invoke(Method.java:507) 
02-23 16:06:19.058: W/System.err(1890):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-23 16:06:19.068: W/System.err(1890):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-23 16:06:19.068: W/System.err(1890):  at dalvik.system.NativeStart.main(Native Method) 

有誰可以請告訴我我哪裏錯了?

回答

0

我想我只是在setDataSource失敗時有相同的錯誤狀態,這意味着媒體文件的格式不被MediaPlayer理解。

0

你必須使用MediaStore.Audio音頻...

media .getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.TITLE)); 

也有look here

相關問題