我正在为儿童构建一个教育应用程序,我正在使用媒体播放器来播放字母和字母歌曲的声音,但是当我调用.stop((函数时,该应用程序有时会崩溃。
错误按摩是
at android.media.MediaPlayer._stop (Native Method) at android.media.MediaPlayer.stop (MediaPlayer.java:1550) at com.alotaibi.alphabetapp.arabic_alph$onCreate$4.onClick (arabic_alph.kt:110) at android.view.View.performClick (View.java:6897) at android.view.View$PerformClick.run (View.java:26101) at android.os.Handler.handleCallback (Handler.java:789) at android.os.Handler.dispatchMessage (Handler.java:98) at android.os.Looper.loop (Looper.java:164) at android.app.ActivityThread.main (ActivityThread.java:6944) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
,这是函数
play_abc.setOnClickListener {/// this fuction plays the sound of alphabet
play_abc.setImageResource(R.drawable.pause)
if(abcSongAra!=null) { // here i check if the song is playing then stop it
abcSongAra?.stop() /// the app crashes here but not always just sometimes
abcSongAra?.prepare()
}
if (active2 == 1) {
abcPlayAra = MediaPlayer.create(this, R.raw.playy)
abcPlayAra?.setOnCompletionListener { mp ->
mp.reset()
mp.release()
abcPlayAra = null
active2=1
}
abcPlayAra?.start()
active2 = 2
play_abc.startAnimation(anim)
play_abc.setImageResource(R.drawable.pause)
} else if (active2 == 2) {
active2 = 1
if(abcPlayAra!=null) {
abcPlayAra?.stop()
abcPlayAra?.prepare()
}
play_abc.startAnimation(anim)
play_abc.setImageResource(R.drawable.play_pause)
}
}
这是整个代码
package com.alotaibi.alphabetapp
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.InterstitialAd
import com.google.android.gms.ads.MobileAds
import kotlinx.android.synthetic.main.activity_arabic_alph.*
lateinit var mAdView_araAlph: AdView
class arabic_alph : AppCompatActivity() {
//song palyer
var abcSongAra:MediaPlayer?= MediaPlayer()
var abcPlayAra:MediaPlayer?= MediaPlayer()
var ls:MediaPlayer?= MediaPlayer()
//animation
var anim:Animation?=null
private lateinit var mInterstitialAd: InterstitialAd
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_arabic_alph)
MobileAds.initialize(this) {}
mAdView_araAlph = findViewById(R.id.adView)
val adRequest = AdRequest.Builder().build()
mAdView_araAlph.loadAd(adRequest)
mInterstitialAd = InterstitialAd(this)
mInterstitialAd.adUnitId = "ca-app-pub-9266659982084890/2364898561"
mInterstitialAd.loadAd(AdRequest.Builder().build())
anim=AnimationUtils.loadAnimation(this,R.anim.rotate)
abcSongAra = MediaPlayer.create(this, R.raw.songg)
abcPlayAra = MediaPlayer.create(this, R.raw.playy)
ls = MediaPlayer.create(this, R.raw.ara1)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start()
var active1 = 1
var active2 = 1
song.setOnClickListener {
song.setImageResource(R.drawable.pause)
if(abcPlayAra!=null) {
abcPlayAra?.stop()
abcPlayAra?.prepare()
}
if (active1 == 1) {
abcSongAra = MediaPlayer.create(this, R.raw.songg)
abcSongAra?.setOnCompletionListener { mp ->
mp.reset()
mp.release()
abcSongAra = null
active1=1
}
abcSongAra?.start()
active1 = 2
song.startAnimation(anim)
song.setImageResource(R.drawable.pause)
} else if (active1 == 2) {
active1 = 1
if(abcSongAra!=null) {
abcSongAra?.stop()
abcSongAra?.prepare()
}
song.startAnimation(anim)
song.setImageResource(R.drawable.soonngg)
}
}
play_abc.setOnClickListener {
play_abc.setImageResource(R.drawable.pause)
if(abcSongAra!=null) {
abcSongAra?.stop()
abcSongAra?.prepare()
}
if (active2 == 1) {
abcPlayAra = MediaPlayer.create(this, R.raw.playy)
abcPlayAra?.setOnCompletionListener { mp ->
mp.reset()
mp.release()
abcPlayAra = null
active2=1
}
abcPlayAra?.start()
active2 = 2
play_abc.startAnimation(anim)
play_abc.setImageResource(R.drawable.pause)
} else if (active2 == 2) {
active2 = 1
if(abcPlayAra!=null) {
abcPlayAra?.stop()
abcPlayAra?.prepare()
}
play_abc.startAnimation(anim)
play_abc.setImageResource(R.drawable.play_pause)
}
}
}
fun letterSound(view: View) {
anim=AnimationUtils.loadAnimation(this,R.anim.rotate)
var choice = view
when (choice.id) {
R.id.h7a2 -> {
h7a2.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.h7a2)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start()
}
R.id.jeem -> {
jeem.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.jeem)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.tha2 -> {
tha2.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.tha2)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.ta2 -> {
ta2.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.ta2)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.ba2 -> {
ba2.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.ba2)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.alf -> {
alf.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.alf)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.seen -> {
seen.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.seen)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.zay -> {
zay.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.zay)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.ra2 -> {
ra2.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.ra2)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.zal -> {
zal.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.zal)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.dal -> {
dal.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.dal)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.kha2 -> {
kha2.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.kha2)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.a3en -> {
a3en.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.eye)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.za2 -> {
za2.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.za2)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.t6a2 -> {
t6a2.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.t6a2)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.dad -> {
dad.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.dad)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.sad -> {
sad.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.sad)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.sheen -> {
sheen.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.sheen)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.meem -> {
meem.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.meem)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.lam -> {
lam.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.lam)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.kaf -> {
kaf.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.kaf)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.k8af -> {
k8af.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.k8af)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.fa2 -> {
fa2.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.fa2)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.kein -> {
kein.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.kein)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.waw -> {
waw.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.waw)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.ha2 -> {
ha2.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.ha2)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.ya2->
{
ya2.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.ya2)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
R.id.noon->
{
noon.startAnimation(anim)
ls = MediaPlayer.create(this, R.raw.noon)
ls!!.setOnCompletionListener { mp ->
mp.reset()
mp.release()
ls = null
}
ls!!.start() }
}
}
override fun onPause() {
super.onPause()
if(abcPlayAra!=null) {
abcPlayAra?.release()
}
if(ls!=null) {
ls?.release()
}
if(abcSongAra!=null) {
abcSongAra?.release()
}
if (mInterstitialAd.isLoaded) {
mInterstitialAd.show()
}
}
}
您没有正确的媒体播放器生命周期。MediaPlayer将抛出IllegalStateException,如果你的播放器不是init。
提供完整的异常堆栈跟踪。