android.media.SoundPool Java Examples
The following examples show how to use
android.media.SoundPool.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Cocos2dxSound.java From Example-of-Cocos2DX with MIT License | 6 votes |
@Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { if (status == 0) { // only play effect that are in mEffecToPlayWhenLoadedArray for ( SoundInfoForLoadedCompleted info : mEffecToPlayWhenLoadedArray) { if (sampleId == info.soundID) { // set the stream id which will be returned by playEffect() mStreamIdSyn = doPlayEffect(info.path, info.soundID, info.isLoop, info.pitch, info.pan, info.gain); // remove it from array, because we will break here // so it is safe to do mEffecToPlayWhenLoadedArray.remove(info); break; } } } else { mStreamIdSyn = Cocos2dxSound.INVALID_SOUND_ID; } mSemaphore.release(); }
Example #2
Source File: CameraActivity.java From vocefiscal-android with Apache License 2.0 | 6 votes |
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); }
Example #3
Source File: TimerView.java From gdk-timer-sample with Apache License 2.0 | 6 votes |
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); }
Example #4
Source File: CaptureActivity.java From AndroidWallet with GNU General Public License v3.0 | 6 votes |
/** * 初始化音效 */ private void initSound() { mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); AssetFileDescriptor descriptor = null; try { descriptor = getResources().openRawResourceFd(R.raw.beep); mSoundId = mSoundPool.load(descriptor, 1); } finally { if (descriptor != null) { try { descriptor.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Example #5
Source File: CodeGenerateActivity.java From Ticket-Analysis with MIT License | 6 votes |
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(); } }
Example #6
Source File: FeedbackController.java From brailleback with Apache License 2.0 | 6 votes |
/** * Constructs and initializes a new feedback controller. */ public FeedbackController(Context context) { mContext = context; mResources = context.getResources(); mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); mSoundPool = new SoundPool(NUMBER_OF_CHANNELS, DEFAULT_STREAM, 1); mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { if (status == 0) { synchronized (mPostLoadPlayables) { if (mPostLoadPlayables.contains(sampleId)) { soundPool.play( sampleId, DEFAULT_VOLUME, DEFAULT_VOLUME, 1, 0, DEFAULT_RATE); mPostLoadPlayables.remove(Integer.valueOf(sampleId)); } } } } }); mHandler = new Handler(); mResourceIdToSoundMap.clear(); mResourceIdToVibrationPatternMap.clear(); MidiUtils.purgeMidiTempFiles(context); }
Example #7
Source File: AndroidAudioRenderer.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
@Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { AudioNode src = mapLoadingAudioNodes.get(sampleId); if (src.getAudioData() instanceof AndroidAudioData) { AndroidAudioData audioData = (AndroidAudioData) src.getAudioData(); if (status == 0) // load was successfull { int channelIndex; channelIndex = soundPool.play(audioData.getId(), 1f, 1f, 1, -1, 1f); src.setChannel(channelIndex); // Playing started ? if (src.getChannel() > 0) { src.setStatus(Status.Playing); } } else { src.setChannel(-1); } } else { throw new IllegalArgumentException("AudioData is not of type AndroidAudioData for AudioNode " + src.toString()); } }
Example #8
Source File: SoundEngine.java From homescreenarcade with GNU General Public License v3.0 | 6 votes |
public SoundEngine(Context context) { this.context = context; sounds = new SoundPool(5, AudioManager.STREAM_RING, 0); soundsMap = new HashMap<Integer, Integer>(); soundsMap.put(EATFOOD, sounds.load(context, R.raw.pacmon_waka_waka, 1)); soundsMap.put(EATGHOST, sounds.load(context, R.raw.pacmon_eating_ghost, 1)); soundsMap.put(DIE, sounds.load(context, R.raw.pacmon_dies, 1)); soundsMap.put(READY, sounds.load(context, R.raw.pacmon_opening_song,1)); soundsMap.put(GAMEOVER, sounds.load(context, R.raw.pacmon_opening_song, 1)); //soundsMap.put(EATCHERRY, sounds.load(context, R.raw.pacmon_eating_cherry, 1)); AudioManager mgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // the music that is played at the beginning // music = MediaPlayer.create(context, R.raw.gameplaymusic); // music.setLooping(true); }
Example #9
Source File: Cocos2dxSound.java From Earlybird with Apache License 2.0 | 6 votes |
@Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { if (status == 0) { // only play effect that are in mEffecToPlayWhenLoadedArray for ( SoundInfoForLoadedCompleted info : mEffecToPlayWhenLoadedArray) { if (sampleId == info.soundID) { // set the stream id which will be returned by playEffect() mStreamIdSyn = doPlayEffect(info.path, info.soundID, info.isLoop, info.pitch, info.pan, info.gain); // remove it from array, because we will break here // so it is safe to do mEffecToPlayWhenLoadedArray.remove(info); break; } } } else { mStreamIdSyn = Cocos2dxSound.INVALID_SOUND_ID; } mSemaphore.release(); }
Example #10
Source File: AudioAction.java From ssj with GNU General Public License v3.0 | 6 votes |
public void registerWithPlayer(SoundPool player) { try { if(fd != null) { soundId = player.load(fd, 1); fd.close(); } else { soundId = player.load(res, 1); } } catch (IOException e) { Log.e("error loading audio files", e); } }
Example #11
Source File: MainActivity.java From Android-9-Development-Cookbook with MIT License | 6 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button button1 = findViewById(R.id.button1); button1.setEnabled(false); final Button button2 = findViewById(R.id.button2); button2.setEnabled(false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { createSoundPoolNew(); } else { createSoundPoolOld(); } mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { button1.setEnabled(true); button2.setEnabled(true); } }); mHashMap = new HashMap<>(); mHashMap.put(1, mSoundPool.load(this, R.raw.sound_1, 1)); mHashMap.put(2, mSoundPool.load(this, R.raw.sound_2, 1)); }
Example #12
Source File: United.java From United4 with GNU General Public License v3.0 | 6 votes |
/** * Makes a new sound pool, loads the requested sound and plays it once it's loaded * Could be made a LOT better by reusing the same pool and checking if it's already loaded * * @param file the sound to play */ public static void playSound(String file) { if (P.getBool("mute_sounds")) return; SoundPool pool = buildPool(); try { AssetFileDescriptor fd = getContext().getAssets().openFd(file); pool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int i, int i1) { soundPool.play(i, 1, 1, 1, 0, 1); } }); pool.load(fd, 1); } catch (Exception ignored) { // } }
Example #13
Source File: TwoDScrollerListview.java From 2DScroller with Apache License 2.0 | 6 votes |
/** * To play sound. * * @param context * {@link Context} * * @param soundID * sound id. * * @param soundPool * {@link SoundPool} instance. * */ public void playSound(Context context, int soundID, SoundPool soundPool){ OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){ // Lower the volume } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { // Raise it back to normal } } }; AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); // Request audio focus for playback int result = audioManager.requestAudioFocus(afChangeListener, // Use the music stream. AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { // Start playback. soundPool.play(soundID, 10, 10, 1, 0,1f ); } }
Example #14
Source File: FeedbackController.java From talkback with Apache License 2.0 | 5 votes |
private static SoundPool createSoundPool() { AudioAttributes aa = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .build(); return new SoundPool.Builder().setMaxStreams(MAX_STREAMS).setAudioAttributes(aa).build(); }
Example #15
Source File: PlayManager.java From Rainville with MIT License | 5 votes |
/** * 根据新的 SDK 文档说明,尽可能使用新的 Build 声明方式 */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) protected void createNewSoundPool() { AudioAttributes attributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .build(); mSoundPool = new SoundPool.Builder() .setMaxStreams(MAX_TRACKS_NUM) .setAudioAttributes(attributes) .build(); }
Example #16
Source File: Cocos2dxSound.java From Example-of-Cocos2DX with MIT License | 5 votes |
private void initData() { this.mSoundPool = new SoundPool(Cocos2dxSound.MAX_SIMULTANEOUS_STREAMS_DEFAULT, AudioManager.STREAM_MUSIC, Cocos2dxSound.SOUND_QUALITY); this.mSoundPool.setOnLoadCompleteListener(new OnLoadCompletedListener()); this.mLeftVolume = 0.5f; this.mRightVolume = 0.5f; this.mSemaphore = new Semaphore(0, true); }
Example #17
Source File: CannonView.java From CSCI4669-Fall15-Android with Apache License 2.0 | 5 votes |
public CannonView(Context context, AttributeSet attrs) { super(context, attrs); // call superclass constructor activity = (Activity) context; // store reference to MainActivity // register SurfaceHolder.Callback listener getHolder().addCallback(this); // initialize Lines and Point representing game items blocker = new Line(); // create the blocker as a Line target = new Line(); // create the target as a Line cannonball = new Point(); // create the cannonball as a Point // initialize hitStates as a boolean array hitStates = new boolean[TARGET_PIECES]; // initialize SoundPool to play the app's three sound effects soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); // create Map of sounds and pre-load sounds soundMap = new SparseIntArray(3); // create new HashMap soundMap.put(TARGET_SOUND_ID, soundPool.load(context, R.raw.target_hit, 1)); soundMap.put(CANNON_SOUND_ID, soundPool.load(context, R.raw.cannon_fire, 1)); soundMap.put(BLOCKER_SOUND_ID, soundPool.load(context, R.raw.blocker_hit, 1)); // construct Paints for drawing text, cannonball, cannon, // blocker and target; these are configured in method onSizeChanged textPaint = new Paint(); cannonPaint = new Paint(); cannonballPaint = new Paint(); blockerPaint = new Paint(); targetPaint = new Paint(); backgroundPaint = new Paint(); }
Example #18
Source File: CannonView.java From CSCI4669-Fall15-Android with Apache License 2.0 | 5 votes |
public CannonView(Context context, AttributeSet attrs) { super(context, attrs); // call superclass constructor activity = (Activity) context; // store reference to MainActivity // register SurfaceHolder.Callback listener getHolder().addCallback(this); // initialize Lines and Point representing game items blocker = new Line(); // create the blocker as a Line target = new Line(); // create the target as a Line cannonball = new Point(); // create the cannonball as a Point // initialize hitStates as a boolean array hitStates = new boolean[TARGET_PIECES]; // initialize SoundPool to play the app's three sound effects soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); // create Map of sounds and pre-load sounds soundMap = new SparseIntArray(3); // create new HashMap soundMap.put(TARGET_SOUND_ID, soundPool.load(context, R.raw.target_hit, 1)); soundMap.put(CANNON_SOUND_ID, soundPool.load(context, R.raw.cannon_fire, 1)); soundMap.put(BLOCKER_SOUND_ID, soundPool.load(context, R.raw.blocker_hit, 1)); // construct Paints for drawing text, cannonball, cannon, // blocker and target; these are configured in method onSizeChanged textPaint = new Paint(); cannonPaint = new Paint(); cannonballPaint = new Paint(); blockerPaint = new Paint(); targetPaint = new Paint(); backgroundPaint = new Paint(); }
Example #19
Source File: SoundManager.java From tilt-game-android with MIT License | 5 votes |
@Override public synchronized void onLoadComplete(final SoundPool pSoundPool, final int pSoundID, final int pStatus) { if (pStatus == SoundManager.SOUND_STATUS_OK) { final Sound sound = this.mSoundMap.get(pSoundID); if (sound == null) { throw new SoundException("Unexpected soundID: '" + pSoundID + "'."); } else { sound.setLoaded(true); } } }
Example #20
Source File: SoundManager.java From tilt-game-android with MIT License | 5 votes |
/** * If the sound is being loaded for the first time, we should wait until it * is completely loaded to play it. */ @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { /* int streamId = _soundPool.play(sampleId, LEFT_VOLUME, RIGHT_VOLUME, STREAM_PRIORITY, LOOP_MODE, PLAYBACK_RATE); _streamIdMap.put(sampleId, streamId); */ }
Example #21
Source File: Game.java From retrobreaker with MIT License | 5 votes |
public Game(Context context) { mContext = context; // Load sound pool, audio shouldn't change between levels mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); mSoundIds = new HashMap<>(4); mSoundIds.put("lost_life", mSoundPool.load(mContext, R.raw.lost_life, 1)); mSoundIds.put("wall_hit", mSoundPool.load(mContext, R.raw.wall_hit, 1)); mSoundIds.put("paddle_hit", mSoundPool.load(mContext, R.raw.paddle_hit, 1)); mSoundIds.put("brick_hit", mSoundPool.load(mContext, R.raw.brick_hit, 1)); mSoundIds.put("explosive_brick", mSoundPool.load(mContext, R.raw.explosive_brick, 1)); // Create level elements resetElements(); }
Example #22
Source File: Auditory.java From ssj with GNU General Public License v3.0 | 5 votes |
protected void load(XmlPullParser xml, final Context context) { super.load(xml, context); player = new SoundPool(4, AudioManager.STREAM_NOTIFICATION, 0); ((AudioAction) action).registerWithPlayer(player); }
Example #23
Source File: SoundPoolBuilder.java From soundboard with GNU General Public License v3.0 | 5 votes |
public static SoundPool build() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return buildModern(); } else { return buildLegacy(); } }
Example #24
Source File: TriggerHelper.java From science-journal with Apache License 2.0 | 5 votes |
public void doAudioAlert(Context context) { // Use a throttler to keep this from interrupting itself too much. if (SystemClock.elapsedRealtime() - lastAudioMs < THROTTLE_LIMIT_MS) { return; } SoundPool soundPool = getSoundPool(context); if (soundId != -1) { lastAudioMs = SystemClock.elapsedRealtime(); soundPool.play(soundId, .8f, .8f, 0, 0, 1.0f); } }
Example #25
Source File: MediaController.java From talk-android with MIT License | 5 votes |
private MediaController() { try { sensorManager = (SensorManager) MainApp.CONTEXT.getSystemService(Context.SENSOR_SERVICE); proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); soundEffectId = soundPool.load(MainApp.CONTEXT, R.raw.add, 1); } catch (Exception e) { Logger.e(TAG, "Sensor init error", e); } }
Example #26
Source File: SoundPoolUtils.java From android-project-wo2b with Apache License 2.0 | 5 votes |
/** * 初始化音频管理器 * * @param context */ private void initAudioManager(Context context) { mContext = context.getApplicationContext(); mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); mSoundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0); mSoundPoolMap = new HashMap<String, Integer>(); }
Example #27
Source File: AuditoryFeedback.java From ssj with GNU General Public License v3.0 | 5 votes |
@Override public void enterFeedback() throws SSJFatalException { if (_evchannel_in == null || _evchannel_in.size() == 0) { throw new SSJFatalException("no input channels"); } player = new SoundPool(4, AudioManager.STREAM_NOTIFICATION, 0); soundId = player.load(options.audioFile.get().value, 1); }
Example #28
Source File: SoundManagerImpl.java From mirror with Apache License 2.0 | 5 votes |
@Override public void onPlugged(PluginBus bus) { super.onPlugged(bus); mSoundPool = new SoundPool.Builder().setAudioAttributes( new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_NOTIFICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .build() ).build(); mAudioManager = D.get(AudioManager.class); mAcknowledgeId = mSoundPool.load(mAppManager.getAppContext(), R.raw.acknowledge, 1); mErrorId = mSoundPool.load(mAppManager.getAppContext(), R.raw.error, 1); }
Example #29
Source File: GameView.java From aws-mobile-self-paced-labs-samples with Apache License 2.0 | 5 votes |
public void loadSounds() { // Sounds sounds = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); biteSound = sounds.load(getContext(), R.raw.apple, 1); crashSound = sounds.load(getContext(), R.raw.crash, 1); appearSound = sounds.load(getContext(), R.raw.appear, 1); }
Example #30
Source File: MediaUtil.java From appinventor-extensions with Apache License 2.0 | 5 votes |
/** * 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 + "."); }