package com.pecuyu.soundpooltest;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
/**
* @author pecuyu
* @date 2018.9.18
*/
public class MainActivity extends AppCompatActivity implements SoundPool.OnLoadCompleteListener {
private static final String TAG = MainActivity.class.getSimpleName();
private SoundPool mSoundPool;
private static final int DEFAULT_INVALID_SOUND_ID = -Integer.MAX_VALUE;
private static final int DEFAULT_INVALID_STREAM_ID = -Integer.MAX_VALUE;
private int mSoundId = DEFAULT_INVALID_SOUND_ID;
private int mStreamID = DEFAULT_INVALID_STREAM_ID;
private SeekBar mLeftVolumeBar, mRightVolumeBar, mRateBar; // 调节左右volume的 seekbar
private AudioManager mAudioManager;
private float mCruLeftVolume, mCurRightVolume; // 用于设置sound pool的左右volume值 0~1f
private int mVolumeBarMax;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLeftVolumeBar = findViewById(R.id.id_sk_left_vloume);
mRightVolumeBar = findViewById(R.id.id_sk_right_vloume);
mRateBar = findViewById(R.id.id_sk_rate);
mRateBar.setProgress(50);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
float volumeRate = getVolumeRate();
mCruLeftVolume = volumeRate;
mCurRightVolume = volumeRate;
//计算位置
mVolumeBarMax = mLeftVolumeBar.getMax();
int position = (int) (mVolumeBarMax * volumeRate);
mRightVolumeBar.setProgress(position);
mLeftVolumeBar.setProgress(position);
mLeftVolumeBar.setOnSeekBarChangeListener(mSeekBarListener);
mRightVolumeBar.setOnSeekBarChangeListener(mSeekBarListener);
mRateBar.setOnSeekBarChangeListener(mSeekBarListener);
}
private OnSeekBarChangeListenerAdapter mSeekBarListener = new OnSeekBarChangeListenerAdapter() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
super.onProgressChanged(seekBar, progress, fromUser);
if (mSoundPool == null) return; // mSoundPool may not be initialized then
float volume = progress * 1.0f / mVolumeBarMax;
switch (seekBar.getId()) {
case R.id.id_sk_left_vloume: // 设置左 volume
mCruLeftVolume = volume;
mSoundPool.setVolume(mStreamID, mCruLeftVolume, mCurRightVolume);
break;
case R.id.id_sk_right_vloume: // 设置又 volume
mCurRightVolume = volume;
mSoundPool.setVolume(mStreamID, mCruLeftVolume, mCurRightVolume);
break;
case R.id.id_sk_rate:
float rate = 0.5f + progress * 1.0f / mVolumeBarMax * 1.5f;
mSoundPool.setRate(mStreamID, rate);
break;
}
}
};
/**
* 计算volume当前占比
* 比如当前是10 , 范围是 0~15
*
* @return rate
*/
private float getVolumeRate() {
if (mAudioManager != null) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//int streamMinVolume = mAudioManager.getStreamMinVolume(AudioManager.STREAM_MUSIC);
int streamMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
Log.d(TAG, "streamVolume=" + streamVolume + "," +
//" streamMinVolume=" + streamMinVolume +
" streamMaxVolume=" + streamMaxVolume);
return streamVolume * 1.0f / streamMaxVolume;
}
return 0;
}
/**
* 创建SoundPool
*/
private void createSoundPoolIfNeeded() {
if (mSoundPool == null) {
// 5.0 及 之后
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttributes = null;
audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mSoundPool = new SoundPool.Builder()
.setMaxStreams(16)
.setAudioAttributes(audioAttributes)
.build();
} else { // 5.0 以前
mSoundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0); // 创建SoundPool
}
mSoundPool.setOnLoadCompleteListener(this); // 设置加载完成监听
}
}
/**
* 播放:如果资源还没有加载,则可能会有一小段等待时间
*/
private void playMayWait() {
createSoundPoolIfNeeded();
if (mSoundId == DEFAULT_INVALID_SOUND_ID) { // mSoundId is invalid ,load from res raw for once before mSoundPool is released
mSoundId = mSoundPool.load(getApplicationContext(), R.raw.autotest, 1); // 加载音频资源
} else {
// reuse the loaded res
if (mStreamID == DEFAULT_INVALID_STREAM_ID)
onLoadComplete(mSoundPool, 0, 0); // manually call this method when there is a valid mSoundId
}
}
/**
* 停止播放
*/
private void stop() {
if (mSoundPool != null) {
mSoundPool.stop(mStreamID);
mStreamID = DEFAULT_INVALID_STREAM_ID;
}
}
/**
* 释放资源
*/
private void releaseSoundPool() {
if (mSoundPool != null) {
mSoundPool.autoPause();
mSoundPool.unload(mSoundId);
mSoundId = DEFAULT_INVALID_SOUND_ID;
mSoundPool.release();
mSoundPool = null;
}
}
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
if (mSoundPool != null) {
if (mStreamID == DEFAULT_INVALID_STREAM_ID)
mStreamID = mSoundPool.play(mSoundId, mCruLeftVolume, mCurRightVolume, 16, -1, 1.0f);
}
}
public void play(View view) {
playMayWait();
}
public void pause(View view) {
if (mSoundPool != null) {
mSoundPool.pause(mStreamID);
}
}
public void resume(View view) {
if (mSoundPool != null) {
mSoundPool.resume(mStreamID);
}
}
public void stop(View view) {
stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
releaseSoundPool();
}
}
评论3
最新资源