Android 使用 Intent 启动音乐播放器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3114471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android launching music player using intent
提问by shaneburgess
Is it possible to open the music app from my app in android, or is it best to write a whole new music app inside of mine. I would rather use theirs since the user will already be comfortable with it.
是否可以从我的 android 应用程序中打开音乐应用程序,或者最好在我的内部编写一个全新的音乐应用程序。我宁愿使用他们的,因为用户已经习惯了。
回答by shaneburgess
I found one way to do this.
我找到了一种方法来做到这一点。
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(YOUR_SONG_PATH), "audio/*");
startActivity(intent);
回答by Rino Farina
To simply launch the music player do:
要简单地启动音乐播放器,请执行以下操作:
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);
回答by Sanjay Mangaroliya
if (Build.VERSION.SDK_INT >= 24) {
try {
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}
}
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);
回答by ViramP
There are number of way by which you can achieve default audio player but those are device and OS specific.
您可以通过多种方式实现默认音频播放器,但这些都是特定于设备和操作系统的。
With this code snippet you can get default audio player.
使用此代码片段,您可以获得默认的音频播放器。
try
{
Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
File file = new File("audiofilepath");
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
myIntent.setDataAndType(Uri.fromFile(file),mimetype);
startActivity(myIntent);
}
catch (Exception e)
{
e.printStackTrace();
}
回答by RAJESH KUMAR ARUMUGAM
This works for older versions and you May get FileUriExposed Exceptiondue to the security Update in the latest Versions. To avoid that use the following code
这适用于旧版本,由于最新版本中的安全更新,您可能会收到FileUriExposed 异常。为避免这种情况,请使用以下代码
try{
File file = new File(filepath);
String mimeType = "audio/*";
Uri fileURI = FileProvider.getUriForFile(
getApplicationContext(),
getApplicationContext()
.getPackageName() + ".provider", file);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(fileURI, mimeType);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}catch (Exception e){
Log.e(TAG,"-------------------------------------------------------FAILED TO PLAY SONG "+e);
}
Add the Following in AndroidManifest.xml
在AndroidManifest.xml 中添加以下内容
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
Create a file provider_paths.xmlunder res/xml/
在res/xml/下创建一个文件provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="."/>
</paths>
回答by Alex Ivana
You can also try this one.
你也可以试试这个。
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);