Last active
September 12, 2022 20:11
-
-
Save gajerarajnit/b603e6968329538b0f286586f059e7f4 to your computer and use it in GitHub Desktop.
Singleton MediaPlayer.
This file contains 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
import android.content.Context; | |
import android.media.MediaPlayer; | |
import android.net.Uri; | |
import java.io.IOException; | |
/** | |
* Created by rajnit on 10/07/17. | |
*/ | |
public final class MyMediaPlayer { | |
public final static String APP_RAW_URI_PATH_1 = String.format("android.resource://%s/raw/", BuildConfig.APPLICATION_ID); | |
private static final String TAG = "MyMediaPlayer"; | |
private static volatile MyMediaPlayer instance = null; | |
MediaPlayer mp; | |
private Context context; | |
private MyMediaPlayer(Context context) { | |
this.context = context; | |
} | |
public static MyMediaPlayer getInstance(Context context) { | |
if (instance == null) { | |
synchronized (MyMediaPlayer.class) { | |
if (instance == null) { | |
instance = new MyMediaPlayer(context); | |
} | |
} | |
} | |
return instance; | |
} | |
/** | |
* | |
* @param fileName if sound name is "sound.mp3" then pass fileName as "sound" only. | |
*/ | |
public synchronized void playSound(String fileName) { | |
if (instance.mp == null) { | |
instance.mp = new MediaPlayer(); | |
} else { | |
instance.mp.reset(); | |
} | |
try { | |
instance.mp.setDataSource(context, Uri.parse(APP_RAW_URI_PATH_1 + fileName)); | |
instance.mp.prepare(); | |
instance.mp.setVolume(100f, 100f); | |
instance.mp.setLooping(false); | |
instance.mp.start(); | |
instance.mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { | |
@Override | |
public void onCompletion(MediaPlayer mp) { | |
L.print("completeSound: " + fileName); | |
if (instance.mp != null) { | |
instance.mp.reset(); | |
instance.mp = null; | |
} | |
} | |
}); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public synchronized void stopSound() { | |
if (instance.mp != null) { | |
instance.mp.stop(); | |
instance.mp.release(); | |
} | |
} | |
public synchronized void pauseSound() { | |
if (instance.mp != null) { | |
instance.mp.pause(); | |
} | |
} | |
public synchronized void restartSound() { | |
if (instance.mp != null) { | |
instance.mp.start(); | |
} | |
} | |
public synchronized void playRepeatedSound(String fileName) { | |
if (instance.mp == null) { | |
instance.mp = new MediaPlayer(); | |
} else { | |
instance.mp.reset(); | |
} | |
try { | |
instance.mp.setDataSource(context, Uri.parse(APP_RAW_URI_PATH_1 + fileName)); | |
instance.mp.prepare(); | |
instance.mp.setVolume(100f, 100f); | |
instance.mp.setLooping(true); | |
instance.mp.start(); | |
instance.mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { | |
@Override | |
public void onCompletion(MediaPlayer mp) { | |
if (mp != null) { | |
mp.reset(); | |
mp = null; | |
} | |
} | |
}); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment