3

我对 MediaControllerCompat 和 MediaSessionCompat 的导入

import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;

private MediaSessionCompat mMediaSessionCompat;
private MediaControllerCompat.TransportControls mTransportControls;

我最近将我的项目迁移到了 Android X,但是这些导入在迁移时并没有改变,而是在我的 gradle 的依赖项中添加了它。

implementation 'androidx.legacy:legacy-support-v4:1.0.0'

现在我想知道为什么他们没有更改为 Android X,我搜索了MediaControllerCompat 和 MediaSessionCompat的 android x 依赖项,但我找不到它们。

4

2 回答 2

2

在您的 gradle.properties 中,您可以看到 android.enableJetifier=true 当我们在 androidx 中迁移项目时处理所有第三方库,一个属性将处理所有迁移内容。

现在关于您的问题,我在开发 mp3 音频应用程序时使用了这两个类,但是在迁移到 Androidx 之后,它仍然没有添加任何额外的库。

implementation 'androidx.legacy:legacy-support-v4:1.0.0'

我的课 :

package com.broadcast.audioplayerkotlin

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatSeekBar
import android.support.v4.media.session.MediaControllerCompat
import android.support.v4.media.MediaMetadataCompat

import android.widget.SeekBar

import android.support.v4.media.session.PlaybackStateCompat

import android.util.Log


class MediaSeekBar : AppCompatSeekBar {
private val TAG = "MediaSeekBar"
private var mMediaController: MediaControllerCompat? = null
private var mControllerCallback: ControllerCallback? = null
private var mIsTracking = false
private val mOnSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener {
    override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {}

    override fun onStartTrackingTouch(seekBar: SeekBar) {
        mIsTracking = true
    }

    override fun onStopTrackingTouch(seekBar: SeekBar) {
        mMediaController?.transportControls?.seekTo(progress.toLong())
        mIsTracking = false
    }
}

constructor(ctx: Context) : super(ctx)
constructor(code: Context, attrs: AttributeSet?) : super(code, attrs) {

    super.setOnSeekBarChangeListener(mOnSeekBarChangeListener)
}

constructor(code: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(code, attrs) {
}

override fun setOnSeekBarChangeListener(l: SeekBar.OnSeekBarChangeListener) {
    // Prohibit adding seek listeners to this subclass.
    throw UnsupportedOperationException("Cannot add listeners to a MediaSeekBar")
}

fun setMediaController(mediaController: MediaControllerCompat?) {
    if (mediaController != null) {
        mControllerCallback = ControllerCallback()
        mediaController.registerCallback(mControllerCallback!!)
    } else if (mMediaController != null) {
        mMediaController!!.unregisterCallback(mControllerCallback!!)
        mControllerCallback = null
    }
    mMediaController = mediaController
}

fun disconnectController() {
    if (mMediaController != null) {
        mMediaController!!.unregisterCallback(mControllerCallback!!)
        mControllerCallback = null
        mMediaController = null
    }
}

private inner class ControllerCallback : MediaControllerCompat.Callback() {

    override fun onSessionDestroyed() {
        super.onSessionDestroyed()
    }


    override fun onPlaybackStateChanged(state: PlaybackStateCompat?) {
        super.onPlaybackStateChanged(state)
        Log.d(TAG, "onPlaybackStateChanged: CALLED: playback state: " + state!!)

        val progress = state?.position?.toInt() ?: 0
        setProgress(progress)

    }

    override fun onMetadataChanged(metadata: MediaMetadataCompat?) {
        super.onMetadataChanged(metadata)
    }

    }
}

摇篮依赖:

 dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

//RecyclerView
implementation 'com.android.support:recyclerview-v7:28.0.0'


def glideVersion = "4.8.0"
implementation "com.github.bumptech.glide:glide:$glideVersion"
annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion"

// ExoPlayer
implementation 'com.google.android.exoplayer:exoplayer:2.8.4' // 2.9.0 has min sdk 26

// Firestore
implementation 'com.google.firebase:firebase-firestore:17.1.2'
}

只是好奇为什么要添加额外的库。

于 2019-12-27T08:30:39.717 回答
0

这是有意的行为:在进程之间传递的类(例如MediaSessionCompat.TokenextendsParcelableMediaControllerCompat.Callbackimplements IBinder.DeathRecipient)不能被移动到新的包中,而不会破坏非 AndroidX 项目和 AndroidX 项目之间的交叉兼容性。因此,这些类必须留在原始包中。

这可以通过查看支持库类映射来确认,这表明这些特定类维护了它们现有的包结构。

于 2019-12-27T05:55:46.990 回答