我知道我可以在媒体播放器一样,播放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的解释。 现在我明白为什么它不工作。 但问题仍然存在,我可以实现我想要什么? 获取文件的乌里存储在原始/资产的文件夹与文件://方案?
更新3
我发现了一个办法做到这一点,虽然这不是它的工作原理是最好的。 我只有3个文件,这并不耽误执行的。 我复制从RES /生到手机上的本地目录中的文件,并从该文件中获取的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);
}