tester3000

AlarmPlay class using Android's Sound Pool

Apr 7th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. package com.example.android;
  2.  
  3. import android.content.Context;
  4. import android.media.AudioManager;
  5. import android.media.SoundPool;
  6. import android.media.SoundPool.OnLoadCompleteListener;
  7.  
  8. public final class AlarmPlay {
  9.    
  10.     private static SoundPool sSoundPool;
  11.     private static int sStreamId;
  12.  
  13.     private AlarmPlay() {
  14.         // prevents instantiation by other packages.
  15.     }
  16.    
  17.     public static void playAlarm(Context context) {
  18.         sSoundPool = new SoundPool(1, AudioManager.STREAM_RING, 0);
  19.        
  20.                 // PATH TO THE FILE HERE BUT NOT AN URI STRING!
  21.                 // BAD: content://media/external/audio/media/2
  22.                 // GOOD: /mnt/sdcard/MyMusic/music.mp3
  23.         final int soundId = sSoundPool.load("PATH HERE", 0);
  24.        
  25.         sSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
  26.            
  27.             @Override
  28.             public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
  29.                 sStreamId = sSoundPool.play(soundId, 1, 1, 0, -1, 1);  
  30.             }
  31.         });
  32.     }
  33.    
  34.     public static void stopPlayAlarm() {
  35.         if (sStreamId != 0) sSoundPool.stop(sStreamId);
  36.         if (sSoundPool != null) {
  37.             sSoundPool.release();
  38.             sSoundPool = null;
  39.         }
  40.     }
  41. }
Add Comment
Please, Sign In to add comment