单击按钮时如何在媒体播放器中播放媒体? (Android Studio,科特林)

时间:2018-08-05 23:09:10

标签: android android-studio button kotlin media-player

我是Android Studio和Kotlin语言的新手。我制作了一个带有媒体播放器的音频应用。单击按钮时,我想打开媒体。当我第一次单击它时,它会播放,但是当我再次单击它时,它会关闭媒体并且不播放。我使用音频管理器在播放时关闭所有媒体。

这是MainActivity中的代码:

import android.annotation.SuppressLint
import android.media.AudioManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.media.MediaPlayer
import android.os.Handler
import java.io.BufferedReader
import java.util.NoSuchElementException
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import java.util.concurrent.TimeUnit
import android.support.v4.os.HandlerCompat.postDelayed
import android.support.v4.os.HandlerCompat.postDelayed
import android.support.v4.os.HandlerCompat.postDelayed
import android.support.v4.os.HandlerCompat.postDelayed
import android.util.Log
import kotlin.concurrent.timer


var x  =0
var y=0
@Suppress("UNREACHABLE_CODE")
class MainActivity : AppCompatActivity() {


@SuppressLint("PrivateResource")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val mediafatihah = MediaPlayer.create(applicationContext, R.raw.fatihah)
    val mediaalbaqarah = MediaPlayer.create(applicationContext, 
R.raw.albaqarah)
    val mediaimran =MediaPlayer.create(applicationContext,R.raw.imran)
    val medianisa =MediaPlayer.create(applicationContext, R.raw.nisa)
    val mediamidiah =MediaPlayer.create(applicationContext,R.raw.maidah)
    val mediaanam =MediaPlayer.create(applicationContext, R.raw.aniam)
    val mediaaraf =MediaPlayer.create(applicationContext,R.raw.araf)
    val mediaanfal =MediaPlayer.create(applicationContext, R.raw.anfal)
    val mediaattaubah =MediaPlayer.create(applicationContext, R.raw.attaubah)
    val mediayunus =MediaPlayer.create(applicationContext, R.raw.yunus)


    val medianooh = MediaPlayer.create(applicationContext, R.raw.nooh)
    val mediaalqasas =MediaPlayer.create(applicationContext, R.raw.alqasas)
fun audiomanager(){
try {
    if (mediaalbaqarah.isPlaying){
        mediaalbaqarah.stop()
        return
    }
    if (mediafatihah.isPlaying){
        mediafatihah.stop()
        return
    }
    if (medianooh.isPlaying){
        medianooh.stop()
        return
    }
    if (mediaimran.isPlaying){
        mediaimran.stop()
        return
    }
    if (medianisa.isPlaying){
        medianisa.stop()
    }
    if (mediamidiah.isPlaying){
        mediamidiah.stop()
    }
        if (mediaanam.isPlaying){
            mediaanam.stop()
        }
    if (mediaaraf.isPlaying){
        mediaaraf.stop()
    }
    if (mediaanfal.isPlaying){
        mediaanfal.stop()
    }
    if (mediayunus.isPlaying){
        mediayunus.stop()
    }
    if (mediaattaubah.isPlaying){
        mediaattaubah.stop()
    }


}catch (ex:Exception){ }


}

    seekBar.isClickable = false

    fatihahid.setOnClickListener {
        try {
            audiomanager()
        }catch (ex:Exception){


                     }
        mediafatihah.start()
        mediafatihah.isLooping = false


        player.setImageResource(R.drawable.ic_puase)

        x = 0


    }

    albaqarah.setOnClickListener {
        try {
            audiomanager()
        }catch (ex:Exception){
            mediaalbaqarah.start()
        }
        mediaalbaqarah.isLooping = false
        mediaalbaqarah.start()
        x = 0
        player.setImageResource(R.drawable.ic_puase)
        return@setOnClickListener

    }

    imranid.setOnClickListener{
        audiomanager()
        mediaimran.isLooping =false
        mediaimran.start()
        player.setImageResource(R.drawable.ic_puase)
        x=0
    }

anamid.setOnClickListener{
audiomanager()
mediaanam.isLooping =false
mediaanam.start()
player.setImageResource(R.drawable.ic_puase)
  x=0
}
    maidah.setOnClickListener{
        audiomanager()
        mediamidiah.isLooping =false
        mediamidiah.start()
        player.setImageResource(R.drawable.ic_puase)
        x=0
    }
    nisaid.setOnClickListener {
        audiomanager()
        medianisa.isLooping =false
        medianisa.start()
        player.setImageResource(R.drawable.ic_puase)
        x=0
    }


    arafid.setOnClickListener {
        audiomanager()
        mediaaraf.isLooping =false
        mediaaraf.start()
        player.setImageResource(R.drawable.ic_puase)
        x=0
    }
    anfalid.setOnClickListener {
        audiomanager()
        mediaanfal.isLooping =false
        mediaanfal.start()
        player.setImageResource(R.drawable.ic_puase)
        x=0
    }
    taubahid.setOnClickListener {
        audiomanager()
        mediaattaubah.isLooping =false
        mediaattaubah.start()
        player.setImageResource(R.drawable.ic_puase)
        x=0
    }
    yunusid.setOnClickListener {
        audiomanager()
        mediayunus.isLooping =false
        mediayunus.start()
        player.setImageResource(R.drawable.ic_puase)
        x=0
    }


    noohid.setOnClickListener {
    audiomanager()
        medianooh.isLooping = false
        medianooh.start()
        x = 0
        player.setImageResource(R.drawable.ic_puase)

        return@setOnClickListener

    }



    player.setOnClickListener {
        if (mediafatihah.isPlaying) {
            player.setImageResource(R.drawable.ic_player)
            mediafatihah.pause()
            x = 2
        } else if (x == 2) {
            mediafatihah.start()
            player.setImageResource(R.drawable.ic_puase)

        }


        if (mediaalbaqarah.isPlaying) {
            mediaalbaqarah.pause()
            player.setImageResource(R.drawable.ic_player)
            x = 1
        } else if (x == 1) {
            player.setImageResource(R.drawable.ic_puase)

            mediaalbaqarah.start()

        }

        if (mediaimran.isPlaying) {
            mediaimran.pause()
            player.setImageResource(R.drawable.ic_player)
            x = 3
        } else if (x == 3) {
            player.setImageResource(R.drawable.ic_puase)

            mediaimran.start()

        }
        if (medianisa.isPlaying) {
            medianisa.pause()
            player.setImageResource(R.drawable.ic_player)
            x = 4
        } else if (x == 4) {
            player.setImageResource(R.drawable.ic_puase)
            medianisa.start()
        }
        if (mediamidiah.isPlaying) {
            mediamidiah.pause()
            player.setImageResource(R.drawable.ic_player)
            x = 5
        } else if (x == 5) {
            player.setImageResource(R.drawable.ic_puase)
            mediamidiah.start()
        }
        if (mediaanam.isPlaying) {
            mediaanam.pause()
            player.setImageResource(R.drawable.ic_player)
            x = 6
        } else if (x == 6) {
            player.setImageResource(R.drawable.ic_puase)
            mediaanam.start()
        }
        if (mediaaraf.isPlaying) {
            mediaaraf.pause()
            player.setImageResource(R.drawable.ic_player)
            x = 7
        } else if (x == 7) {
            player.setImageResource(R.drawable.ic_puase)
            mediaaraf.start()
        }
        if (mediaanfal.isPlaying) {
            mediaanfal.pause()
            player.setImageResource(R.drawable.ic_player)
            x = 8
        } else if (x == 8) {
            player.setImageResource(R.drawable.ic_puase)
            mediaanfal.start()
        }
        if (mediaattaubah.isPlaying) {
            mediaattaubah.pause()
            player.setImageResource(R.drawable.ic_player)
            x = 9
        } else if (x == 9) {
            player.setImageResource(R.drawable.ic_puase)
            mediaattaubah.start()
        }
        if (mediayunus.isPlaying) {
            mediayunus.pause()
            player.setImageResource(R.drawable.ic_player)
            x = 10
        } else if (x == 10) {
            player.setImageResource(R.drawable.ic_puase)
            mediayunus.start()
        }



        if (medianooh.isPlaying) {
            player.setImageResource(R.drawable.ic_player)
            medianooh.pause()
            x = 71
        } else if (x == 71) {
            medianooh.start()
            player.setImageResource(R.drawable.ic_puase)

        }

    }
}

}

0 个答案:

没有答案