如何在 Android Studio Kotlin 中切换到其他页面时停止播放音乐?
How to stop music from playing when user switch to other page in Android Studio Kotlin?
我创建了一个冥想应用程序,用户可以在片段中的回收站视图中select他们想要的class,然后它会在一个新的[=21]中显示详细的内容和步骤=].在新的 activity 中,我实现了一个音频播放功能,用户可以在其中从 firebase 流式传输音频文件。现在的问题是,每当我切换回片段中的回收站视图时,音频文件仍在后台播放。每当我切换到应用内的另一个页面或切换到另一个应用时,如何停止播放音频文件?
这是片段中回收站视图的代码
class ClassFragment : Fragment() {
private lateinit var recyclerView: RecyclerView
private lateinit var classArrayList: ArrayList<Classes>
private lateinit var tempArraylist: ArrayList<Classes>
private lateinit var classAdapter: ClassAdapter
private lateinit var db: FirebaseFirestore
var mediaPlayer: MediaPlayer? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view: View = inflater.inflate(R.layout.fragment_class, container, false)
recyclerView = view.findViewById(R.id.ClassList)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.setHasFixedSize(true)
classArrayList = arrayListOf()
tempArraylist = arrayListOf()
eventChangeListener()
classAdapter = ClassAdapter(requireContext(), classArrayList)
recyclerView.adapter = classAdapter
return view
}
private fun eventChangeListener() {
db = FirebaseFirestore.getInstance()
db.collection("class").addSnapshotListener(object : EventListener<QuerySnapshot> {
override fun onEvent(value: QuerySnapshot?, error: FirebaseFirestoreException?) {
if (error != null) {
Log.e("Firestore error", error.message.toString())
return
}
for (dc: DocumentChange in value?.documentChanges!!) {
if (dc.type == DocumentChange.Type.ADDED) {
classArrayList.add(
dc.document.toObject(
(Classes::class.java)
)
)
}
}
tempArraylist.clear()
tempArraylist.addAll(classArrayList)
classAdapter.notifyDataSetChanged()
}
})
}
}
这是新 activity 页面的代码,当用户 select 来自 recyclerview
时,它将显示内容
class DetailActivity : AppCompatActivity() {
private lateinit var imageViewClass: ImageView
private lateinit var textViewClassName: TextView
private lateinit var textViewClassDes: TextView
private lateinit var textViewClassContent: TextView
private var stop:Boolean = false
var mediaPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
val btnPlay: Button = findViewById(R.id.btnPlay)
val btnStop: Button = findViewById(R.id.btnStop)
imageViewClass = findViewById(R.id.imageViewClassImage)
textViewClassName = findViewById(R.id.textViewClassName)
textViewClassDes = findViewById(R.id.textViewClassDescription)
textViewClassContent = findViewById(R.id.textViewClassContent)
val classImage = intent.getStringExtra("class_image")
val className = intent.getStringExtra("class_name")
val classDes = intent.getStringExtra("class_des")
val classContent = intent.getStringExtra("class_content")
val classAudio = intent.getStringExtra("class_audio")
Glide.with(this)
.load(classImage)
.centerCrop()
.into(imageViewClass)
textViewClassName.text = className
textViewClassDes.text = classDes
textViewClassDes.text = classDes!!.replace("\n", "\n")
textViewClassContent.text = classContent
textViewClassContent.text = classContent!!.replace("\n", "\n")
btnPlay.isEnabled = true
btnStop.isEnabled = false
btnPlay.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this@DetailActivity, Uri.parse(classAudio))
mediaPlayer!!.isLooping = true
mediaPlayer!!.start()
btnStop.isEnabled = true
btnPlay.isEnabled = false
Toast.makeText(applicationContext,"Audio Starts",Toast.LENGTH_SHORT).show()
} else mediaPlayer!!.start()
}
})
mediaPlayer?.setOnCompletionListener {
btnPlay.isEnabled = true
btnStop.isEnabled = false
Toast.makeText(this,"end",Toast.LENGTH_SHORT).show()
}
btnStop.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
if(mediaPlayer!!.isPlaying){
stop = false
mediaPlayer!!.stop()
mediaPlayer!!.reset()
mediaPlayer!!.release()
mediaPlayer = null
btnPlay.isEnabled = true
btnStop.isEnabled = false
Toast.makeText(applicationContext,"Audio Stops",Toast.LENGTH_SHORT).show()
}
}
})
}
}
这是调用 Activity
的 onDestroy
的 classic 案例。我会保持简短,但是 Activity
经历了一个生命周期,当你似乎从第二个 Activity 回到第一个 Activity 时,onDestroy
你的第二个activity被调用来执行销毁函数
所以解决方案,重写 DetailActivity
class 中的 onDestroy
方法并执行类似这样的操作
override fun onDestroy(){
if(mediaPlayer != null){
mediaPlayer!!.stop()
mediaPlayer!!.reset()
mediaPlayer!!.release()
mediaPlayer = null
}
}
您可以详细阅读 Activity Lifecyle
我创建了一个冥想应用程序,用户可以在片段中的回收站视图中select他们想要的class,然后它会在一个新的[=21]中显示详细的内容和步骤=].在新的 activity 中,我实现了一个音频播放功能,用户可以在其中从 firebase 流式传输音频文件。现在的问题是,每当我切换回片段中的回收站视图时,音频文件仍在后台播放。每当我切换到应用内的另一个页面或切换到另一个应用时,如何停止播放音频文件?
这是片段中回收站视图的代码
class ClassFragment : Fragment() {
private lateinit var recyclerView: RecyclerView
private lateinit var classArrayList: ArrayList<Classes>
private lateinit var tempArraylist: ArrayList<Classes>
private lateinit var classAdapter: ClassAdapter
private lateinit var db: FirebaseFirestore
var mediaPlayer: MediaPlayer? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view: View = inflater.inflate(R.layout.fragment_class, container, false)
recyclerView = view.findViewById(R.id.ClassList)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.setHasFixedSize(true)
classArrayList = arrayListOf()
tempArraylist = arrayListOf()
eventChangeListener()
classAdapter = ClassAdapter(requireContext(), classArrayList)
recyclerView.adapter = classAdapter
return view
}
private fun eventChangeListener() {
db = FirebaseFirestore.getInstance()
db.collection("class").addSnapshotListener(object : EventListener<QuerySnapshot> {
override fun onEvent(value: QuerySnapshot?, error: FirebaseFirestoreException?) {
if (error != null) {
Log.e("Firestore error", error.message.toString())
return
}
for (dc: DocumentChange in value?.documentChanges!!) {
if (dc.type == DocumentChange.Type.ADDED) {
classArrayList.add(
dc.document.toObject(
(Classes::class.java)
)
)
}
}
tempArraylist.clear()
tempArraylist.addAll(classArrayList)
classAdapter.notifyDataSetChanged()
}
})
}
}
这是新 activity 页面的代码,当用户 select 来自 recyclerview
时,它将显示内容class DetailActivity : AppCompatActivity() {
private lateinit var imageViewClass: ImageView
private lateinit var textViewClassName: TextView
private lateinit var textViewClassDes: TextView
private lateinit var textViewClassContent: TextView
private var stop:Boolean = false
var mediaPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
val btnPlay: Button = findViewById(R.id.btnPlay)
val btnStop: Button = findViewById(R.id.btnStop)
imageViewClass = findViewById(R.id.imageViewClassImage)
textViewClassName = findViewById(R.id.textViewClassName)
textViewClassDes = findViewById(R.id.textViewClassDescription)
textViewClassContent = findViewById(R.id.textViewClassContent)
val classImage = intent.getStringExtra("class_image")
val className = intent.getStringExtra("class_name")
val classDes = intent.getStringExtra("class_des")
val classContent = intent.getStringExtra("class_content")
val classAudio = intent.getStringExtra("class_audio")
Glide.with(this)
.load(classImage)
.centerCrop()
.into(imageViewClass)
textViewClassName.text = className
textViewClassDes.text = classDes
textViewClassDes.text = classDes!!.replace("\n", "\n")
textViewClassContent.text = classContent
textViewClassContent.text = classContent!!.replace("\n", "\n")
btnPlay.isEnabled = true
btnStop.isEnabled = false
btnPlay.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this@DetailActivity, Uri.parse(classAudio))
mediaPlayer!!.isLooping = true
mediaPlayer!!.start()
btnStop.isEnabled = true
btnPlay.isEnabled = false
Toast.makeText(applicationContext,"Audio Starts",Toast.LENGTH_SHORT).show()
} else mediaPlayer!!.start()
}
})
mediaPlayer?.setOnCompletionListener {
btnPlay.isEnabled = true
btnStop.isEnabled = false
Toast.makeText(this,"end",Toast.LENGTH_SHORT).show()
}
btnStop.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
if(mediaPlayer!!.isPlaying){
stop = false
mediaPlayer!!.stop()
mediaPlayer!!.reset()
mediaPlayer!!.release()
mediaPlayer = null
btnPlay.isEnabled = true
btnStop.isEnabled = false
Toast.makeText(applicationContext,"Audio Stops",Toast.LENGTH_SHORT).show()
}
}
})
}
}
这是调用 Activity
的 onDestroy
的 classic 案例。我会保持简短,但是 Activity
经历了一个生命周期,当你似乎从第二个 Activity 回到第一个 Activity 时,onDestroy
你的第二个activity被调用来执行销毁函数
所以解决方案,重写 DetailActivity
class 中的 onDestroy
方法并执行类似这样的操作
override fun onDestroy(){
if(mediaPlayer != null){
mediaPlayer!!.stop()
mediaPlayer!!.reset()
mediaPlayer!!.release()
mediaPlayer = null
}
}
您可以详细阅读 Activity Lifecyle