Advertisement
mousa_mk

MediaPlayerWrapper class

Mar 17th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. public class MediaPlayerWrapper
  2. {
  3.     private static final String LOG_TAG = "MediaPlayerWrapper";
  4.     private String mFilename;
  5.     PlayerState mState = PlayerState.Stopped;
  6.     MediaPlayer mPlayerEngine;
  7.     int mStartTime;
  8.     int mStopTime;
  9.     int mVolume;
  10.     Handler mHandler = new Handler();
  11.    
  12.    
  13.     Runnable mStopPlayerTask = new Runnable(){
  14.         @Override
  15.         public void run() {
  16.             stop(PlayStopReason.Finished);
  17.         }};
  18.    
  19.    
  20.     public MediaPlayerWrapper(Context context)
  21.     {
  22.     }
  23.    
  24.    
  25.     public void play(AudioClip clip)
  26.              throws IllegalStateException, IllegalArgumentException, SecurityException, IOException
  27.     {
  28.         Log.v(LOG_TAG, "play()");
  29.         mPlayerEngine = new MediaPlayer();
  30.         setAudioClip(clip);
  31.         mPlayerEngine.start();
  32.         mState = PlayerState.Playing;
  33.         mHandler.postDelayed(mStopPlayerTask, clip.getStopTime()-clip.getStartTime());
  34.     }
  35.    
  36.    
  37.     public void stop(PlayStopReason reason)
  38.     {
  39.         Log.v(LOG_TAG, "stop()");
  40.         if (PlayerState.Paused == mState || PlayerState.Playing == mState)
  41.         {
  42.             Log.d(LOG_TAG, "Stopping player...");
  43.             mHandler.removeCallbacks(mStopPlayerTask);
  44.             mPlayerEngine.stop();
  45.             mState = PlayerState.Stopped;
  46.         }
  47.     }
  48.    
  49.    
  50.     private void setAudioClip(AudioClip clip)
  51.             throws IllegalStateException, IllegalArgumentException, SecurityException, IOException
  52.     {
  53.         Log.v(LOG_TAG, "setAudioClip()");
  54.         mFilename = clip.getSource();
  55.         if (null == mFilename || mFilename.equals(""))
  56.         {
  57.             String text = "The given audio file name is empty!";
  58.             Log.e(LOG_TAG, text);
  59.             throw new IllegalStateException(text);
  60.         }
  61.         File f = new File(mFilename);
  62.         if (!f.exists())
  63.         {
  64.             String text = "The specified file [" + mFilename + "] does not exist!";
  65.             Log.e(LOG_TAG, "text");
  66.             throw new FileNotFoundException(text);
  67.         }
  68.         try
  69.         {
  70.             mPlayerEngine.setDataSource(mFilename);
  71.             mPlayerEngine.prepare();
  72.         }catch(Exception ex)
  73.         {
  74.             Log.e(LOG_TAG, "An error happened preparing media player: " + ex.getMessage());
  75.             ex.printStackTrace();
  76.         }
  77.         mStartTime = clip.getStartTime();
  78.         mStopTime = clip.getStopTime();
  79.         setPosition(mStartTime);
  80.     }
  81.    
  82.    
  83.     public void pause()
  84.     {
  85.         if (PlayerState.Playing == mState)
  86.         {
  87.             mPlayerEngine.pause();
  88.             mHandler.removeCallbacks(mStopPlayerTask);
  89.             mState = PlayerState.Paused;
  90.         }
  91.     }
  92.    
  93.    
  94.     public void resume()
  95.     {
  96.         if (PlayerState.Paused == mState)
  97.         {
  98.             mHandler.postDelayed(mStopPlayerTask, mStopTime-mPlayerEngine.getCurrentPosition());
  99.             mPlayerEngine.start();
  100.             mState = PlayerState.Playing;
  101.         }
  102.     }
  103.    
  104.    
  105.     private void setPosition(int pos)
  106.     {
  107.         Log.v(LOG_TAG, "setPosition()");
  108.         mPlayerEngine.seekTo(pos);
  109.     }
  110.  
  111.  
  112.     @Override
  113.     public void playRequested(AudioClip clip)
  114.     {
  115.         try
  116.         {
  117.             play(clip);
  118.         } catch (Exception e)
  119.         {
  120.             //TODO: Inform user that the book is corrupted.
  121.             e.printStackTrace();
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement