1

音楽を再生するアプリがあります。アプリの読み込みが開始されると、音楽の再生が開始されます。選曲はランダムです。フラグメントでは、曲の名前が表示されます。音楽は流れていますが、曲名が表示されません。私は何を間違っていますか?(これは MainActivity ではなく、ロード画面です) ここに私のサービスがあります:

class MediaService : Service() {
    private val binder = MediaBinder()
    private var list = arrayOf(
        R.raw.1, R.raw.2, R.raw.3, R.raw.4)
    private lateinit var mediaPlayer: MediaPlayer
    private val mGenerator = Random()

    val randomNumber: Int
        get() = list.get(mGenerator.nextInt(4))

    override fun onCreate() {
        super.onCreate()
        mediaPlayer = MediaPlayer.create(applicationContext, randomNumber)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        mediaPlayer.start()
        Log.d(LOGCAT, "Media Player started!")
        if (!mediaPlayer.isLooping) {
            Log.d(LOGCAT, "Problem in Playing Audio")
        }
        return START_STICKY
    }

    override fun onDestroy() {
        mediaPlayer.stop()
        mediaPlayer.release()
    }

    inner class MediaBinder : Binder() {
        // Return this instance of LocalService so clients can call public methods
        fun getService(): MediaService = this@MediaService
    }

    override fun onBind(intent: Intent): IBinder {
        return binder
    }
}

ここに私の活動があります:

class MyActivity : AppCompatActivity() {

    private lateinit var mService: MediaService
    private var mBound: Boolean = false
    private val connection = object : ServiceConnection {

        override fun onServiceConnected(className: ComponentName, service: IBinder) {
            val binder = service as MediaService.MediaBinder
            mService = binder.getService()
            mBound = true
        }

        override fun onServiceDisconnected(arg0: ComponentName) {
            mBound = false
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(view)

        var fragment = LayoutProcessingFragment()
        if (mBound) {
            when (mService.randomNumber) {
                R.1
                -> {
                    fragment.binding.textGroupName.text = "1"
                }
                R.raw.2
                -> {
                    fragment.binding.textGroupName.text = "2"
                }
                R.raw.3
                -> {
                    fragment.binding.textGroupName.text = "3"
                }
                R.raw.4
                -> {
                    fragment.binding.textGroupName.text = "4"
                }
            }

        }
        playAudio(view)
 }

    fun playAudio(view: View?) {
        val objIntent = Intent(this, MediaService::class.java)
        startService(objIntent)
    }

    override fun onStart() {
        super.onStart()
        // Bind to LocalService
        Intent(this, MediaService::class.java).also { intent ->
            bindService(intent, connection, Context.BIND_AUTO_CREATE)
        }
    }

    override fun onStop() {
        super.onStop()
        unbindService(connection)
        mBound = false
    }
  }
4

0 に答える 0