use of android.media.MediaPlayer in project platform_frameworks_base by android.
the class SimplePlayer method playOrPause.
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();
}
use of android.media.MediaPlayer in project platform_frameworks_base by android.
the class MediaSource method setupMediaPlayer.
/** Creates a media player, sets it up, and calls prepare */
private synchronized boolean setupMediaPlayer(boolean useUrl) {
mPrepared = false;
mGotSize = false;
mPlaying = false;
mPaused = false;
mCompleted = false;
mNewFrameAvailable = false;
if (mLogVerbose)
Log.v(TAG, "Setting up playback.");
if (mMediaPlayer != null) {
// Clean up existing media players
if (mLogVerbose)
Log.v(TAG, "Resetting existing MediaPlayer.");
mMediaPlayer.reset();
} else {
// Create new media player
if (mLogVerbose)
Log.v(TAG, "Creating new MediaPlayer.");
mMediaPlayer = new MediaPlayer();
}
if (mMediaPlayer == null) {
throw new RuntimeException("Unable to create a MediaPlayer!");
}
// Set up data sources, etc
try {
if (useUrl) {
if (mLogVerbose)
Log.v(TAG, "Setting MediaPlayer source to URI " + mSourceUrl);
if (mContext == null) {
mMediaPlayer.setDataSource(mSourceUrl);
} else {
mMediaPlayer.setDataSource(mContext, Uri.parse(mSourceUrl.toString()));
}
} else {
if (mLogVerbose)
Log.v(TAG, "Setting MediaPlayer source to asset " + mSourceAsset);
mMediaPlayer.setDataSource(mSourceAsset.getFileDescriptor(), mSourceAsset.getStartOffset(), mSourceAsset.getLength());
}
} catch (IOException e) {
mMediaPlayer.release();
mMediaPlayer = null;
if (useUrl) {
throw new RuntimeException(String.format("Unable to set MediaPlayer to URL %s!", mSourceUrl), e);
} else {
throw new RuntimeException(String.format("Unable to set MediaPlayer to asset %s!", mSourceAsset), e);
}
} catch (IllegalArgumentException e) {
mMediaPlayer.release();
mMediaPlayer = null;
if (useUrl) {
throw new RuntimeException(String.format("Unable to set MediaPlayer to URL %s!", mSourceUrl), e);
} else {
throw new RuntimeException(String.format("Unable to set MediaPlayer to asset %s!", mSourceAsset), e);
}
}
mMediaPlayer.setLooping(mLooping);
mMediaPlayer.setVolume(mVolume, mVolume);
// Bind it to our media frame
Surface surface = new Surface(mSurfaceTexture);
mMediaPlayer.setSurface(surface);
surface.release();
// Connect Media Player to callbacks
mMediaPlayer.setOnVideoSizeChangedListener(onVideoSizeChangedListener);
mMediaPlayer.setOnPreparedListener(onPreparedListener);
mMediaPlayer.setOnCompletionListener(onCompletionListener);
// Connect SurfaceTexture to callback
mSurfaceTexture.setOnFrameAvailableListener(onMediaFrameAvailableListener);
if (mLogVerbose)
Log.v(TAG, "Preparing MediaPlayer.");
mMediaPlayer.prepareAsync();
return true;
}
use of android.media.MediaPlayer in project platform_frameworks_base by android.
the class CodecTest method playToEnd.
public static boolean playToEnd(String filePath) {
Log.v(TAG, "shortMediaStop - " + filePath);
//This test is only for the short media file
int duration = 200000;
int updateDuration = 0;
int currentPosition = 0;
boolean isPlaying = false;
MediaPlayer mp = new MediaPlayer();
try {
Thread.sleep(5000);
mp.setDataSource(filePath);
Log.v(TAG, "start playback");
mp.prepare();
//duration = mp.getDuration();
mp.start();
Thread.sleep(50000);
} catch (Exception e) {
}
isPlaying = mp.isPlaying();
currentPosition = mp.getCurrentPosition();
//updateDuration = mp.getDuration();
Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying);
mp.stop();
mp.release();
//Log.v(TAG, "Update duration = " + updateDuration);
if (currentPosition > duration || isPlaying)
return false;
else
return true;
}
use of android.media.MediaPlayer in project platform_frameworks_base by android.
the class MediaPlayerInvokeTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
mPlayer = new MediaPlayer();
}
use of android.media.MediaPlayer in project platform_frameworks_base by android.
the class VideoDumpView method onResume.
@Override
public void onResume() {
Log.d(TAG, "onResume");
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(VideoDumpConfig.VIDEO_URI);
class RGBFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(VideoDumpConfig.IMAGE_SUFFIX));
}
}
File dump_dir = new File(VideoDumpConfig.ROOT_DIR);
File[] dump_files = dump_dir.listFiles(new RGBFilter());
for (File dump_file : dump_files) {
dump_file.delete();
}
File image_list = new File(VideoDumpConfig.ROOT_DIR + VideoDumpConfig.IMAGES_LIST);
image_list.delete();
mImageListWriter = new BufferedWriter(new FileWriter(image_list));
} catch (java.io.IOException e) {
Log.e(TAG, e.getMessage(), e);
}
queueEvent(new Runnable() {
public void run() {
mRenderer.setMediaPlayer(mMediaPlayer);
mRenderer.setImageListWriter(mImageListWriter);
}
});
super.onResume();
}
Aggregations