3

I want to play a particular song in a url through the default media player

I am able to play it using Play song on default music player - android

but it is not opening the app fully to play it

Also i am able to open the music player with following code

Intent intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,Intent.CATEGORY_APP_MUSIC);


startActivity(intent);

How can i do both ?

Community
  • 1
  • 1
Bibin
  • 433
  • 5
  • 12
  • what you mean by "it's not opening the app fully to play it" plz explain. – Rucha Bhatt Joshi Jun 02 '17 at 14:02
  • it launch a compact version of the player as mentioned in https://stackoverflow.com/questions/30937370/play-song-on-default-music-player-android you can see the screenshot in that post. – Bibin Jun 03 '17 at 04:11

1 Answers1

1

Every manufacturer will have its default music player, but if you still want to open default android music player which google provides i.e Google Play Music then you can open it with following code:-

This code will open Google Play Music if its installed otherwise it will open some other music player.

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    //Check for Google Play Music exist
    if (isPackageInstalled("com.google.android.music", getPackageManager())) 
    {
        Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.google.android.music");
        startActivity(LaunchIntent);
    } 
    else
    {
    else
    {
        //Your previous code goes here
        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/alarm.mp3");
        if (file!=null)
        {
            intent.setDataAndType(Uri.fromFile(file), "audio/*");
            startActivity(intent);
        }
        else
        {
            Toast.makeText(Music.this,"Sound Track missing",Toast.LENGTH_LONG);
        }
    }

}

private boolean isPackageInstalled(String packagename, PackageManager packageManager) 
{
    try 
    {
        packageManager.getPackageInfo(packagename, 0);
        return true;
    } 
    catch (PackageManager.NameNotFoundException e) 
    {
        return false;
    }
}
Akshay Katariya
  • 1,464
  • 9
  • 20
  • this may open the default music player .but the actual question is how to play a particular song in that music player say File file = new File(Environment.getExternalStorageDirectory().getPath()+"/Over the Horizon.mp3"); – Bibin Jun 02 '17 at 13:05
  • it launch a compact version of the player as mentioned in https://stackoverflow.com/questions/30937370/play-song-on-default-music-player-android you can see the screenshot in that post. – Bibin Jun 02 '17 at 15:01