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()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)