Info

src/irdc.ex08_15/EX08_15.java mP3 ^JisetDatoSourceOWS^aiftJiffil^W. ^f

MediaPlayer MM^R SurfaceView ^ SurfaceHolder fflM^t

SurfaceView jgiii&if R&ft (strVideoURL) MediaPlayer.prepare()

^ MediaPlayer. start(), ^m&feJ&XflrWHHtt} I/O ^fo ft Request, playVideo()^#T^IE, mMediaPlayerOl != null

IE& ), PJWiflh MediaPlayer ftmMediaPlayerOl.stop()0

package irdc.ex08_15;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.net.URL;

import java.net.URLConnection;

import android.app.Activity; import android.graphics.PixelFormat; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.webkit.URLUtil; import android.widget.EditText; import android.widget.ImageButton; import android.widget.TextView;

/* Activity^iU SurfaceHolder.Callback */ public class EX08_15 extends Activity implements SurfaceHolder.Callback {

private TextView mTextViewOl; private EditText mEditTextOl; /* frjj® MediaPlayer MM */ private MediaPlayer mMediaPlayerOl; /* $ WSEM MediaPlayer SurfaceView */ private SurfaceView mSurfaceViewOl; /* SurfaceHolder MM */

private SurfaceHolder mSurfaceHolderOl;

private ImageButton mPlay, mReset, mPause, mStop;

/* iRSO MediaPlayer MSBMSift */ private boolean blsReleased = false;

/* iRS'J MediaPlayer M^JEHh^m */ private boolean blsPaused = false;

private static final String TAG = "HippoMediaPlayer"; private String currentFilePath = ""; private String currentTempFilePath = ""; private String strVideoURL = "";

/** Called when the activity is first created. */ ©Override public void onCreate(Bundle savedlnstanceState) {

super.onCreate(savedlnstanceState) ; setContentView(R.layout.main);

strVideoURL =

"http://www.dubblogs.cc:8751/android/Test/Media/3gp/test.3gp"; //http://www.dubblogs.cc:8751/android/Test/Media/3gp/test2.3gp

mTextViewOl = (TextView)findViewByld(R.id.myTextViewl); mEditTextOl = (EditText)findViewByld(R.id.myEditTextl); mEditTextOl.setText(strVideoURL) ;

mSurfaceViewOl = (SurfaceView) findViewByld(R.id.mSurfaceViewl);

getWindow().setFormat(PixelFormat.TRANSPARENT);

/* iSB SurfaceHolder ^ Layout SurfaceView */ mSurfaceHolderOl = mSurfaceViewOl.getHolder();

mSurfaceHolderOl.addCallback(this);

/* iiJgl^ft Size^/J\, ^^»^S^tb^J */ mSurfaceHolderOl.setFixedSize(160, 128) ;

mSurfaceHolderOl. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS) ;

mPlay = (ImageButton) findViewByld(R.id.play); mReset = (ImageButton) findViewByld(R.id.reset); mPause = (ImageButton) findViewByld(R.id.pause); mStop = (ImageButton) findViewByld(R.id.stop);

mPlay.setOnClickListener(new ImageButton.OnClickListener() {

public void onClick(View view) {

strVideoURL = mEditTextOl.getText().toString(); playVideo(strVideoURL);

mTextViewOl.setText(R.string.str_play) ;

mTextViewOl.setText(R.string.str_err_nosd) ;

mReset.setOnClickListener(new ImageButton.OnClickListener() {

public void onClick(View view) {

if(checkSDCard())

mMediaPlayerOl.seekTo(0);

mTextViewOl.setText(R.string.str_play);

mTextViewOl.setText(R.string.str_err_nosd) ;

mPause.setOnClickListener(new ImageButton.OnClickListener() {

public void onClick(View view) {

if(bIsPaused==false) {

mMediaPlayerOl.pause();

blsPaused = true;

mTextViewOl.setText(R.string.str_pause) ;

else if(bIsPaused==true) {

mMediaPlayerOl.start();

blsPaused = false;

mTextViewOl.setText(R.string.str_play) ;

mTextViewOl.setText(R.string.str_err_nosd) ;

mStop.setOnClickListener(new ImageButton.OnClickListener() {

public void onClick(View view) {

if(bIsReleased==false) {

mMediaPlayerOl.seekTo(0) ; mMediaPlayerOl.pause();

mTextViewOl.setText(R.string.str_stop);

catch(Exception e) {

mTextViewOl.setText(e.toString());

mTextViewOl.setText(R.string.str_err_nosd) ;

private void playVideo(final String strPath)

/* StrPath fcSl^JSiicWgJg, mij*/

if (strPath.equals(currentFilePath) && mMediaPlayerOl != null) {

mMediaPlayerOl.start() ;

return;

else if(mMediaPlayerOl != null) {

mMediaPlayer01.stop();

currentFilePath = strPath;

/* M^r^ljt MediaPlayer 5ft& */ mMediaPlayerOl = new MediaPlayer();

mMediaPlayerOl.setAudioStreamType(2);

mMediaPlayerOl.setDisplay(mSurfaceHolderOl);

mMediaPlayerOl.setOnErrorListener

(new MediaPlayer.OnErrorListener () {

©Override public boolean onError(MediaPlayer mp, int what, int extra)

// TODO Auto-generated method stub Log. i (

TAG,

"Error on Listener, what: " + what + "extra: " + extra

return false;

mMediaPlayerOl.setOnBufferingUpdateListener

(new MediaPlayer.OnBufferingUpdateListener() {

©Override public void onBufferingUpdate(MediaPlayer mp, int percent) {

// TODO Auto-generated method stub Log. i (

TAG, "Update buffer: " + Integer.toString(percent) + "%"

mMediaPlayerOl.setOnCompletionListener

(new MediaPlayer.OnCompletionListener() {

@Override public void onCompletion(MediaPlayer mp) {

// TODO Auto-generated method stub

Log.i(TAG,"mMediaPlayerOl Listener Completed");

mTextViewOl.setText(R.string.str_done);

mMediaPlayerOl.setOnPreparedListener

(new MediaPlayer.OnPreparedListener() {

(¿Override public void onPrepared(MediaPlayer mp) {

// TODO Auto-generated method stub Log.i(TAG,"Prepared Listener");

Runnable r = new Runnable()

setDataSource(strPath);

mMediaPlayerOl .prepare () ;

TAG, "Duration: " + mMediaPlayerOl.getDuration()

mMediaPlayerOl.start();

blsReleased = false;

catch (Exception e) {

catch (Exception e) {

mMediaPlayerOl.stop(); mMediaPlayerOl.release();

private void setDataSource(String strPath) throws Exception

if (!URLUtil.isNetworkUrl(strPath)) {

mMediaPlayerOl.setDataSource(strPath);

URL myURL = new URL(strPath); URLConnection conn = myURL.openConnection(); conn.connect();

InputStream is = conn.getInputStream();

throw new RuntimeException("stream is null");

File myFileTemp = File.createTempFile ("hippoplayertmp", "."+getFileExtension(strPath));

currentTempFilePath = myFileTemp.getAbsolutePath();

/*currentTempFilePath = /sdcard/mediaplayertmp39327.dat */

FileOutputStream fos = new FileOutputStream(myFileTemp); byte buf[] = new byte[128];

int numread = is.read(buf);

break;

mMediaPlayerOl.setDataSource(currentTempFilePath);

catch (Exception ex) {

Log.e(TAG, "error: " + ex.getMessage(), ex);

private String getFileExtension(String strFileName) {

File myFile = new File(strFileName); String strFileExtension=myFile.getName(); strFileExtension=(strFileExtension.substring (strFileExtension.lastlndexOf(".")+l)).toLowerCase();

if(strFileExtension=="") {

strFileExtension = "dat";

return strFileExtension;

private boolean checkSDCard() {

if(android.os.Environment.getExternalStorageState().equals

(android.os.Environment.MEDIA_MOUNTED)) {

return true;

else

return false;

(¿Override public void surfaceChanged

(SurfaceHolder surfaceholder, int format, int w, int h) {

// TODO Auto-generated method stub Log.i(TAG, "Surface Changed");

(¿Override public void surfaceCreated(SurfaceHolder surfaceholder)

Was this article helpful?

0 0

Post a comment