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 java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
/* Activity实现SurfaceHolder.Callback */
public class EX08_15 extends Activity
implements SurfaceHolder.Callback, OnClickListener
{
private TextView mTextView01;
private EditText mEditText01;
/* 创建MediaPlayer对象 */
private MediaPlayer mMediaPlayer01;
/* 用以配置MediaPlayer的SurfaceView */
private SurfaceView mSurfaceView01;
/* SurfaceHolder对象 */
private SurfaceHolder mSurfaceHolder01;
private ImageButton mPlay, mReset, mPause, mStop;
/* 识别MediaPlayer是否已被释放 */
private boolean bIsReleased = false;
/* 识别MediaPlayer是否正处于暂停 */
private boolean bIsPaused = false;
private static final String TAG = "HippoMediaPlayer";
private String currentFilePath = "";
private String currentTempFilePath = "";
private String strVideoURL = "";
private List fileDownloaded_List;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**定义临时文件路径列表 */
fileDownloaded_List = new ArrayList<String>();
/* 将.3gp图像文件存放URL网址 */
strVideoURL = "http://www.dubblogs.cc:8751/Android/Test/Media/3gp/test.3gp";
//http://www.dubblogs.cc:8751/Android/Test/Media/3gp/test2.3gp
mTextView01 = (TextView)findViewById(R.id.myTextView1);
mEditText01 = (EditText)findViewById(R.id.myEditText1);
mEditText01.setText(strVideoURL);
mSurfaceView01 = (SurfaceView) findViewById(R.id.mSurfaceView1);
/* 设置PixnelFormat */
getWindow().setFormat(PixelFormat.TRANSPARENT);
/* 设置SurfaceHolder为Layout SurfaceView */
mSurfaceHolder01 = mSurfaceView01.getHolder();
mSurfaceHolder01.addCallback(this);
/* 由于原有的影片Size较小,故指定其为固定比例 */
mSurfaceHolder01.setFixedSize(160, 128);
mSurfaceHolder01.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mPlay = (ImageButton) findViewById(R.id.play);
mReset = (ImageButton) findViewById(R.id.reset);
mPause = (ImageButton) findViewById(R.id.pause);
mStop = (ImageButton) findViewById(R.id.stop);
mPlay.setOnClickListener(this);
mReset.setOnClickListener(this);
mPause.setOnClickListener(this);
mStop.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch(v.getId()){
case R.id.play: // 播放
if(checkSDCard())
{
strVideoURL = mEditText01.getText().toString();
playVideo(strVideoURL);
mTextView01.setText(R.string.str_play);
}
else
mTextView01.setText(R.string.str_err_nosd);
break;
case R.id.reset: // 重新播放
if(checkSDCard())
{
if(bIsReleased == false)
{
if (mMediaPlayer01 != null)
{
mMediaPlayer01.seekTo(0);
mTextView01.setText(R.string.str_play);
}
}
}
else
mTextView01.setText(R.string.str_err_nosd);
break;
case R.id.pause: // 暂停
if(checkSDCard())
{
if (mMediaPlayer01 != null)
{
if(bIsReleased == false)
{
if(bIsPaused==false)
{
mMediaPlayer01.pause();
bIsPaused = true;
mTextView01.setText(R.string.str_pause);
}
else if(bIsPaused==true)
{
mMediaPlayer01.start();
bIsPaused = false;
mTextView01.setText(R.string.str_play);
}
}
}
}
else
mTextView01.setText(R.string.str_err_nosd);
break;
case R.id.stop: // 终止
if(checkSDCard()) {
try {
if (mMediaPlayer01 != null) {
if(bIsReleased==false) {
mMediaPlayer01.seekTo(0);
mMediaPlayer01.pause();
mTextView01.setText(R.string.str_stop);
}
}
} catch(Exception e) {
mTextView01.setText(e.toString());
Log.e(TAG, e.toString());
e.printStackTrace();
}
} else
mTextView01.setText(R.string.str_err_nosd);
break;
}
}
/* 自定义下载URL影片并播放 */
private void playVideo(final String strPath)
{
try
{
/* 若传入的strPath为现有播放的连接,则直接播放 */
if (strPath.equals(currentFilePath) && mMediaPlayer01 != null)
{
mMediaPlayer01.start();
return;
}
else if(mMediaPlayer01 != null)
mMediaPlayer01.stop();
currentFilePath = strPath;
/* 重新构建MediaPlayer对象 */
mMediaPlayer01 = new MediaPlayer();
/* 设置播放音量 */
mMediaPlayer01.setAudioStreamType(2);
/* 设置显示于SurfaceHolder */
mMediaPlayer01.setDisplay(mSurfaceHolder01);
mMediaPlayer01.setOnErrorListener
(new MediaPlayer.OnErrorListener()
{
@Override
public boolean onError(MediaPlayer mp, int what, int extra)
{
Log.i(TAG, "Error on Listener, what: " + what + "extra: " + extra);
return false;
}
});
mMediaPlayer01.setOnBufferingUpdateListener
(new MediaPlayer.OnBufferingUpdateListener()
{
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
Log.i(TAG, "Update buffer: " + Integer.toString(percent) + "%");
}
});
mMediaPlayer01.setOnCompletionListener
(new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
Log.i(TAG,"mMediaPlayer01 Listener Completed");
mTextView01.setText(R.string.str_done);
}
});
mMediaPlayer01.setOnPreparedListener
(new MediaPlayer.OnPreparedListener()
{
@Override
public void onPrepared(MediaPlayer mp)
{
Log.i(TAG,"Prepared Listener");
}
});
Runnable r = new Runnable()
{
public void run()
{
try
{
/* 在线程运行中,调用自定义函数抓下文件 */
setDataSource(strPath);
/* 下载完后才会调用prepare */
mMediaPlayer01.prepare();
Log.i
(
TAG, "Duration: " + mMediaPlayer01.getDuration()
);
mMediaPlayer01.start();
bIsReleased = false;
}
catch (Exception e)
{
Log.e(TAG, e.getMessage(), e);
}
}
};
new Thread(r).start();
}
catch(Exception e)
{
if (mMediaPlayer01 != null)
{
mMediaPlayer01.stop();
mMediaPlayer01.release();
}
}
}
/* 自定义setDataSource,由线程启动 */
private void setDataSource(String strPath) throws Exception
{
if (!URLUtil.isNetworkUrl(strPath))
mMediaPlayer01.setDataSource(strPath);
else {
if(bIsReleased == false)
{
URL myURL = new URL(strPath);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
if (is == null) {
throw new RuntimeExcept