我知道我可以像这样在媒体播放器中播放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);
在this link之后,我试图获得如下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:// scheme获取存储在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);
}
解决方法:
当您调用startActivity()时,您正在尝试启动一个活动.传递给startActivity()的Intent表示您要启动的活动 – 或从一组可用活动中选择的活动.在您的情况下,您正在尝试查看android.resource:// Uri.这不是http:// Uri,也不是https:// Uri,也不是file:// Uri.
在他们的< intent-filter>中宣传自己作为这样的支持操作的活动.他们支持的Uri计划的声明.您假设在用户的设备上有一个支持android.resource://方案的应用程序.就个人而言,我认为这不是一个安全的假设.
http://,https://和file://应该是安全的,而content://(对于ContentProvider)也是可能的.
例如,AOSP音乐应用程序不支持基于its current manifest contents的android.resource方案.