在前台服务中播放Android VideoView的音频

问题描述

我有一个Android应用程序,可以播放VideoView中的视频。当用户按下电话的主页按钮,并且应用程序转到后台时,我想继续播放视频的音轨。我正在使用前台服务。

   class AudioService : Service() {
          private val mybinder = mybinder()
          var mediaPlayer: MediaPlayer? = null

          override fun onBind(intent: Intent?): IBinder? {
                  Log.d("AudioService","audio service bound")
                  return mybinder
          }

          override fun onUnbind(intent: Intent?): Boolean {
              mediaPlayer = null
              stopForeground(true)
              return super.onUnbind(intent)
          }

          fun playAudio(mp: MediaPlayer) {
              val notificationIntent = PendingIntent.getActivity(
                  this,Intent(this,MainActivity::class.java),PendingIntent.FLAG_UPDATE_CURRENT
              )

              val notificationBuilder =
                  createNotificationService("is playing audio",notificationIntent)

              startForeground(
                  10700,notificationBuilder.build()
              )
              // not sure how can I assign VideoView's media player to service media player
              mediaPlayer = mp
          }

          inner class mybinder : Binder() {
                 fun getService(): AudioService {
                        return this@AudioService
                 }
          }
   }

这是我的MainActivity代码

   class MainActivity : AppCompatActivity() {
       private lateinit var mediaPlayer: MediaPlayer

       override fun onCreate(savedInstanceState: Bundle?) {
               super.onCreate(savedInstanceState)
               setContentView(R.layout.activity_main)

               video_view.setonPreparedListener { mp ->
                   mediaPlayer = mp
               }
       }

       override fun onPause() {
               super.onPause()

               mediaPlayer?.let {
                       val intent = Intent(this,AudioService::class.java)
                       bindService(intent,audioServiceConnection,Context.BIND_AUTO_CREATE)
               }
       }

       private val audioServiceConnection = object : ServiceConnection {
               var isBound = false

               override fun onServiceConnected(className: ComponentName,service: IBinder) {
                   val binder = service as AudioService.mybinder
                   audioService = binder.getService()
                   if (video_view.visibility == View.VISIBLE) {
                       audioService?.playAudio(mediaPlayer)
                   }
                   isBound = true
               }

               override fun onServicedisconnected(name: ComponentName) {
                   isBound = false
               }
       }
   }

我可以将VideoView的媒体播放器分配给前台服务媒体播放器,这样在活动暂停(按下电话的主页按钮)时它不会停止播放吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...