android - 在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)
解决方案
你的代码是正确的!正如您所提到的,您已经完成了音乐播放器,所以我不编写 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)
}
}
}
希望对您有所帮助,我刚刚测试过它并且它有效!
推荐阅读
- android - 离子/电容器 - PushNotiifcations - Android - “通知” + “数据” 消息
- typescript - 找不到打字稿编译器
- amazon-web-services - 使用 ssh 克隆 Git
- c# - 当我将它们拖到 Unity 时,动画不起作用
- google-play - Playstore:无法上传,请重试
- python - 为什么当 4 个球到达底部而不是 3 个时我的游戏会暂停?
- sql - 如何从包含 json 查询的表中构建多活动卫星表?
- html - 在 nth-child 的 CSS 选择器中添加异常
- angular - 即使我在 entryComponents 数组中添加了组件,也会出现错误,因为找不到名称“ProductFormComponent”
- c++ - C++ 和 Rust 中“引用”的概念是否不同?