10

我已经阅读了锁屏小部件文档,我实现了它,但这不是自动放置在主锁窗口上的内容。我正在寻找通过主锁屏窗口(在 Jelly Bean 及更高版本中)提供媒体控制的解决方案,例如 Google Play 音乐应用程序。

看看 Google Play 音乐锁,这显然不是锁屏小部件。

在此处输入图像描述

4

3 回答 3

15

你检查过 RemoteControlClient 吗?即使应用程序处于锁定模式,它也可用于 Android 音乐遥控器。(与您附加的图像相同)

请检查RemoteControlClient

只需在您接收歌曲曲目的播放、暂停、下一个和上一个命令操作时调用以下方法。

  private void lockScreenControls() {

    // Use the media button APIs (if available) to register ourselves for media button
    // events

    MediaButtonHelper.registerMediaButtonEventReceiverCompat(mAudioManager, mMediaButtonReceiverComponent);
    // Use the remote control APIs (if available) to set the playback state
    if (mRemoteControlClientCompat == null) {
        Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        intent.setComponent(mMediaButtonReceiverComponent);
        mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this /*context*/,0 /*requestCode, ignored*/, intent /*intent*/, 0 /*flags*/));
        RemoteControlHelper.registerRemoteControlClient(mAudioManager,mRemoteControlClientCompat);
    }
    mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    mRemoteControlClientCompat.setTransportControlFlags(
            RemoteControlClient.FLAG_KEY_MEDIA_PAUSE |
            RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS |
            RemoteControlClient.FLAG_KEY_MEDIA_NEXT |
            RemoteControlClient.FLAG_KEY_MEDIA_STOP);

  //update remote controls
    mRemoteControlClientCompat.editMetadata(true)
            .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, "NombreArtista")
            .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, "Titulo Album")
            .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, nombreCancion)
            //.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION,playingItem.getDuration())
                    // TODO: fetch real item artwork
            .putBitmap(RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK, getAlbumArt())
            .apply();
    }
}

MediaButtonHelper 类

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.ComponentName;
import android.media.AudioManager;
import android.util.Log;

/**
 * Class that assists with handling new media button APIs available in API level 8.
 */
public class MediaButtonHelper {
    // Backwards compatibility code (methods available as of API Level 8)
    private static final String TAG = "MediaButtonHelper";

    static {
        initializeStaticCompatMethods();
    }

    static Method sMethodRegisterMediaButtonEventReceiver;
    static Method sMethodUnregisterMediaButtonEventReceiver;

    static void initializeStaticCompatMethods() {
        try {
            sMethodRegisterMediaButtonEventReceiver = AudioManager.class.getMethod(
                    "registerMediaButtonEventReceiver",
                    new Class[] { ComponentName.class });
            sMethodUnregisterMediaButtonEventReceiver = AudioManager.class.getMethod(
                    "unregisterMediaButtonEventReceiver",
                    new Class[] { ComponentName.class });
        } catch (NoSuchMethodException e) {
            // Silently fail when running on an OS before API level 8.
        }
    }

    public static void registerMediaButtonEventReceiverCompat(AudioManager audioManager,
            ComponentName receiver) {
        if (sMethodRegisterMediaButtonEventReceiver == null)
            return;

        try {
            sMethodRegisterMediaButtonEventReceiver.invoke(audioManager, receiver);
        } catch (InvocationTargetException e) {
            // Unpack original exception when possible
            Throwable cause = e.getCause();
            if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            } else if (cause instanceof Error) {
                throw (Error) cause;
            } else {
                // Unexpected checked exception; wrap and re-throw
                throw new RuntimeException(e);
            }
        } catch (IllegalAccessException e) {
            Log.e(TAG, "IllegalAccessException invoking registerMediaButtonEventReceiver.");
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unused")
    public static void unregisterMediaButtonEventReceiverCompat(AudioManager audioManager,
            ComponentName receiver) {
        if (sMethodUnregisterMediaButtonEventReceiver == null)
            return;

        try {
            sMethodUnregisterMediaButtonEventReceiver.invoke(audioManager, receiver);
        } catch (InvocationTargetException e) {
            // Unpack original exception when possible
            Throwable cause = e.getCause();
            if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            } else if (cause instanceof Error) {
                throw (Error) cause;
            } else {
                // Unexpected checked exception; wrap and re-throw
                throw new RuntimeException(e);
            }
        } catch (IllegalAccessException e) {
            Log.e(TAG, "IllegalAccessException invoking unregisterMediaButtonEventReceiver.");
            e.printStackTrace();
        }
    }
}

还请检查这个开发者应用程序,了解如何集成 RemoteControlClient:随机音乐播放器但是 RemoteControlClient 的 UI 根据设备延迟,您无法将其 UI 更新为您自己的,但您可以控制显示和显示音乐的组件和控制应用程序。

更新

上面提到的类现在已被弃用。因此,请与Media Session联系并进行相应更新。

于 2014-09-02T06:36:56.837 回答
2

RemoteControlClient 是您正在寻找的东西,但现在它已被弃用并已被 MediaSession 取代。

文档在这里: https ://developer.android.com/reference/android/media/session/MediaSession.html

于 2016-08-19T20:37:34.110 回答
0

如果您的媒体控件在通知中运行良好并且媒体控件已显示在锁定屏幕上但无法正常工作,那么您可以按照此代码开始在锁定屏幕上使用媒体控件(播放、暂停、下一个、上一个):-

私有 MediaSessionCompat mMediaSessionCompat;
私人音频管理器音频管理器;

在您的服务类的 onCreate 中调用以下方法:-

private void RegisterRemoteClient() {
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    assert audioManager != null;
    audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    ComponentName mRemoteControlResponder = new ComponentName(getPackageName(),
            NotificationBroadcast.class.getName());


    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(mRemoteControlResponder);

    mMediaSessionCompat = new MediaSessionCompat(getApplication(), "JairSession", mRemoteControlResponder, null);
    mMediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    PlaybackStateCompat playbackStateCompat = new PlaybackStateCompat.Builder()
            .setActions(
                    PlaybackStateCompat.ACTION_SEEK_TO |
                            PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                            PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                            PlaybackStateCompat.ACTION_PLAY |
                            PlaybackStateCompat.ACTION_PAUSE |
                            PlaybackStateCompat.ACTION_STOP
            )
            .build();
    mMediaSessionCompat.setPlaybackState(playbackStateCompat);
    mMediaSessionCompat.setCallback(mMediaSessionCallback);
    mMediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
}

现在在服务类中创建这个回调:-

private MediaSessionCompat.Callback mMediaSessionCallback = new MediaSessionCompat.Callback() {

    @Override
    public void onPlay() {
        super.onPlay();
        mMediaSessionCompat.setActive(true);
        Log.d("dvmMediaSessionCompat ","onPlay");
        if (PlayerConstants.SONG_PAUSED){
            Controls.playControl(getApplicationContext());
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("dvmMediaSessionCompat ","onPause");
        if (!PlayerConstants.SONG_PAUSED){
            Controls.pauseControl(getApplicationContext());
        }
        else
            Controls.playControl(getApplicationContext());


    }
    @Override
    public void onSkipToQueueItem(long queueId) {

    }

    @Override
    public void onSeekTo(long position) {

    }

    @Override
    public void onStop() {
        Log.d("dvmMediaSessionCompat ","onStop");
    }

    @Override
    public void onSkipToNext() {
        Log.d("dvmMediaSessionCompat ","onSkipToNext");
        Controls.nextControl(getApplicationContext());
    }

    @Override
    public void onSkipToPrevious() {
        Log.d("dvmMediaSessionCompat ","onSkipToPrevious");
        Controls.previousControl(getApplicationContext());
    }

};

这就是我所做的并且控件开始工作,我只是忘记在我的MediaSessionCompat实例上添加setCallback,这就是我的控件不起作用的原因。但现在它的工作完美

注意 - Controls.nextControl(context) 这是我添加了我自己的功能来切换歌曲的方法,你可以用你的逻辑替换它。

于 2022-03-01T07:52:05.993 回答