【问题标题】:Keep App Android Running after minimize / block phone [duplicate]最小化/阻止手机后保持应用程序Android运行[重复]
【发布时间】:2022-11-10 15:19:17
【问题描述】:

我创建了一个简单的应用程序(点击此链接How to Play Sound On Button Click in Android Studio Java 2021),一切正常,但是,我需要这个应用程序在最小化或阻止电话后仍然可以播放声音(是一个来自睡眠帮助的白噪声应用程序我的宝贝女儿,我需要它继续玩一整晚)。

有没有什么方法或设置可以让声音在最小化或阻止应用程序后不停止?

感谢所有可以帮助我和我的宝宝的人:)

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

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

    基本示例:

    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
                    }
                }
            }
    

    祝你好运!

    【讨论】:

    • @EddyFrancisco 您需要在 AndroidManifest.xml 中声明服务,然后创建一个扩展服务类的类。您可以轻松访问 Nisa 链接。
    • 我为您添加了一个简单的示例,您需要根据您的应用进行调整。祝你好运@EddyFrancisco
    • 谢谢,我添加了一个简单的例子。 @MicheleVerriello
    • 谢谢@NisaEfendioglu,这个例子对我有用!!
    • 我很高兴,干得好:) @EddyFrancisco
    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 2022-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    相关资源
    最近更新 更多