Android MediaPlayer调用raw/assets/

2022-03-27  本文已影响0人  没有了遇见

MediaPlayer Android 体统的播放流媒体文件的工具类,项目需要播放音频,特此简单记录一下音频播放的方法调用

1. MediaPlayer支持:AAC、AMR、FLAC、MP3、MIDI、OGG、PCM等格式

2. MediaPlayer 方法调用

Media方法调用.png

三种资源实现播放:

工具类

package com.wu.material.util
import android.annotation.SuppressLint
import android.content.Context
import android.content.res.AssetFileDescriptor
import android.media.AudioManager
import android.media.MediaPlayer
import android.net.Uri
import android.os.Build
import androidx.annotation.RequiresApi

/**
 * 音频播放
 */
@SuppressLint("StaticFieldLeak")
object MediaPlayerUtil {

    private var mContext: Context? = null
    var mPlayer: MediaPlayer? = null

    private var isPause = false

    private var isPlaying = false

    fun isPlaying(): Boolean {
        return isPlaying
    }

    fun initMedia(mContext:Context,rawRes: Int) {
        mPlayer = MediaPlayer.create(mContext, rawRes)
        mPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC)
    }

    @RequiresApi(Build.VERSION_CODES.N)
    fun initMedia(mContext:Context, fad: AssetFileDescriptor) {
        mPlayer = MediaPlayer()
        mPlayer!!.setDataSource(fad)
    }

    fun initMedia(mContext:Context,path: String) {
        mPlayer = MediaPlayer()
        mPlayer!!.setDataSource(path)
    }

    fun initUriMedia(mContext:Context,uriPath: String) {
        mPlayer = MediaPlayer()
        mPlayer!!.setDataSource(mContext!!, Uri.parse(uriPath))
        mPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC)
    }

    fun setMediaListener(listener: MediaPlayerListener) {
        mPlayer!!.setOnCompletionListener {
            isPlaying = false
            listener.finish()
        }

        mPlayer!!.setOnErrorListener { mp, what, extra ->
            isPlaying = false
            listener.onErr(what)
            false
        }
        mPlayer!!.setOnPreparedListener {
            listener.prepare()
        }
    }

    fun playMedia() {
        if (mPlayer != null && !mPlayer!!.isPlaying) {
            mPlayer!!.start()
        }
    }

    fun prepare() {
        if (mPlayer != null && !mPlayer!!.isPlaying) {
            mPlayer!!.prepare()
        }
    }

    fun prepareAsync() {
        if (mPlayer != null && !mPlayer!!.isPlaying) {
            mPlayer!!.prepareAsync()
        }
    }

    fun pause() {
        if (mPlayer != null && mPlayer!!.isPlaying) {
            mPlayer!!.pause()
            isPause = true
        }
    }

    // 继续
    fun resume() {
        if (mPlayer != null && isPause) {
            mPlayer!!.start()
            isPause = false
        }
    }

    fun release() {
        if (mPlayer != null) {
            try {
                mPlayer!!.a's'()
            } catch (e: Exception) {
            }
            mPlayer = null
        }
        isPlaying = false
    }

}

注意:MediaPlayer.create() 内部已调用 mp.prepare()


源码查看.png

1.引用raw 的资源播放音频

        MediaPlayerUtil.initMedia(this,R.raw.audio)
        MediaPlayerUtil.setMediaListener(object :MediaPlayerListener{
            override fun onErr(messageCode: Int) {
            }

            override fun finish() {
            }

            override fun prepare() {
            }
        })

        MediaPlayerUtil.playMedia()

2.引用assets的资源播放音频

 var fikkDes= assets.openFd("audio.mp3") as AssetFileDescriptor
        MediaPlayerUtil.initMedia(this,fikkDes)
        MediaPlayerUtil.setMediaListener(object :MediaPlayerListener{
            override fun onErr(messageCode: Int) {
            }

            override fun finish() {
            }
            override fun prepare() {
                MediaPlayerUtil.playMedia()
            }
        })
        MediaPlayerUtil.prepareAsync()

3.引用网络资源播放音频

   var path="https://downsc.chinaz.net/Files/DownLoad/sound1/202103/s1024.mp3"
    private fun playRawUri() {
        MediaPlayerUtil.initMedia(this,path)

        MediaPlayerUtil.setMediaListener(object :MediaPlayerListener{
            override fun onErr(messageCode: Int) {
            }

            override fun finish() {
            }
            override fun prepare() {
                MediaPlayerUtil.playMedia()
            }
        })
        MediaPlayerUtil.prepareAsync()
    }

总结

如有帮助欢迎点赞,
源码地址

上一篇 下一篇

猜你喜欢

热点阅读