有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

设置MediaBrowserService和MediaSession时遇到java问题

我想将我的应用程序更新到最新的安卓标准,所以我想实现MediaSession和更进一步的MediaBrowserCompat来控制我的播放

实际上,我得到了这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at 安卓.os.Binder.queryLocalInterface(Binder.java:247) at 安卓.service.media.IMediaBrowserService$Stub.asInterface(IMediaBrowserService.java:31) at 安卓.media.browse.MediaBrowser$MediaServiceConnection.onServiceConnected(MediaBrowser.java:709) at 安卓.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1209) at 安卓.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1226) at 安卓.os.Handler.handleCallback(Handler.java:739) at 安卓.os.Handler.dispatchMessage(Handler.java:95) at 安卓.os.Looper.loop(Looper.java:135) at 安卓.app.ActivityThread.main(ActivityThread.java:5294) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:699)

由该代码引起的:

mMediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, MusicService.class), new MediaBrowserCompat.ConnectionCallback() {

    @Override
    public void onConnectionSuspended() {
        super.onConnectionSuspended();
    }

    @Override
    public void onConnectionFailed() {
        super.onConnectionFailed();
    }

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

        if (MusicPlayer.isPreparing() || MusicPlayer.isPlaying()) {
            Start_Timer();
        }
    }
}, null);

mMediaBrowser.connect();

这是我音乐服务的一部分:

public class MusicService extends MediaBrowserServiceCompat
            implements AudioManager.OnAudioFocusChangeListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {

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

        InitMediaPlayer();

        mMediaSession = new MediaSessionCompat(getApplicationContext(), MusicService.class.getSimpleName());
        mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        iCurrentSongIndex = 0;
        iSeekTo = -1;

        bPlayingFromQueue = false;

        bStarted = bPreparing = bRestartAfterLoss = bControlReceiverRegistered = false;

        mPlayMode = PlayMode.PASS;

        mSongQueue = new ArrayList<>();

        nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        int iRequestResult = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

        if (iRequestResult != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            Toast.makeText(getApplicationContext(), "Couldn't gain the Permission to play music!", Toast.LENGTH_LONG).show();

            stopForeground(true);
            stopSelf();

            System.exit(0);

            return;
        }

        mControlReceiver = new MediaControlReceiver();

        IntentFilter infNotification = new IntentFilter();
        infNotification.addAction(MediaControlReceiver.NOTIFY_PLAYPAUSE);
        infNotification.addAction(MediaControlReceiver.NOTIFY_PREVIOUS);
        infNotification.addAction(MediaControlReceiver.NOTIFY_NEXT);
        infNotification.addAction(MediaControlReceiver.NOTIFY_CANCEL);
        infNotification.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);

        if (!bControlReceiverRegistered) {
            registerReceiver(mControlReceiver, infNotification);

            bControlReceiverRegistered = true;
        }

        mRemoteControlComponent = new ComponentName(this, RemoteControlReceiver.class);

        mAudioManager.registerMediaButtonEventReceiver(mRemoteControlComponent);

        getTheme().applyStyle(RuntimeInfo.getThemeID(), true);
    }

    private Notification Build_Notification() {
        NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);
        nBuilder.setShowWhen(false);

        TypedValue typedValue = new TypedValue();

        getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);

        int iPrimaryColor = typedValue.data;

        nBuilder.setColor(iPrimaryColor);

        Intent notIntent = new Intent(getApplicationContext(), MainActivity.class);

        notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent notOpenOnClick = PendingIntent.getActivity(getApplicationContext(), 0, notIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        mMediaSession.setActive(true);

        ArtworkProvider artworkProvider = new ArtworkProvider(this);

        MediaMetadataCompat.Builder mMetaDataBuilder = new MediaMetadataCompat.Builder();

        Bitmap bCover = artworkProvider.getAlbumArtwork(getCurrentSong().getAlbumID(), 150, 150);

        mMetaDataBuilder
                .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getCurrentSong().getTitle())
                .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getCurrentSong().getAlbum())
                .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getCurrentSong().getArtist());

        String ArtworkOnLockscreenKey = getString(R.string.keyArtworkOnLockscreen);

        boolean bArtworkOnLockscreen = mPreferences.getBoolean(ArtworkOnLockscreenKey, true);

        if (bArtworkOnLockscreen) {
            mMetaDataBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bCover);
        }

        MediaMetadataCompat mMetadata = mMetaDataBuilder.build();

        mMediaSession.setMetadata(mMetadata);

        mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

        nBuilder.setSmallIcon(R.drawable.not_icon)
                .setContentTitle(mMetadata.getString(MediaMetadataCompat.METADATA_KEY_TITLE))
                .setContentText(mMetadata.getString(MediaMetadataCompat.METADATA_KEY_ALBUM) + Constants.Char.SEPERATOR_DOT + mMetadata.getString(MediaMetadataCompat.METADATA_KEY_ARTIST))
                .setLargeIcon(bCover)
                .setContentIntent(notOpenOnClick);

        if (MainActivity.isActive()) {
            nBuilder.setOngoing(true);
        }

        PendingIntent pPrevious = MediaStyleHelper.getActionIntent(getApplicationContext(), KeyEvent.KEYCODE_MEDIA_PREVIOUS);
        PendingIntent pPlayPause = MediaStyleHelper.getActionIntent(getApplicationContext(), KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
        PendingIntent pNext = MediaStyleHelper.getActionIntent(getApplicationContext(), KeyEvent.KEYCODE_MEDIA_NEXT);
        PendingIntent pCancel = MediaStyleHelper.getActionIntent(getApplicationContext(), KeyEvent.KEYCODE_MEDIA_STOP);

        nBuilder.addAction(R.drawable.ic_previous_48dp, MediaControlReceiver.NOTIFY_PREVIOUS, pPrevious);

        if (isPreparing() || isPlaying()) {
            nBuilder.addAction(R.drawable.ic_pause_48dp, MediaControlReceiver.NOTIFY_PLAYPAUSE, pPlayPause);
        }
        else {
            nBuilder.addAction(R.drawable.ic_play_48dp, MediaControlReceiver.NOTIFY_PLAYPAUSE, pPlayPause);
        }

        nBuilder.addAction(R.drawable.ic_next_48dp, MediaControlReceiver.NOTIFY_NEXT, pNext);

        nBuilder.setStyle(new NotificationCompat.MediaStyle()
                .setMediaSession(mMediaSession.getSessionToken())
                .setShowActionsInCompactView(1, 2)
                .setShowCancelButton(true)
                .setCancelButtonIntent(pCancel));

        nBuilder.setDeleteIntent(pCancel);

        mNotification = nBuilder.build();

        return mNotification;
    }
}

我希望你能帮助我。。。 我在youtube上观看了谷歌开发者的视频,但我并不清楚如何做到这一点。。。 任何示例源代码等都将是伟大的

谢谢


共 (0) 个答案