在我的应用程序中,我获取外部存在的音频文件列表,并使用该文件的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;
}
}
它还会将歌曲的Uri记录在logcat中,但它不播放该文件,我得到以下异常。
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 :(得分:0)
我认为我在setDataSource失败时遇到了相同的错误状态,这意味着MediaPlayer无法理解媒体文件的格式。
答案 1 :(得分:0)
对于音频,您必须使用 MediaStore.Audio ...
media .getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.TITLE));