Can someone help me with Android RemoteControlClient?

ぐ巨炮叔叔 提交于 2019-12-20 21:57:09

问题


I'm trying to get the RemoteControlClient set up so my app's music can be controlled by the widget that pops up on the lock screen (like SoundCloud, Google Play Music, and other music/video apps work). I'm not sure what's wrong with my code and why it isn't correctly hooking, but here's what I have so far...

A class called MusicService that tries to handle the updates to the RemoteControlClient

public class MusicService extends Service
{
public static final String ACTION_PLAY = "com.stfi.music.action.PLAY";
private RemoteController controller = null;

@Override
public void onCreate()
{
    super.onCreate();

    System.out.println("Creating the service.");

    if(controller == null)
    {
        controller = new RemoteController();
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    String action = intent.getAction();
    System.out.println("Got an action of " + action);

           /* Logic to get my Song cur */
    controller.register(this);
    controller.updateMetaData(cur);

    return START_STICKY;
}

@Override
public void onDestroy()
{
    super.onDestroy();
    System.out.println("Destorying MusicService");
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

This uses a class I have called RemoteController which houses my RemoteControlClient.

public class RemoteController { 
private RemoteControlClient remoteControlClient;
private Bitmap dummyAlbumArt;


public void register(Context context)
{
    if (remoteControlClient == null)
    {
        System.out.println("Trying to register it.");

        dummyAlbumArt = BitmapFactory.decodeResource(context.getResources(), R.drawable.dummy_album_art);

        AudioManager audioManager = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);

        ComponentName myEventReceiver = new ComponentName(context.getPackageName(), MediaButtonReceiver.class.getName());
        audioManager.registerMediaButtonEventReceiver(myEventReceiver);

        // build the PendingIntent for the remote control client 
        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setComponent(myEventReceiver);
        // create and register the remote control client 
        PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);
        remoteControlClient = new RemoteControlClient(mediaPendingIntent);
        remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
                | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
                | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                | RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                );
        audioManager.registerRemoteControlClient(remoteControlClient);


    }
} 

/** 
 * Update the state of the remote control. 
 */ 
public void updateState(boolean isPlaying)
{
    if(remoteControlClient != null)
    {
        if (isPlaying)
        {
            remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
        }

        else
        { 
            remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
        } 
    } 
} 

/** 
 * Updates the state of the remote control to "stopped". 
 */ 
public void stop()
{ 
    if (remoteControlClient != null)
    {
        remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
    } 
} 

public void updateMetaData(Song song)
{
    if (remoteControlClient != null && song != null)
    {
        System.out.println("Updating metadata");
        MetadataEditor editor = remoteControlClient.editMetadata(true);
        editor.putBitmap(MetadataEditor.BITMAP_KEY_ARTWORK, dummyAlbumArt);
        editor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, (long)1000);
        editor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, "Artist");
        editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, "Title");
        editor.apply();

        updateState(true);
    }
}

/** 
 * Release the remote control. 
 */ 
public void release() { 
    remoteControlClient = null;
} 
} 

Every time I want to update the widget, I call startService(new Intent(MusicService.ACTION_PLAY));. It looks like it correctly creates the service, and it always gets to the point where it says "Updating metadata", but for some reason when I lock my screen and unlock it, I don't see any widget on my lock screen.

Below is the important parts of my manifest as well, seeing as that could somehow cause the issue...

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stfi"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<application
    android:hardwareAccelerated="true"
    android:allowBackup="true"
    android:icon="@drawable/stfi"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/MyActionBarTheme" >
    <meta-data
        android:name="android.app.default_searchable"
        android:value=".activities.SearchActivity" />

    <activity
        android:name=".StartingToFeelIt"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
    ...other activities listed

    <service
        android:name=".helpers.MyNotificationService"
        android:enabled="true"
        android:label="MyNotificationServiceLabel" >
    </service>
    <service
        android:name=".music.MusicService"
        android:exported="false" >
        <intent-filter>

            <action android:name="com.stfi.music.action.PLAY" />

        </intent-filter>
        <intent-filter>
            <action android:name="com.example.android.musicplayer.action.URL" />

            <data android:scheme="http" />
        </intent-filter>
    </service>

    <receiver
        android:name=".music.MediaButtonReceiver"
        android:exported="false" >
    </receiver>
</application>

Right now my MediaButtonReceiver doesn't really do much of anything. I'm just trying to get the hooks set up. If you want, this is my MediaButtonReceiver class...

public class MediaButtonReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
    System.out.println("Receiving something.");
    if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON))
    {
        final KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

        if (event != null && event.getAction() == KeyEvent.ACTION_UP)
        {

            if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)
            {
                System.out.println("You clicked pause.");
            }

            else if(event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY)
            {
                System.out.println("You clicked play.");
            }

            else if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_NEXT)
            {
                System.out.println("You clicked next.");
            }

            else if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PREVIOUS)
            {
                System.out.println("You clicked previous.");
            }
        }
    }
}

}


回答1:


if you can't see remoteControlClient on lock screen you must implement audio focus. You can look here



来源:https://stackoverflow.com/questions/22105993/can-someone-help-me-with-android-remotecontrolclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!