Created
April 2, 2017 20:30
-
-
Save akexorcist/94afbd551cd9527667be97e06d6377c0 to your computer and use it in GitHub Desktop.
Singleton Sound Player in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SoundPoolManager.getInstance().play(getContext(), R.raw.any_sound); | |
MediaPlayerManager.getInstance().play(getContext(), R.raw.any_sound); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.akexorcist.myapplication; | |
import android.content.Context; | |
import android.media.MediaPlayer; | |
/** | |
* Created by Akexorcist on 4/3/2017 AD. | |
*/ | |
public class MediaPlayerManager { | |
private static MediaPlayerManager manager; | |
public static MediaPlayerManager getInstance() { | |
if (manager == null) { | |
manager = new MediaPlayerManager(); | |
} | |
return manager; | |
} | |
private MediaPlayer mediaPlayer; | |
public void play(Context context, int resId) { | |
play(context, resId, null, null); | |
} | |
public void play(Context context, int resId, MediaPlayer.OnCompletionListener completionListener) { | |
play(context, resId, completionListener, null); | |
} | |
public void play(Context context, int resId, MediaPlayer.OnErrorListener errorListener) { | |
play(context, resId, null, errorListener); | |
} | |
public void play(Context context, int resId, MediaPlayer.OnCompletionListener completionListener, MediaPlayer.OnErrorListener errorListener) { | |
if (mediaPlayer != null) { | |
if (mediaPlayer.isPlaying()) { | |
mediaPlayer.stop(); | |
} | |
mediaPlayer.release(); | |
mediaPlayer = null; | |
} | |
mediaPlayer = MediaPlayer.create(context.getApplicationContext(), resId); | |
mediaPlayer.setOnCompletionListener(completionListener); | |
mediaPlayer.setOnErrorListener(errorListener); | |
mediaPlayer.start(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.akexorcist.myapplication; | |
import android.content.Context; | |
import android.media.AudioManager; | |
import android.media.SoundPool; | |
/** | |
* Created by Akexorcist on 4/3/2017 AD. | |
*/ | |
public class SoundPoolManager { | |
private static SoundPoolManager manager; | |
public static SoundPoolManager getInstance() { | |
if (manager == null) { | |
manager = new SoundPoolManager(); | |
} | |
return manager; | |
} | |
private SoundPool soundPool; | |
private int currentSoundId = -1; | |
public SoundPoolManager() { | |
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); | |
} | |
public void play(Context context, int resid) { | |
play(context, resid); | |
} | |
public void play(Context context, int resId, SoundPool.OnLoadCompleteListener listener) { | |
if (currentSoundId != -1) { | |
soundPool.stop(currentSoundId); | |
} | |
soundPool.load(context.getApplicationContext(), resId, 0); | |
soundPool.play(currentSoundId, 1, 1, 1, 0, 1); | |
soundPool.setOnLoadCompleteListener(listener); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At line 29 StackOverFlow i believe? There should be last parameter with null as listener?