下面列出了android.media.SoundPool#load ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public TimerView(Context context, AttributeSet attrs, int style) {
super(context, attrs, style);
mSoundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
mTimerFinishedSoundId = mSoundPool.load(context, R.raw.timer_finished, SOUND_PRIORITY);
LayoutInflater.from(context).inflate(R.layout.card_timer, this);
mHoursView = (TextView) findViewById(R.id.hours);
mMinutesView = (TextView) findViewById(R.id.minutes);
mSecondsView = (TextView) findViewById(R.id.seconds);
mTipView = (TextView) findViewById(R.id.tip);
mTipView.setText(context.getResources().getString(R.string.timer_finished));
mTipView.setVisibility(View.INVISIBLE);
mWhiteColor = context.getResources().getColor(R.color.white);
mRedColor = context.getResources().getColor(R.color.red);
mTimer = new Timer();
mTimer.setListener(mTimerListener);
mTimer.setDurationMillis(0);
}
private void shake() {
SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_SYSTEM, 5);
int weiChatAudio = soundPool.load(CodeGenerateActivity.this, R.raw.weichat_audio, 1);
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
//发出提示音
soundPool.play(weiChatAudio, 1, 1, 0, 0, 1);
vibrator.vibrate(300);
generateGroup();
isShaking = true;
try {
Thread.sleep(100);
isShaking = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void setupSound()
{
spool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
spool.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
{
if(status==0)
{
if(sampleId==soundID)
canPlaySound = true;
}
}
});
soundID = spool.load(getApplicationContext(), R.raw.one_click, 1);
}
public void loadSounds(String directory) {
// Sounds
sounds = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
biteSound = sounds.load(directory + "/apple.wav", 1);
crashSound = sounds.load(directory + "/crash.wav", 1);
appearSound = sounds.load(directory + "/appear.wav", 1);
}
protected void initView() {
//初始化SoundPool
mSoundPool = new SoundPool(1, AudioManager.STREAM_SYSTEM, 5);
mWeiChatAudio = mSoundPool.load(this, R.raw.shake_sound, 1);
//获取Vibrator震动服务
mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
}
private void initSound() {
mSoundPool = new SoundPool(50, AudioManager.STREAM_SYSTEM, 5);
try {
mSoundPool.load(getContext(), R.raw.wheelpickerkeypress, 1);
} catch (Exception e) {
}
}
private void doAlarm() {
if (mCurrMInfo.isVibrate()){
mVibrator.vibrate(1000);
}
if (mCurrMInfo.isRing()){
SoundPool soundPool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 5);
soundPool.load(this, R.raw.ticket_alarm, 1);
soundPool.play(1, 1, 1, 0, 0, 1);
}
}
private void init() {
finder = ResoureFinder.getInstance(getContext());
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundId = soundPool.load(getContext(), finder.getRawId("collision"), 1);
touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
DisplayMetrics dm = getResources().getDisplayMetrics();
bitmapBall = BitmapFactory.decodeResource(getResources(), finder.getDrawableId("platform_myspace_ball"));
bitmapHole = BitmapFactory.decodeResource(getResources(), finder.getDrawableId("platform_myspace_hole"));
bitmapW = bitmapBall.getWidth();
bitmapH = bitmapBall.getHeight();
radius = bitmapW / 2;
dissAreaLeft = 200 * dm.density;
dissAreaTop = 50 * dm.density;
dissAreaRight = dissAreaLeft + bitmapHole.getWidth();
dissAreaBottom = dissAreaTop + bitmapHole.getHeight();
activity = (Activity) getContext();
Rect rectgle = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
windowWidth = rectgle.width();
windowHeight = rectgle.height();
mVibrator = (Vibrator) activity.getApplication().getSystemService(Service.VIBRATOR_SERVICE);
sm = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
setBackgroundDrawable(finder.getDrawable("platform_myspace_gsense_bg_shape"));
params = new WindowManager.LayoutParams();
params.height = windowHeight;
params.width = windowWidth;
params.type = WindowManager.LayoutParams.TYPE_APPLICATION | WindowManager.LayoutParams.FIRST_SUB_WINDOW;
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_BLUR_BEHIND;// 模态,不能获得焦点,背景失焦
params.alpha = 1.0f;
params.format = PixelFormat.TRANSPARENT;
params.gravity = Gravity.LEFT | Gravity.TOP;
params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
params.x = dm.widthPixels - windowWidth;
params.y = dm.heightPixels - windowHeight;
params.windowAnimations = finder.getStyleId("Anim_platform_myspace_fade");
}
/**
* Loads the audio specified by mediaPath into the given SoundPool and
* returns the sound id.
*
* Note that if the mediaPath is a content URI or an URL, the audio must be
* copied to a temp file and then loaded from there. This could have
* performance implications.
*
* @param soundPool the SoundPool
* @param form the Form
* @param mediaPath the path to the media
*/
public static int loadSoundPool(SoundPool soundPool, Form form, String mediaPath)
throws IOException {
MediaSource mediaSource = determineMediaSource(form, mediaPath);
switch (mediaSource) {
case ASSET:
return soundPool.load(getAssetsIgnoreCaseAfd(form,mediaPath), 1);
case REPL_ASSET:
form.assertPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
return soundPool.load(replAssetPath(mediaPath), 1);
case SDCARD:
form.assertPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
return soundPool.load(mediaPath, 1);
case FILE_URL:
if (isExternalFileUrl(mediaPath)) {
form.assertPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
}
return soundPool.load(fileUrlToFilePath(mediaPath), 1);
case CONTENT_URI:
case URL:
File tempFile = cacheMediaTempFile(form, mediaPath, mediaSource);
return soundPool.load(tempFile.getAbsolutePath(), 1);
case CONTACT_URI:
throw new IOException("Unable to load audio for contact " + mediaPath + ".");
}
throw new IOException("Unable to load audio " + mediaPath + ".");
}
public MessagesController() {
MessagesStorage storage = MessagesStorage.getInstance();
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
fontSize = preferences.getInt("fons_size", 16);
try {
soundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
sound = soundPool.load(ApplicationLoader.applicationContext, R.raw.electronic, 1);
} catch (Exception e) {
FileLog.e("yahala", e);
}
}
@Override
public void onCreate() {
super.onCreate();
if (BuildVars.LOGS_ENABLED) {
FileLog.d("=============== VoIPService STARTING ===============");
}
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)!=null) {
int outFramesPerBuffer = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
VoIPController.setNativeBufferSize(outFramesPerBuffer);
} else {
VoIPController.setNativeBufferSize(AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT) / 2);
}
try {
cpuWakelock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "telegram-voip");
cpuWakelock.acquire();
btAdapter=am.isBluetoothScoAvailableOffCall() ? BluetoothAdapter.getDefaultAdapter() : null;
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
if(!USE_CONNECTION_SERVICE){
filter.addAction(ACTION_HEADSET_PLUG);
if(btAdapter!=null){
filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
}
filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
}
registerReceiver(receiver, filter);
soundPool = new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
spConnectingId = soundPool.load(this, R.raw.voip_connecting, 1);
spRingbackID = soundPool.load(this, R.raw.voip_ringback, 1);
spFailedID = soundPool.load(this, R.raw.voip_failed, 1);
spEndId = soundPool.load(this, R.raw.voip_end, 1);
spBusyId = soundPool.load(this, R.raw.voip_busy, 1);
am.registerMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));
if(!USE_CONNECTION_SERVICE && btAdapter!=null && btAdapter.isEnabled()){
int headsetState=btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
updateBluetoothHeadsetState(headsetState==BluetoothProfile.STATE_CONNECTED);
//if(headsetState==BluetoothProfile.STATE_CONNECTED)
// am.setBluetoothScoOn(true);
for (StateListener l : stateListeners)
l.onAudioSettingsChanged();
}
} catch (Exception x) {
if (BuildVars.LOGS_ENABLED) {
FileLog.e("error initializing voip controller", x);
}
callFailed();
}
}
private void initNotification() {
notiMediaplayer = new SoundPool(3, AudioManager.STREAM_SYSTEM, 5);
notiSoundPoolID = notiMediaplayer.load(this, R.raw.crystalring, 1);
notiVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
}
protected void createSoundPool() {
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 100);
mBeepId = mSoundPool.load(this, R.raw.beep, 1);
}
@Override
public void onCreate() {
super.onCreate();
if (BuildVars.LOGS_ENABLED) {
FileLog.d("=============== VoIPService STARTING ===============");
}
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)!=null) {
int outFramesPerBuffer = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
VoIPController.setNativeBufferSize(outFramesPerBuffer);
} else {
VoIPController.setNativeBufferSize(AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT) / 2);
}
try {
cpuWakelock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "telegram-voip");
cpuWakelock.acquire();
btAdapter=am.isBluetoothScoAvailableOffCall() ? BluetoothAdapter.getDefaultAdapter() : null;
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
if(!USE_CONNECTION_SERVICE){
filter.addAction(ACTION_HEADSET_PLUG);
if(btAdapter!=null){
filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
}
filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
}
registerReceiver(receiver, filter);
soundPool = new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
spConnectingId = soundPool.load(this, R.raw.voip_connecting, 1);
spRingbackID = soundPool.load(this, R.raw.voip_ringback, 1);
spFailedID = soundPool.load(this, R.raw.voip_failed, 1);
spEndId = soundPool.load(this, R.raw.voip_end, 1);
spBusyId = soundPool.load(this, R.raw.voip_busy, 1);
am.registerMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));
if(!USE_CONNECTION_SERVICE && btAdapter!=null && btAdapter.isEnabled()){
int headsetState=btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
updateBluetoothHeadsetState(headsetState==BluetoothProfile.STATE_CONNECTED);
//if(headsetState==BluetoothProfile.STATE_CONNECTED)
// am.setBluetoothScoOn(true);
for (StateListener l : stateListeners)
l.onAudioSettingsChanged();
}
} catch (Exception x) {
if (BuildVars.LOGS_ENABLED) {
FileLog.e("error initializing voip controller", x);
}
callFailed();
}
}
public int getSoundId(Context context, SoundPool pool) {
return pool.load(context, getSoundRes(), 1);
}
private void loadSoundRes(Context context, SoundPool soundPool) {
soundId = soundPool.load(context, soundRes, 1);
}
public void initSoundPool() {
soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
soundPool.load(service, R.raw.instant_message, 1);
}
public Box2DContactListener(Context context_)
{
soundpool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
sound_id = soundpool.load(context_, R.raw.bounce,1);
}
@Override
public void onCreate() {
super.onCreate();
if (BuildVars.LOGS_ENABLED) {
FileLog.d("=============== VoIPService STARTING ===============");
}
try {
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)!=null) {
int outFramesPerBuffer = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
TgVoip.setBufferSize(outFramesPerBuffer);
} else {
TgVoip.setBufferSize(AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT) / 2);
}
cpuWakelock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "telegram-voip");
cpuWakelock.acquire();
btAdapter=am.isBluetoothScoAvailableOffCall() ? BluetoothAdapter.getDefaultAdapter() : null;
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
if(!USE_CONNECTION_SERVICE){
filter.addAction(ACTION_HEADSET_PLUG);
if(btAdapter!=null){
filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
}
filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
}
registerReceiver(receiver, filter);
soundPool = new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
spConnectingId = soundPool.load(this, R.raw.voip_connecting, 1);
spRingbackID = soundPool.load(this, R.raw.voip_ringback, 1);
spFailedID = soundPool.load(this, R.raw.voip_failed, 1);
spEndId = soundPool.load(this, R.raw.voip_end, 1);
spBusyId = soundPool.load(this, R.raw.voip_busy, 1);
am.registerMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));
if(!USE_CONNECTION_SERVICE && btAdapter!=null && btAdapter.isEnabled()){
int headsetState=btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
updateBluetoothHeadsetState(headsetState==BluetoothProfile.STATE_CONNECTED);
//if(headsetState==BluetoothProfile.STATE_CONNECTED)
// am.setBluetoothScoOn(true);
for (StateListener l : stateListeners)
l.onAudioSettingsChanged();
}
} catch (Exception x) {
if (BuildVars.LOGS_ENABLED) {
FileLog.e("error initializing voip controller", x);
}
callFailed();
}
}
public SoundHelper(Context context){
mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
soundIdRedEnvelopeComing = mSoundPool.load(context, R.raw.red_envelope_coming, 1);
}