Android launching music player using intent
Asked Answered
B

5

41

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.

Bistro answered 24/6, 2010 at 22:50 Comment(0)
B
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);
Bistro answered 29/7, 2010 at 21:48 Comment(7)
How to check the media player is app is availbe or not on the deviceAntaeus
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) existsDade
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
S
23

To simply launch the music player do:

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);
Stepup answered 28/5, 2011 at 18:31 Comment(4)
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 othersRooke
Note that this action was deprecated in favor of CATEGORY_APP_MUSICPolarity
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
C
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>
Casmey answered 18/8, 2019 at 9:37 Comment(0)
A
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();
    }
Averi answered 16/9, 2017 at 5:27 Comment(0)
R
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);
Rebut answered 5/6, 2014 at 8:27 Comment(1)
This API is now obsolete.Dendy

© 2022 - 2024 — McMap. All rights reserved.