锁定屏幕上的媒体控制,如Android中的Google Play音乐?

前端之家收集整理的这篇文章主要介绍了锁定屏幕上的媒体控制,如Android中的Google Play音乐?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经通过了Lock-Widget Widget文档,我实现了这一点,但这不是主锁定窗口中的什么.我搜索解决方案,提供媒体控制在主要锁屏窗口(在果冻豆及以上),如Google Play音乐应用程序.

看看Google Play音乐锁显然不是锁屏小部件.

解决方法

你检查过RemoteControlClient吗?即使应用程序处于锁定模式也可用于 Android Music Remote控件(与您附加的图像相同)

请检查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,"IllegalAccessException invoking unregisterMediaButtonEventReceiver.");
            e.printStackTrace();
        }
    }
}

还请查看此开发人员应用程序,以了解如何集成RemoteControlClient:Random Music Player但是,根据您无法将其UI更新到您自己的设备的RemoteControlClient deffer的UI,但您可以控制显示显示音乐应用程序的组件和控件.

希望这将有助于您.如果您不了解如何为音乐应用实现锁定屏幕控制,请通知我.

享受编码.

猜你在找的Android相关文章