androidaudiogoogle-music

Get Audio File of Song from Google Play Music in Android Development


I am trying to make a music player app, and I want to get the file of a song that the user selects in my app from their Google Play Music library. Is there any way I can do this? If not, is there a way to detect a song they are playing in the background, possibly from a different app, and then grab that audio file?


Solution

  • is there a way to detect a song they are playing in the background

    Yes there is a way.

    For simple check whether music is playing or not. Use

    AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
    if(manager.isMusicActive())
     {
         // Something is being played.
     }
    

    And for getting info of songs playing in other app

    public class CurrentMusicTrackInfoActivity extends Activity {
    
        public static final String SERVICECMD = "com.android.music.musicservicecommand";
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            IntentFilter iF = new IntentFilter();
            iF.addAction("com.android.music.metachanged");
            iF.addAction("com.android.music.playstatechanged");
            iF.addAction("com.android.music.playbackcomplete");
            iF.addAction("com.android.music.queuechanged");
            iF.addAction("com.htc.music.metachanged");
            iF.addAction("fm.last.android.metachanged");
            iF.addAction("com.sec.android.app.music.metachanged");
            iF.addAction("com.nullsoft.winamp.metachanged");
            iF.addAction("com.amazon.mp3.metachanged");     
            iF.addAction("com.miui.player.metachanged");        
            iF.addAction("com.real.IMP.metachanged");
            iF.addAction("com.sonyericsson.music.metachanged");
            iF.addAction("com.rdio.android.metachanged");
            iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged");
            iF.addAction("com.andrew.apollo.metachanged");
    
            registerReceiver(mReceiver, iF);
        }
    
        private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                String cmd = intent.getStringExtra("command");
                Log.v("tag ", action + " / " + cmd);
                String artist = intent.getStringExtra("artist");
                String album = intent.getStringExtra("album");
                String track = intent.getStringExtra("track");
                Log.v("tag", artist + ":" + album + ":" + track);
                Toast.makeText(CurrentMusicTrackInfoActivity.this, track, Toast.LENGTH_SHORT).show();
            }
        };
    
    }
    

    For details.