Android Studio 最小化/阻止手机后保持应用程序Android运行[duplicate]

jvlzgdj9  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(207)

此问题在此处已有答案

Android - implementing startForeground for a service?(11个答案)
8个月前关闭。
我已经创建了一个简单的应用程序(以下链接How to Play Sound On Button Click in Android Studio Java 2021),一切正常,但是,我需要这个应用程序仍然可以播放的声音后,最小化然后或阻止手机(是一个白色噪声应用程序从帮助我的宝贝女儿睡眠,我需要保持播放一整夜).
是否有任何方法或设置可以使声音在最小化或阻止应用程序后不停止?
谢谢你们所有能帮助我和我的孩子的人:)

fafcakar

fafcakar1#

如果您添加了在后台运行应用程序的功能,就可以解决您的问题。您可以查看的资源:

基本示例:
AndroidManifest.xml

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
*
*
*
*
        <service android:name=".ui.main.AudioPlayerService"/>

 </application>

AudioPlayerService

private const val PLAYBACK_CHANNEL_ID = "blabla"
        private const val PLAYBACK_NOTIFICATION_ID = 1
        
        class AudioPlayerService : Service() {
        
            private var player: SimpleExoPlayer? = null
            private var playerNotificationManager: PlayerNotificationManager? = null
            private val mediaItem: MediaItem = MediaItem.fromUri(ApiInterface.Channel24LiveURL)
        
            /** Classes to be connected to the service with the 
             service
             *reference providing the link between
             */
            private val mBinder = AudioServiceBinder()
        
            /**
* Data that the service will share to other classes.             */
            inner class AudioServiceBinder : Binder() {
                val service
                    get() = this@AudioPlayerService
        
                val player
                    get() = this@AudioPlayerService.player
            }
        
            override fun onBind(intent: Intent?): IBinder? {
                return mBinder
            }
        
            /**
             * Service start part
             */
            override fun onCreate() {
                super.onCreate()
        
                // init player
                player = SimpleExoPlayer.Builder(this)
                        .build().apply {
                            setMediaItem(mediaItem)
                            playWhenReady = true
                            prepare()
                        }
        
                playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
                        applicationContext,
                        PLAYBACK_CHANNEL_ID,
                        R.string.playback_channel_name,
                        R.string.playback_channel_desc,
                        PLAYBACK_NOTIFICATION_ID,
                        object : PlayerNotificationManager.MediaDescriptionAdapter {
                            override fun getCurrentContentTitle(player: Player): CharSequence {
                                return getString(R.string.def_playback_title)
                            }
        
                            override fun createCurrentContentIntent(player: Player): PendingIntent? {
                                return PendingIntent.getActivity(
                                        applicationContext,
                                        0,
                                        Intent(applicationContext, MainActivity::class.java),
                                        PendingIntent.FLAG_CANCEL_CURRENT
                                )
                            }
        
                            override fun getCurrentContentText(player: Player): CharSequence? {
                                return null
                            }
        
                            override fun getCurrentLargeIcon(
                                    player: Player,
                                    callback: PlayerNotificationManager.BitmapCallback
                            ): Bitmap? {
                                return getBitmapFromVectorDrawable(applicationContext, R.mipmap.ic_launcher)
                            }
                        }, object : PlayerNotificationManager.NotificationListener {
                    override fun onNotificationCancelled(notificationId: Int, dismissedByUser: Boolean) {
                        stopSelf()
                    }
        
                    override fun onNotificationPosted(notificationId: Int, notification: Notification, ongoing: Boolean) {
                        if (ongoing) {
                            // Make sure the service will not get destroyed while playing media.
                            startForeground(notificationId, notification)
                        } else {
                            // Make notification cancellable.
                            stopForeground(false)
                        }
                    }
                }
                ).apply {
                    //   previous and next actions.
                    setUseNavigationActions(true)
                    setPlayer(player)
                }
            }
        
            @MainThread
            private fun getBitmapFromVectorDrawable(
                    context: Context,
                    @Suppress("SameParameterValue") @DrawableRes drawableId: Int
            ): Bitmap? {
                return ContextCompat.getDrawable(context, drawableId)?.let {
                    val drawable = DrawableCompat.wrap(it).mutate()
        
                    val bitmap = Bitmap.createBitmap(
                            drawable.intrinsicWidth,
                            drawable.intrinsicHeight,
                            Bitmap.Config.ARGB_8888
                    )
                    val canvas = Canvas(bitmap)
                    drawable.setBounds(0, 0, canvas.width, canvas.height)
                    drawable.draw(canvas)
        
                    bitmap
                }
            }
        
            @MainThread
            fun changePlayerVolume(view: ImageView) {
                player?.let {
                    if (it.volume == 1.0f) {
                        it.volume = 0.0f
                        view.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_volume_off))
                    } else {
                        it.volume = 1.0f
                        view.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_volume_up))
                    }
                }
            }
        
            @MainThread
            fun jumpLiveStream() {
                player?.let {
                    it.setMediaItem(mediaItem)
                    it.playWhenReady = true
                }
            }
        
            /**
* Triggered when the app is closed.             */
            override fun onTaskRemoved(rootIntent: Intent?) {// Stop the service when the user closes the application.l
                releasePlayer()
                stopSelf()
                super.onTaskRemoved(rootIntent)
            }
        
            override fun onDestroy() {
                releasePlayer()
                stopSelf()
                super.onDestroy()
            }
        
            /**
* delete player and playerNotificationManager             */
            private fun releasePlayer() {
                player?.let {
                    it.release()
                    player = null
                }
                playerNotificationManager?.let {
                    it.setPlayer(null)
                    playerNotificationManager = null
                }
            }
        }

祝你好运!

相关问题