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)
有誰可以請告訴我我哪裏錯了?