我知道我可以像这样在媒体播放器中播放 mp3 文件:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
按照此链接,我尝试获取如下 URI:
Uri audio = Uri.parse("android.resource://com.audio.test/"+R.raw.audio1);
Log.d(TAG,"uri:"+audio.toString());
和
Uri audio = Uri.parse("android.resource://com.audio.test/raw/audio");
Log.d(TAG,"uri:"+audio.toString());
输出预期结果:
01-24 15:28:23.190: D/MyClass(30234): uri:android.resource://com.audio.test/2131034112
01-24 15:29:13.: D/MyClass(30234): uri:android.resource://com.audio.test/raw/audio1
但它不起作用。媒体播放器不启动。任何想法为什么?
更新
我包含了一个 createChooser,而不是预期的玩家列表,我收到“无法找到执行此操作的应用程序”消息。这是我的确切代码:
public void playAudio(){
Intent viewMediaIntent = new Intent();
viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);
Uri audio = Uri.parse("android.resource://com.audio.test/raw/"+R.raw.audio1);
Log.d(TAG,"uri:"+audio.toString());
viewMediaIntent.setDataAndType(audio, "video/*");
viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Log.d(TAG,"Starting");
Intent i = Intent.createChooser(viewMediaIntent, "Play Music");
mGap.startActivity(i);
Log.d(TAG,"Started");
}
更新 2
谢谢@CommonsWare 的解释。现在我明白为什么它不起作用了。但问题依然存在,我能达到我想要的吗?使用 file:// 方案获取存储在 raw/assets 文件夹中的文件的 Uri?
更新 3
我找到了一种方法来做到这一点,虽然它不是最好的。我只有 3 个文件,这根本不会延迟执行。我正在将文件从 res/raw 复制到手机上的本地目录并从该文件中获取 Uri。任何有关如何避免该步骤的建议表示赞赏。
public void copyFileToDir(String audioFile){
File test = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/" + audioFile + ".mp3");
if (test.exists()){
Toast.makeText(mGap, "Exists", Toast.LENGTH_SHORT).show();
return;
}
File dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
int i = mGap.getResources().getIdentifier("raw/"+audioFile, "string", mGap.getPackageName());
InputStream in = mGap.getResources().openRawResource(i);
// Used the File-constructor
OutputStream out;
try {
out = new FileOutputStream(new File(dest, audioFile + ".mp3"));
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
try {
// A little more explicit
while ( (len = in.read(buf, 0, buf.length)) != -1){
out.write(buf, 0, len);
}
} finally {
// Ensure the Streams are closed:
in.close();
out.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void playAudio(String audioFile){
copyFileToDir(audioFile);
File dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/" + audioFile + ".mp3");
Uri r = Uri.fromFile(dest);
Intent viewMediaIntent = new Intent();
viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);
viewMediaIntent.setDataAndType(r, "audio/*");
viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Intent i = Intent.createChooser(viewMediaIntent, "Play Music");
mGap.startActivity(i);
}
public void playVideo(String movieurl){
Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
intentToPlayVideo.setDataAndType(Uri.parse(movieurl), "video/*");
Log.d(TAG,"Playing:" + movieurl);
mGap.startActivity(intentToPlayVideo);
}