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 launching music player using intent
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);
How to check the media player is app is availbe or not on the device –
Antaeus
This doesn't work on the Nexus 6 (opens in photos app) –
Targett
@Targett try audio/mp3 instean of audio/* –
Sherikasherill
When I try this Android Studio says
startActivity()
wants a context instead of an intent. My compileSdkVersion
and targetSdkVersion
are 28. –
Wastrel 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. –
Wastrel @Wastrel i dont see such method startActivity(getContext(), intent, null) exists –
Dade
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. –
Fuel To simply launch the music player do:
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);
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 –
Rooke
Note that this action was deprecated in favor of CATEGORY_APP_MUSIC –
Polarity
MediaStore.INTENT_ACTION_MUSIC_PLAYER is deprecated!! –
Walkabout
This answer is now obsolete as this action was deprecated. Follow this link to learn how to use Intent.CATEGORY_APP_MUSIC. –
Broida
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>
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();
}
You can also try this one.
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This API is now obsolete. –
Dendy
© 2022 - 2024 — McMap. All rights reserved.