Can MediaPlaybackservice be used for getting infor

2020-05-05 17:52发布

问题:

I wrote a class which will handle the MediaPlaybackService in android. But the code is not working.

My code for binding the serviceconnection is as follows:

Intent in = new Intent();                                              
  in.setClassName("com.android.playerapps","com.android.playerapps.MediaPlayerServiceConnection");
  ServiceConnection conn = new MediaPlayerServiceConnection();
  ctx.bindService(in, conn, 0);                 

ServiceConnection class is as follows:

public class MediaPlayerServiceConnection implements ServiceConnection {
IMediaPlaybackService mService; 
public void onServiceConnected(ComponentName name, IBinder service) {
    Log.i("MediaPlayerServiceConnection", "Connected! Name: " +name.getClassName());    

    // This is the important line
    mService = IMediaPlaybackService.Stub.asInterface(service);
    // Process org.videolan.vlc.android with component org.videolan.vlc.android.AudioService
     System.out.println( "---------------- track:");

    // If all went well, now we can use the interface
    try {       

        if (mService.isPlaying())
        { 
            //String str=getTrackName();    

            //String str=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath()+"/"+mService.getPath();
            System.out.println( "Playing track: "+this.getTrackName());
            Log.i("MediaPlayerServiceConnection", "Music player is playing.");
        } 
        else 
        {
            Log.i("MediaPlayerServiceConnection", "Music player is not playing.");
        }
    } 
    catch (Exception e) {
        e.printStackTrace();
        //throw new RuntimeException(e);
    }
}

public void onServiceDisconnected(ComponentName name) {
    Log.i("MediaPlayerServiceConnection", "Disconnected!");
}

public String getTrackName() {
    try {
        return ( mService.getTrackName() );
    } catch (RemoteException e) {
        Log.e("MediaConnection", "Failed to get TrackName");
        return "Failed to get TrackName";
    }           
}

Plz help!!

回答1:

Two problems here:

1 - The IMediaPlaybackService is not accessible from other applications. This is because this service is not exported in the manifest file of the Music app since Gingerbread (Android 2.3) (see packages/apps/Music/AndroidManifest.xml in the Android source code):

<service android:name="com.android.music.MediaPlaybackService" 
    android:exported="false" />

When the value is false, only components of the same application or applications with the same user ID can start the service or bind to it.

The Music app could be rebuilt changing the XML attribute to true and installed again on the device, but this would lead to the problem number 2

2 - The MediaPlaybackService is part of the built-in Android Music app and it is used only by this application internally. If a 3rd party music player is installed on the device, this won’t use the MediaPlaybackService and no information about the playback will be available through its API.

Unfortunately, no public system service provides the same API to get access to information about the current audio track or to interact with the current playback.

This is probably because the Android team decided to let each music player manage the internal information about the audio track, rather than relying on a central service that keeps track of what’s being played.