FileDocCategorySizeDatePackage
SimplePlayer.javaAPI DocAndroid 5.1 API7162Thu Mar 12 22:22:30 GMT 2015com.android.effectstest

SimplePlayer

public class SimplePlayer extends Object implements android.view.View.OnClickListener

Fields Summary
private static final String
TAG
int
mPlayPauseButtonId
int
mStopButtonId
android.content.Context
mContext
android.widget.ImageView
mPlayPauseButton
int
mPlayImageResource
int
mPauseImageResource
String
mFileName
int
mFileResId
android.media.MediaPlayer
mMediaPlayer
int
mStreamType
int
mSession
float
mSendLevel
int
mEffectId
android.widget.TextView
mSessionText
Constructors Summary
SimplePlayer(android.content.Context context, int playPausebuttonId, android.widget.ImageView playPausebutton, int stopButtonId, android.widget.ImageView stopButton, android.widget.TextView sessionText, String fileName, int stream, int session)


         
                           
    
        set(context, playPausebuttonId, playPausebutton, stopButtonId, stopButton, sessionText, stream, session);
        mFileName = fileName;
    
SimplePlayer(android.content.Context context, int playPausebuttonId, android.widget.ImageView playPausebutton, int stopButtonId, android.widget.ImageView stopButton, android.widget.TextView sessionText, int fileResId, int stream, int session)

        set(context, playPausebuttonId, playPausebutton, stopButtonId, stopButton, sessionText, stream, session);
        mFileResId = fileResId;
        mFileName = "";
    
Methods Summary
public voidattachAuxEffect(int effectId)

        mEffectId = effectId;
        if (mMediaPlayer != null) {
            Log.d(TAG,"attach effect: "+effectId);
            mMediaPlayer.attachAuxEffect(effectId);
        }
    
public booleanisPlaying()

        if (mMediaPlayer != null) {
            return mMediaPlayer.isPlaying();
        } else {
            return false;
        }
    
public voidonClick(android.view.View v)

        if (v.getId() == mPlayPauseButtonId) {
            playOrPause();
        } else if (v.getId() == mStopButtonId) {
            stop();
        }
    
public voidplayOrPause()

        if (mMediaPlayer == null || !mMediaPlayer.isPlaying()){
              if (mMediaPlayer == null) {
                  try {
                      mMediaPlayer = new MediaPlayer();
                      if (mSession != 0) {
                          mMediaPlayer.setAudioSessionId(mSession);
                          Log.d(TAG, "mMediaPlayer.setAudioSessionId(): "+ mSession);
                      }

                      if (mFileName.equals("")) {
                          Log.d(TAG, "Playing from resource");
                          AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(mFileResId);
                          mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                          afd.close();
                      } else {
                          Log.d(TAG, "Playing file: "+mFileName);
                          mMediaPlayer.setDataSource(mFileName);
                      }
                      mMediaPlayer.setAudioStreamType(mStreamType);
                      mMediaPlayer.prepare();
                      mMediaPlayer.setLooping(true);
                  } catch (IOException ex) {
                      Log.e(TAG, "mMediaPlayercreate failed:", ex);
                      mMediaPlayer = null;
                  } catch (IllegalArgumentException ex) {
                      Log.e(TAG, "mMediaPlayercreate failed:", ex);
                      mMediaPlayer = null;
                  } catch (SecurityException ex) {
                      Log.e(TAG, "mMediaPlayercreate failed:", ex);
                      mMediaPlayer = null;
                  }

                  if (mMediaPlayer != null) {
                      mMediaPlayer.setAuxEffectSendLevel(mSendLevel);
                      mMediaPlayer.attachAuxEffect(mEffectId);
                      mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                          public void onCompletion(MediaPlayer mp) {
                              updatePlayPauseButton();
                          }
                      });
                      mSessionText.setText("Session: "+Integer.toString(mMediaPlayer.getAudioSessionId()));
                  }
              }
              if (mMediaPlayer != null) {
                  mMediaPlayer.start();
              }
          } else {
              mMediaPlayer.pause();
          }
          updatePlayPauseButton();
    
public voidset(android.content.Context context, int playPausebuttonId, android.widget.ImageView playPausebutton, int stopButtonId, android.widget.ImageView stopButton, android.widget.TextView sessionText, int stream, int session)

        mContext = context;
        mPlayPauseButtonId = playPausebuttonId;
        mStopButtonId = stopButtonId;
        mPlayPauseButton = (ImageButton) playPausebutton;
        ImageButton stop = (ImageButton) stopButton;

        mPlayPauseButton.setOnClickListener(this);
        mPlayPauseButton.requestFocus();
        stop.setOnClickListener(this);

        mPlayImageResource = android.R.drawable.ic_media_play;
        mPauseImageResource = android.R.drawable.ic_media_pause;
        mStreamType = stream;
        mSession = session;
        mSessionText = sessionText;
    
public voidsetAuxEffectSendLevel(float level)

        mSendLevel = level;
        if (mMediaPlayer != null) {
            mMediaPlayer.setAuxEffectSendLevel(level);
        }
    
public voidsetContext(android.content.Context context)

        mContext = context;
    
public voidstop()

      if (mMediaPlayer != null) {
          mMediaPlayer.stop();
          mMediaPlayer.release();
          mMediaPlayer = null;
      }
      updatePlayPauseButton();
    
public voidupdatePlayPauseButton()

        mPlayPauseButton.setImageResource(isPlaying() ? mPauseImageResource : mPlayImageResource);