作者:wuhua
空間:htt://wuhua.3geye.net
轉載請保留上面的資訊(請尊重知識產品)謝謝
package com.ucweb.player;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.Menu.Item;
public class VideoPlayer extends Activity implements OnErrorListener,
OnBufferingUpdateListener, OnCompletionListener
{
private static final String TAG = "PlayVideo";
private static final int PLAY_VIDEO = Menu.FIRST + 1;
private static final int PLAY_MP3 = Menu.FIRST + 2;
private static final int STOP_PLAY_ID = Menu.FIRST + 4;
private MediaPlayer mp;
private Preview mPreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
Log.v(TAG, "onCreate: ===> ");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFormat(PixelFormat.TRANSPARENT);
mPreview = new Preview(this);
setContentView(mPreview);
}
private void playVideo()
{
try
{
SurfaceView view = new SurfaceView(this);
Surface surface = view.getHolder().getSurface();
setContentView(view);
Log.v(TAG, "height: " + mPreview.getHeight());
Log.v(TAG, "width: " + mPreview.getWidth());
mp = MediaPlayer.create(this, R.raw.mp3);
mp.setOnErrorListener(this);
mp.setDisplay(mPreview.getHolder().getSurface());
mp.prepare();
Log.v(TAG, "Duration: ===>" + mp.getDuration());
mp.start();
}
catch (Exception e)
{
//e.printStackTrace();
Log.e(TAG, "error: " + e.getMessage());
stopPlay();
}
}
private void playMp3() throws IOException
{
mp = MediaPlayer.create(this, R.raw.mp3);
mp.prepare();
Log.v(TAG, "Duration: ===>" + mp.getDuration());
mp.start();
}
private void stopPlay() {
if (mp != null) {
mp.stop();
mp.release();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, PLAY_MP3,"MP3");
menu.add(1, PLAY_VIDEO, "MP3");
menu.add(1, STOP_PLAY_ID, "MP3");
return result;
}
@Override
public boolean onOptionsItemSelected(Item item)
{
Log.v(TAG, "option: " + item.getId());
switch (item.getId()) {
case PLAY_MP3:
try{
playMp3();
}catch(Exception e){
}
break;
case PLAY_VIDEO:
playVideo();
break;
case STOP_PLAY_ID:
stopPlay();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected boolean isFullscreenOpaque()
{
// Our main window is set to translucent, but we know that we
//will
// fill it with opaque data. Tell the system that so it can
//perform
// some important optimizations.
return false;
}
@Override
protected void onResume() {
// Because the CameraDevice object is not a shared resource,
// it's very important to release it when the activity is paused.
super.onResume();
// mPreview.resume();
}
@Override
protected void onPause() {
// Start Preview again when we resume.
super.onPause();
// mPreview.pause();
}
public void onError(MediaPlayer mediaPlayer, int what, int extra) {
Log.e(TAG, "onError---> what:" + what + " extra:" + extra);
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
public void onBufferingUpdate(MediaPlayer arg0, int percent) {
Log.d(TAG, "onBufferingUpdate---> percent:" + percent);
}
public void onCompletion(MediaPlayer arg0) {
Log.d(TAG, "onCompletion---> finished");
}
}
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
private boolean mHasSurface;
Preview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHasSurface = false;
mHolder.setFixedSize(320, 240);
//mHolder.setFixedSize(192, 242);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, start our main acquisition thread.
mHasSurface = true;
// return true;
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return. Stop the preview.
mHasSurface = false;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
// Surface size or format has changed. This should not happen in
//this
// example.
}}
現在對這個東西還進行摸索階段不知道他有沒有提供對流媒體的支援。如果可以直接支援流媒體的話。那真是太好了。這樣就可以用java來寫流媒體。 繼續研究