我正在尝试实现Media3 MediaSessionService并播放音乐。但是服务没有启动,我没有看到通知,大约10秒后应用程序崩溃。我使用的是三星A70 Android 11。
MyMusicService.kt
@UnstableApi
class MyMusicService : MediaSessionService(), MediaSession.Callback {
lateinit var exoPlayer: ExoPlayer
lateinit var mediaSession: MediaSession
@SuppressLint("UnsafeOptInUsageError")
override fun onCreate() {
super.onCreate()
Log.d("tag","----------------------------- MediaSessionService, onCreate")
exoPlayer = ExoPlayer.Builder(this).build()
mediaSession = MediaSession.Builder(this, exoPlayer).setCallback(this).build()
}
@SuppressLint("UnsafeOptInUsageError")
private fun updateNotification(session: MediaSession): MediaNotification {
val notify = NotificationCompat.Builder(this,"Radio")
.setSmallIcon(R.drawable.ic_launcher_foreground)
// This is globally changed every time when
// I add a new MediaItem from background service
.setContentTitle("title")
.setContentText("artist")
.setStyle(MediaStyleNotificationHelper.MediaStyle(session))
.build()
return MediaNotification(1, notify)
}
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession = mediaSession
override fun onAddMediaItems(mediaSession: MediaSession, controller: MediaSession.ControllerInfo, mediaItems: MutableList<MediaItem>): ListenableFuture<MutableList<MediaItem>> {
val updatedMediaItems = mediaItems.map { it.buildUpon().setUri(it.mediaId).build() }.toMutableList()
return Futures.immediateFuture(updatedMediaItems)
}
override fun onDestroy() {
Log.d("tag","----------------------------- MediaSessionService, onDestroy")
exoPlayer.stop()
exoPlayer.release()
mediaSession.release()
super.onDestroy()
}
}
MainActivity.kt
@UnstableApi class MainActivity : AppCompatActivity() {
lateinit var controller: MediaController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onStart() {
super.onStart()
val sessionToken = SessionToken(this, ComponentName(this, MyMusicService::class.java))
val controllerFuture = MediaController.Builder(this, sessionToken)
.buildAsync()
controllerFuture.addListener({
findViewById<PlayerControlView>(R.id.player).player = controllerFuture.get()
},
MoreExecutors.directExecutor())
controller = controllerFuture.get()
initController()
controller.apply {
setMediaItem(
MediaItem.Builder()
.setMediaId("http://listen.vo.llnwd.net/g3/prvw/9/5/1/1/7/2489571159.mp3")
.build()
)
prepare()
play()
}
}
private fun initController() {
//controller.playWhenReady = true
controller.addListener(object : Player.Listener {
override fun onMediaMetadataChanged(mediaMetadata: MediaMetadata) {
super.onMediaMetadataChanged(mediaMetadata)
Log.d("tag","onMediaMetadataChanged=$mediaMetadata")
}
override fun onIsPlayingChanged(isPlaying: Boolean) {
super.onIsPlayingChanged(isPlaying)
Log.d("tag","onIsPlayingChanged=$isPlaying")
}
override fun onPlaybackStateChanged(playbackState: Int) {
super.onPlaybackStateChanged(playbackState)
Log.d("tag","onPlaybackStateChanged=?}")
}
override fun onPlayerError(error: PlaybackException) {
super.onPlayerError(error)
Log.d("tag","onPlayerError=${error.stackTraceToString()}")
}
override fun onPlayerErrorChanged(error: PlaybackException?) {
super.onPlayerErrorChanged(error)
Log.d("tag","onPlayerErrorChanged=${error?.stackTraceToString()}")
}
})
Log.d("tag","start=}")
Log.d("tag","COMMAND_PREPARE=${controller.isCommandAvailable(COMMAND_PREPARE)}")
Log.d("tag","COMMAND_SET_MEDIA_ITEM=${controller.isCommandAvailable(COMMAND_SET_MEDIA_ITEM)}")
Log.d("tag","COMMAND_PLAY_PAUSE=${controller.isCommandAvailable(COMMAND_PLAY_PAUSE)}")
}
}
舱单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".chatgptservice.App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.TestApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyMusicService"
android:foregroundServiceType="mediaPlayback"
android:exported="true"
android:enabled="true"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService"/>
<action android:name="android.media.browse.MediaBrowserService"/>
</intent-filter>
</service>
</application>
</manifest>
库
implementation "androidx.media3:media3-exoplayer:1.0.0-rc02"
implementation "androidx.media3:media3-ui:1.0.0-rc02"
implementation "androidx.media3:media3-exoplayer-dash:1.0.0-rc02"
implementation "androidx.media3:media3-session:1.0.0-rc02"
implementation "androidx.media3:media3-common:1.0.0-rc02"
对数表
17:42:01.607 I Build Config : S P 10.0.7 AArch64
17:42:01.607 I Driver Path : /vendor/lib64/egl/libGLESv2_adreno.so
17:42:01.613 I PFP: 0x016ee190, ME: 0x00000000
17:42:01.663 D Checking for metadata for AppLocalesMetadataHolderService : Service not found
17:42:01.702 I [INFO] isPopOver=false, config=true
17:42:01.702 I updateCaptionType >> DecorView@db543ef[], isFloating=false, isApplication=true, hasWindowDecorCaption=false, hasWindowControllerCallback=true
17:42:01.702 D setCaptionType = 0, this = DecorView@db543ef[]
17:42:01.761 W Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
17:42:01.762 W Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
17:42:03.151 W A resource failed to call close.
17:42:03.152 W A resource failed to call close.
17:42:32.581 I Thread[5,tid=23284,WaitingInMainSignalCatcherLoop,Thread*=0xb4000078297f0570,peer=0x12c42330,"Signal Catcher"]: reacting to signal 3
17:42:32.581 I
17:42:32.696 I Wrote stack traces to tombstoned
17:42:43.133 E ANR in com.example.testapp
PID: 20571
Reason: executing service com.example.testapp/.MyMusicService
Load: 0.0 / 0.0 / 0.0
------ Current CPU Core Info ------
- offline :
- online : 0-7
- AP Temp = 381
0 1 2 3 4 5 6 7
------------------------------------------------------------------------------------------------------------------
scaling_cur_freq 576000 576000 576000 576000 576000 576000 1516800 1516800
scaling_governor schedutil schedutil schedutil schedutil schedutil schedutil schedutil schedutil
scaling_max_freq 1708800 1708800 1708800 1708800 1708800 1708800 2016000 2016000
------------------------------------------------------------------------------------------------------------------
----- Output from /proc/pressure/memory -----
some avg10=0.03 avg60=0.25 avg300=0.44 total=2639634764
full avg10=0.01 avg60=0.11 avg300=0.14 total=1360303511
----- End output from /proc/pressure/memory -----
CPU usage from 0ms to 11068ms later (2023-03-05 17:42:31.989 to 2023-03-05 17:42:43.057):
27% 32320/com.zhiliaoapp.musically: 15% user + 12% kernel / faults: 5592 minor 33 major
23% 1038/system_server: 9.8% user + 14% kernel / faults: 7237 minor 26 major
7.2% 1163/media.codec: 4.1% user + 3.1% kernel / faults: 41149 minor 2 major
0.4% 1190/media.swcodec: 0.1% user + 0.2% kernel / faults: 23989 minor 1 major
5.8% 851/surfaceflinger: 1.7% user + 4.1% kernel / faults: 304 minor
5.4% 21899/org.telegram.messenger: 3.1% user + 2.2% kernel / faults: 3211 minor 8 major
4.6% 8551/transport: 0.8% user + 3.7% kernel / faults: 412 minor
4.1% 2035/com.android.phone: 2.2% user + 1.8% kernel / faults: 4015 minor 165 major
3.7% 14760/kworker/u16:8: 0% user + 3.7% kernel / faults: 15 minor
3.4% 6678/adbd: 1.1% user + 2.2% kernel / faults: 434 minor
1.3% 142/kswapd0: 0% user + 1.3% kernel
0.1% 1111/media.extractor: 0% user + 0% kernel / faults: 6408 minor
3% 20149/kworker/u16:0: 0% user + 3% kernel / faults: 6 minor
2.5% 2053/com.android.systemui: 1% user + 1.4% kernel / faults: 3179 minor 38 major
2.4% 5987/kworker/u16:11: 0% user + 2.4% kernel / faults: 6 minor
2.4% 8222/kworker/u16:1: 0% user + 2.4% kernel / faults: 4 minor
0% 802/media.hwcodec: 0% user + 0% kernel / faults: 2775 minor 2 major
1.9% 9661/com.pomodrone.app: 1.4% user + 0.5% kernel / faults: 20 minor
1.8% 12649/kworker/u16:5: 0% user + 1.8% kernel / faults: 7 minor
1.7% 16441/ru.yandex.translate: 1.6% user + 0% kernel / faults: 387 minor
1.6% 3135/irq/114-90b6300: 0% user + 1.6% kernel
0.6% 16443/com.google.android.apps.tasks: 0.6% user + 0% kernel / faults: 429 minor
1.4% 10589/logd: 0.2% user + 1.1% kernel / faults: 18 minor
0.4% 30094/android.process.acore: 0.2% user + 0.2% kernel / faults: 548 minor
0.3% 20571/com.example.testapp: 0.2% user + 0.1% kernel / faults: 3215 minor
0.9% 32730/com.android.chrome: 0.9% user + 0% kernel / faults: 560 minor
0.9% 4598/com.sec.android.sdhms: 0.2% user + 0.6% kernel / faults: 73 minor
0.9% 26391/android.process.acore: 0.8% user + 0% kernel / faults: 507 minor
0.8% 22628/com.android.vending: 0.5% user + 0.2% kernel / faults: 395 minor 1 major
0.6% 9/rcu_preempt: 0% user + 0.6% kernel
0.2% 2866/com.android.bluetooth: 0% user + 0.2% kernel / faults: 176 minor
0.6% 25771/kworker/u17:2: 0% user + 0.6% kernel
0.2% 560/lmkd: 0% user + 0.2% kernel
0.4% 10/rcu_sched: 0% user + 0.4% kernel
0.4% 22441/com.instagram.android: 0.2% user + 0.1% kernel / faults: 20 minor
0.4% 27059/kworker/u17:0: 0% user + 0.4% kernel
0.3% 12/rcuop/0: 0% user + 0.3% kernel
0.3% 62/rcuop/6: 0% user + 0.3% kernel
0.3% 70/rcuop/7: 0% user + 0.3% kernel
0% 679/tombstoned: 0% user + 0% kernel
0.1% 835/audioserver: 0% user + 0% kernel / faults: 74 minor
0.3% 3674/com.samsung.SMT: 0.3% user + 0% kernel / faults: 314 minor
我试着用rc01版本做这个。但是没有得到改变。试着在android 12模拟器上启动,得到了同样的问题。
1条答案
按热度按时间but5z9lq1#
首先,添加这个权限,因为media3内部需要它来进行网络播放:
其次,你把代码放错地方了,你的控制器在未来成功之前是不存在的。以下是你的onStart的更新代码:
第三,您根本不应该使用'ExoPlayer'对象类型,Media3依赖于抽象的高级对象类型'Player',因此,在您的服务类中,请改用以下全局变量:
最后,最好使用一个可以为空的mediasession变量,如下所示:
这应该涵盖了代码中的所有错误。希望你能让它工作起来:)