首页 > 解决方案 > 在kotlin中接听电话时如何停止媒体播放器

问题描述

我已经完成了我的音乐播放器应用程序,但是当我接到电话或 Whatsapp 等电话时,我不知道如何控制媒体播放器。你能帮帮我吗?

        val phoneStateListener: PhoneStateListener = object : PhoneStateListener() {
        override fun onCallStateChanged(state: Int, incomingNumber: String) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                //Incoming call: Pause music
            } else if (state == TelephonyManager.CALL_STATE_IDLE) {
                //Not in call: Play music
            } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                //A call is dialing, active or on hold
            }
            super.onCallStateChanged(state, incomingNumber)
        }
    }
    val mgr = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
    mgr?.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE)

标签: androidkotlin

解决方案


你的代码是正确的!正如您所提到的,您已经完成了音乐播放器,所以我不编写 MediaPlayer 代码。我认为你有类似的东西:

mp = MediaPlayer()

现在,如果你想在 Activity 中编码:

import android.os.Bundle
import android.telephony.PhoneStateListener
import android.telephony.TelephonyManager
import androidx.appcompat.app.AppCompatActivity

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

    val phoneStateListener: PhoneStateListener = object : PhoneStateListener() {
        override fun onCallStateChanged(state: Int, incomingNumber: String) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                //Incoming call: Pause music
                mp!!.pause()
            } else if (state == TelephonyManager.CALL_STATE_IDLE) {
                //Not in call: Play music
                if(mp != null){
                   mp!!.start()
                }
            } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                //A call is dialing, active or on hold
                mp!!.pause()
            }
            super.onCallStateChanged(state, incomingNumber)
        }
    }
    val mgr = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
    mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE)
   }
}

如果你想在 Fragment 中编码:

import android.content.Context
import android.os.Bundle
import android.telephony.PhoneStateListener
import android.telephony.TelephonyManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class HomeFragment : Fragment() {
 override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_home, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val mgr = activity?.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE)
}

val phoneStateListener: PhoneStateListener = object : PhoneStateListener() {
    override fun onCallStateChanged(state: Int, incomingNumber: String) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            //Incoming call: Pause music
            mp!!.pause()
        } else if (state == TelephonyManager.CALL_STATE_IDLE) {
            //Not in call: Play music
            if(mp != null){
                mp!!.start()
            }
        } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
            //A call is dialing, active or on hold
            mp!!.pause()
        }
        super.onCallStateChanged(state, incomingNumber)
    }
  }
  }

希望对您有所帮助,我刚刚测试过它并且它有效!


推荐阅读