SimplePlayerpublic 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 void | attachAuxEffect(int effectId)
mEffectId = effectId;
if (mMediaPlayer != null) {
Log.d(TAG,"attach effect: "+effectId);
mMediaPlayer.attachAuxEffect(effectId);
}
| public boolean | isPlaying()
if (mMediaPlayer != null) {
return mMediaPlayer.isPlaying();
} else {
return false;
}
| public void | onClick(android.view.View v)
if (v.getId() == mPlayPauseButtonId) {
playOrPause();
} else if (v.getId() == mStopButtonId) {
stop();
}
| public void | playOrPause()
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 void | set(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 void | setAuxEffectSendLevel(float level)
mSendLevel = level;
if (mMediaPlayer != null) {
mMediaPlayer.setAuxEffectSendLevel(level);
}
| public void | setContext(android.content.Context context)
mContext = context;
| public void | stop()
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
updatePlayPauseButton();
| public void | updatePlayPauseButton()
mPlayPauseButton.setImageResource(isPlaying() ? mPauseImageResource : mPlayImageResource);
|
|