Created
February 4, 2016 14:13
-
-
Save korrio/58223c4b02d10683c583 to your computer and use it in GitHub Desktop.
MediaManager.java
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
| public class MediaManager { | |
| private static MediaPlayer mMediaPlayer; | |
| private static boolean isPause; | |
| public static void playSound(String filePath, | |
| OnCompletionListener onCompletionListener) { | |
| if(mMediaPlayer==null) | |
| { | |
| mMediaPlayer=new MediaPlayer(); | |
| mMediaPlayer.setOnErrorListener(new OnErrorListener() { | |
| @Override | |
| public boolean onError(MediaPlayer mp, int what, int extra) { | |
| mMediaPlayer.reset(); | |
| return false; | |
| } | |
| }); | |
| }else { | |
| mMediaPlayer.reset(); | |
| } | |
| mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); | |
| mMediaPlayer.setOnCompletionListener(onCompletionListener); | |
| try { | |
| mMediaPlayer.setDataSource(filePath); | |
| mMediaPlayer.prepare(); | |
| } catch (Exception e) { | |
| // TODO: handle exception | |
| } | |
| mMediaPlayer.start(); | |
| } | |
| public static void pause() | |
| { | |
| if(mMediaPlayer!=null&&mMediaPlayer.isPlaying()) | |
| { | |
| mMediaPlayer.pause(); | |
| isPause=true; | |
| } | |
| } | |
| public static void resume() { | |
| if(mMediaPlayer!=null&&isPause) | |
| { | |
| mMediaPlayer.start(); | |
| isPause=false; | |
| } | |
| } | |
| public static void release() { | |
| if(mMediaPlayer!=null) | |
| { | |
| mMediaPlayer.release(); | |
| mMediaPlayer=null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment