Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.android;
- import android.content.Context;
- import android.media.AudioManager;
- import android.media.SoundPool;
- import android.media.SoundPool.OnLoadCompleteListener;
- public final class AlarmPlay {
- private static SoundPool sSoundPool;
- private static int sStreamId;
- private AlarmPlay() {
- // prevents instantiation by other packages.
- }
- public static void playAlarm(Context context) {
- sSoundPool = new SoundPool(1, AudioManager.STREAM_RING, 0);
- // PATH TO THE FILE HERE BUT NOT AN URI STRING!
- // BAD: content://media/external/audio/media/2
- // GOOD: /mnt/sdcard/MyMusic/music.mp3
- final int soundId = sSoundPool.load("PATH HERE", 0);
- sSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
- @Override
- public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
- sStreamId = sSoundPool.play(soundId, 1, 1, 0, -1, 1);
- }
- });
- }
- public static void stopPlayAlarm() {
- if (sStreamId != 0) sSoundPool.stop(sStreamId);
- if (sSoundPool != null) {
- sSoundPool.release();
- sSoundPool = null;
- }
- }
- }
Add Comment
Please, Sign In to add comment