Created
May 13, 2024 14:25
-
-
Save Raf0707/5b7964026fc19c9dd7995ab11b99bf83 to your computer and use it in GitHub Desktop.
StandartMediaPlayer
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.media.MediaPlayer; | |
public class StandartMediaPlayer { | |
private MediaPlayer mediaPlayer; | |
private int currentPosition; | |
static MediaPlayer instance; | |
public StandartMediaPlayer() { | |
mediaPlayer = new MediaPlayer(); | |
} | |
public void play(String filePath) { | |
try { | |
if (mediaPlayer.isPlaying()) { | |
mediaPlayer.stop(); | |
} | |
mediaPlayer.reset(); | |
mediaPlayer.setDataSource(filePath); | |
mediaPlayer.prepare(); | |
mediaPlayer.start(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void pause() { | |
if (mediaPlayer.isPlaying()) { | |
mediaPlayer.pause(); | |
currentPosition = mediaPlayer.getCurrentPosition(); | |
} | |
} | |
public void resume() { | |
if (!mediaPlayer.isPlaying()) { | |
mediaPlayer.seekTo(currentPosition); | |
mediaPlayer.start(); | |
} | |
} | |
public void stop() { | |
if (mediaPlayer.isPlaying()) { | |
mediaPlayer.stop(); | |
} | |
mediaPlayer.reset(); | |
currentPosition = 0; | |
} | |
public void release() { | |
stop(); | |
mediaPlayer.release(); | |
mediaPlayer = null; | |
} | |
public static MediaPlayer getInstance(){ | |
if(instance == null){ | |
instance = new MediaPlayer(); | |
} | |
return instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment