Created
March 10, 2012 04:03
-
-
Save justlaputa/2010040 to your computer and use it in GitHub Desktop.
LiveStreamer API demo
This file contains 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
package com.mmg.mlive.media; | |
import java.io.IOException; | |
import com.mmg.mlive.activity.R; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.util.Log; | |
import android.view.Display; | |
import android.widget.LinearLayout; | |
import android.media.MediaPlayer; | |
import android.media.MediaPlayer.OnCompletionListener; | |
import android.media.MediaPlayer.OnErrorListener; | |
import android.media.MediaPlayer.OnInfoListener; | |
import android.media.MediaPlayer.OnPreparedListener; | |
import android.media.MediaPlayer.OnSeekCompleteListener; | |
import android.media.MediaPlayer.OnVideoSizeChangedListener; | |
import android.view.SurfaceHolder; | |
import android.view.SurfaceView; | |
public class StreamerDemo extends Activity implements OnCompletionListener, | |
OnErrorListener, OnInfoListener, OnPreparedListener, | |
OnSeekCompleteListener, OnVideoSizeChangedListener, | |
SurfaceHolder.Callback { | |
Display currentDisplay; | |
SurfaceView surfaceView; | |
SurfaceHolder surfaceHolder; | |
MediaPlayer mediaPlayer; | |
int videoWidth = 0; | |
int videoHeight = 0; | |
boolean readyToPlay = false; | |
public final static String LOGTAG = "CUSTOM_VIDEO_PLAYER"; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
surfaceView = (SurfaceView) this.findViewById(R.id.play_view); | |
surfaceHolder = surfaceView.getHolder(); | |
surfaceHolder.addCallback(this); | |
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); | |
mediaPlayer = new MediaPlayer(); | |
mediaPlayer.setOnCompletionListener(this); | |
mediaPlayer.setOnErrorListener(this); | |
mediaPlayer.setOnInfoListener(this); | |
mediaPlayer.setOnPreparedListener(this); | |
mediaPlayer.setOnSeekCompleteListener(this); | |
mediaPlayer.setOnVideoSizeChangedListener(this); | |
String filePath = Environment.getExternalStorageDirectory().getPath() | |
+ "/Test_Movie iPhone.m4v"; | |
try { | |
mediaPlayer.setDataSource(filePath); | |
} catch (IllegalArgumentException e) { | |
Log.v(LOGTAG, e.getMessage()); | |
finish(); | |
} catch (IllegalStateException e) { | |
Log.v(LOGTAG, e.getMessage()); | |
finish(); | |
} catch (IOException e) { | |
Log.v(LOGTAG, e.getMessage()); | |
finish(); | |
} | |
currentDisplay = getWindowManager().getDefaultDisplay(); | |
} | |
public void surfaceCreated(SurfaceHolder holder) { | |
Log.v(LOGTAG, "surfaceCreated Called"); | |
mediaPlayer.setDisplay(holder); | |
try { | |
mediaPlayer.prepare(); | |
} catch (IllegalStateException e) { | |
Log.v(LOGTAG, e.getMessage()); | |
finish(); | |
} catch (IOException e) { | |
Log.v(LOGTAG, e.getMessage()); | |
finish(); | |
} | |
} | |
public void surfaceChanged(SurfaceHolder holder, int format, int width, | |
int height) { | |
Log.v(LOGTAG, "surfaceChanged Called"); | |
} | |
public void surfaceDestroyed(SurfaceHolder holder) { | |
Log.v(LOGTAG, "surfaceDestroyed Called"); | |
} | |
public void onCompletion(MediaPlayer mp) { | |
Log.v(LOGTAG, "onCompletion Called"); | |
finish(); | |
} | |
public boolean onError(MediaPlayer mp, int whatError, int extra) { | |
Log.v(LOGTAG, "onError Called"); | |
if (whatError == MediaPlayer.MEDIA_ERROR_SERVER_DIED) { | |
Log.v(LOGTAG, "Media Error, Server Died " + extra); | |
} else if (whatError == MediaPlayer.MEDIA_ERROR_UNKNOWN) { | |
Log.v(LOGTAG, "Media Error, Error Unknown " + extra); | |
} | |
return false; | |
} | |
public boolean onInfo(MediaPlayer mp, int whatInfo, int extra) { | |
if (whatInfo == MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING) { | |
Log.v(LOGTAG, "Media Info, Media Info Bad Interleaving " + extra); | |
} else if (whatInfo == MediaPlayer.MEDIA_INFO_NOT_SEEKABLE) { | |
Log.v(LOGTAG, "Media Info, Media Info Not Seekable " + extra); | |
} else if (whatInfo == MediaPlayer.MEDIA_INFO_UNKNOWN) { | |
Log.v(LOGTAG, "Media Info, Media Info Unknown " + extra); | |
} else if (whatInfo == MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING) { | |
Log.v(LOGTAG, "MediaInfo, Media Info Video Track Lagging " + extra); | |
} else if (whatInfo == MediaPlayer.MEDIA_INFO_METADATA_UPDATE) { | |
Log.v(LOGTAG, "MediaInfo, Media Info Metadata Update " + extra); | |
} | |
return false; | |
} | |
public void onPrepared(MediaPlayer mp) { | |
Log.v(LOGTAG, "onPrepared Called"); | |
videoWidth = mp.getVideoWidth(); | |
videoHeight = mp.getVideoHeight(); | |
if (videoWidth > currentDisplay.getWidth() | |
|| videoHeight > currentDisplay.getHeight()) { | |
float heightRatio = (float) videoHeight | |
/ (float) currentDisplay.getHeight(); | |
float widthRatio = (float) videoWidth | |
/ (float) currentDisplay.getWidth(); | |
if (heightRatio > 1 || widthRatio > 1) { | |
if (heightRatio > widthRatio) { | |
videoHeight = (int) Math.ceil((float) videoHeight | |
/ (float) heightRatio); | |
videoWidth = (int) Math.ceil((float) videoWidth | |
/ (float) heightRatio); | |
} else { | |
videoHeight = (int) Math.ceil((float) videoHeight | |
/ (float) widthRatio); | |
videoWidth = (int) Math.ceil((float) videoWidth | |
/ (float) widthRatio); | |
} | |
} | |
} | |
surfaceView.setLayoutParams(new LinearLayout.LayoutParams(videoWidth, | |
videoHeight)); | |
mp.start(); | |
} | |
public void onSeekComplete(MediaPlayer mp) { | |
Log.v(LOGTAG, "onSeekComplete Called"); | |
} | |
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { | |
Log.v(LOGTAG, "onVideoSizeChanged Called"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment