[6.1.0]M1平行驾驶状态下弱网提示改为提示音提示并且不展示提示接管动画
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package com.mogo.eagle.core.utilcode.util;
|
||||
|
||||
import android.media.*;
|
||||
import android.content.*;
|
||||
import android.os.*;
|
||||
import android.app.*;
|
||||
|
||||
/**
|
||||
* 音频播放工具类
|
||||
*/
|
||||
public class SoundPoolUtils {
|
||||
|
||||
private SoundPool mSoundPool;
|
||||
private AudioManager mAudioManager;
|
||||
private float volume;
|
||||
// Maximumn sound stream.
|
||||
private static final int MAX_STREAMS = 5;
|
||||
// Stream type.
|
||||
private static final int streamType = AudioManager.STREAM_MUSIC;
|
||||
private int mSoundId;
|
||||
private int resId;
|
||||
private Context mContext;
|
||||
|
||||
private volatile static SoundPoolUtils INSTANCE;
|
||||
|
||||
public static SoundPoolUtils getSoundPool(){
|
||||
if (INSTANCE == null){
|
||||
synchronized (SoundPoolUtils.class){
|
||||
if (INSTANCE == null){
|
||||
INSTANCE = new SoundPoolUtils();
|
||||
}
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public SoundPoolUtils(){}
|
||||
|
||||
//播放资源文件
|
||||
public void playSoundWithRedId(Context context,int resId){
|
||||
this.mContext = context;
|
||||
this.resId=resId;
|
||||
init();
|
||||
}
|
||||
|
||||
//init settings
|
||||
private void init(){
|
||||
// AudioManager audio settings for adjusting the volume
|
||||
mAudioManager = (AudioManager)this.mContext. getSystemService(Context.AUDIO_SERVICE);
|
||||
|
||||
// Current volumn Index of particular stream type.
|
||||
float currentVolumeIndex = (float) mAudioManager.getStreamVolume(streamType);
|
||||
|
||||
// Get the maximum volume index for a particular stream type.
|
||||
float maxVolumeIndex = (float) mAudioManager.getStreamMaxVolume(streamType);
|
||||
|
||||
// Volumn (0 --> 1)
|
||||
this.volume = currentVolumeIndex / maxVolumeIndex;
|
||||
|
||||
// Suggests an audio stream whose volume should be changed by
|
||||
// the hardware volume controls.
|
||||
((Activity)this.mContext).setVolumeControlStream(streamType);
|
||||
|
||||
if (mSoundPool == null){
|
||||
// For Android SDK >= 21
|
||||
if (Build.VERSION.SDK_INT >= 21 ) {
|
||||
|
||||
AudioAttributes audioAttrib = new AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_GAME)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||
.build();
|
||||
|
||||
SoundPool.Builder builder= new SoundPool.Builder();
|
||||
builder.setAudioAttributes(audioAttrib).setMaxStreams(MAX_STREAMS);
|
||||
|
||||
this.mSoundPool = builder.build();
|
||||
} else {// for Android SDK < 21
|
||||
// SoundPool(int maxStreams, int streamType, int srcQuality)
|
||||
this.mSoundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// When Sound Pool load complete.
|
||||
this.mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
|
||||
@Override
|
||||
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
|
||||
playSound();
|
||||
}
|
||||
});
|
||||
|
||||
//load res
|
||||
this.mSoundId =this.mSoundPool.load(this.mContext,this.resId,1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放资源
|
||||
*/
|
||||
public void releaseSoundPool() {
|
||||
if (mSoundPool != null) {
|
||||
mSoundPool.autoPause();
|
||||
mSoundPool.unload(mSoundId);
|
||||
mSoundPool.release();
|
||||
mSoundPool = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//play the sound res
|
||||
private void playSound(){
|
||||
float leftVolumn = volume;
|
||||
float rightVolumn = volume;
|
||||
// Play sound of gunfire. Returns the ID of the new stream.
|
||||
int streamId = this.mSoundPool.play(this.mSoundId,leftVolumn, rightVolumn, 1, 0, 1f);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user