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.
Asked
Active
Viewed 6.0k times
5 Answers
84
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);

Yogesh Choudhary Paliyal
- 604
- 1
- 8
- 23

shaneburgess
- 15,702
- 15
- 47
- 66
-
How to check the media player is app is availbe or not on the device – Qadir Hussain Jan 20 '14 at 07:01
-
This doesn't work on the Nexus 6 (opens in photos app) – nbarraille Jul 24 '15 at 21:21
-
2@nbarraille try audio/mp3 instean of audio/* – Fedor Dec 01 '16 at 07:27
-
When I try this Android Studio says `startActivity()` wants a context instead of an intent. My `compileSdkVersion` and `targetSdkVersion` are 28. – Dan Jul 10 '18 at 15:28
-
For me I needed to use `startActivity(getContext(), intent, null)`. Now it's working like a charm! Thanks!! I'm doing this from inside an adapter if that makes any difference. – Dan Jul 10 '18 at 15:41
-
2@Dan i dont see such method startActivity(getContext(), intent, null) exists – immutable Nov 12 '18 at 11:53
-
You need to add `intent.resolveActivity(CONTEX.getPackageManager())` in `if` statement to make sure the app won't crash if no app can't handle the request. – Alston Jul 06 '19 at 09:20
23
To simply launch the music player do:
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);

Rino Farina
- 1,016
- 9
- 6
-
plz will you tell me what all things should be done after writing the above code of yours.your help will really be beneficial for me and others – Hardik Vora Dec 04 '12 at 12:31
-
7Note that this action was deprecated in favor of CATEGORY_APP_MUSIC – Nik Reiman Dec 10 '12 at 10:04
-
1
-
1This answer is now obsolete as this action was deprecated. Follow this [link](https://developer.android.com/reference/android/content/Intent#CATEGORY_APP_MUSIC) to learn how to use Intent.CATEGORY_APP_MUSIC. – Sajib Acharya Oct 02 '18 at 17:22
2
This works for older versions and you May get FileUriExposed Exception due to the security Update in the latest Versions. To avoid that use the following code
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
<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.xml under res/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>

RAJESH KUMAR ARUMUGAM
- 1,560
- 21
- 35
1
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();
}

ViramP
- 1,659
- 11
- 11
0
You can also try this one.
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Alex Ivana
- 21
- 6