android 实现同时播放多个音频和连续播放多个音频

android 实现同时播放多个音频和连续播放多个音频,第1张

1,同时播放多个音频

lateinit var mSoundPool: SoundPool
var mSoundMap = HashMap()

val audioAttrib: AudioAttributes = AudioAttributes.Builder()
    .setUsage(AudioAttributes.USAGE_GAME)
    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
    .build()
mSoundPool = SoundPool.Builder().setAudioAttributes(audioAttrib).setMaxStreams(6).build()

//音频
mSoundMap[1] = mSoundPool.load(this, R.raw.pre, 1)
mSoundMap[2] = mSoundPool.load(this, R.raw.nine, 1)
mSoundMap[3] = mSoundPool.load(this, R.raw.dot, 1)
mSoundMap[4] = mSoundPool.load(this, R.raw.yuan, 1)

//播放

val mgr = getSystemService(Context.AUDIO_SERVICE) as AudioManager
val streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC).toFloat()
val streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC).toFloat()
val volume = streamVolumeCurrent / streamVolumeMax
//播放第一个
mSoundPool.play(mSoundMap[1]!!, volume, volume, 1, 0, 1.0f)

//播放第二个

mSoundPool.play(mSoundMap[2]!!, volume, volume, 2, 0, 1.0f)

2,连续播放多个音频

implementation 'com.google.android.exoplayer:exoplayer:2.13.3'

//多个音频

val url =
    Uri.parse("android.resource://" + packageName + "/" + R.raw.pre)
val url2 =
    Uri.parse("android.resource://" + packageName + "/" + R.raw.nine)

val url3 =
    Uri.parse("android.resource://" + packageName + "/" + R.raw.yuan)
var one = MediaItem.fromUri(url)
var two = MediaItem.fromUri(url2)
var three = MediaItem.fromUri(url3)

//播放

var     player = SimpleExoPlayer .Builder(this).build()
player.addMediaItem(one)
player.addMediaItem(two)
player.addMediaItem(three)
player.prepare()
player.play()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/992492.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存