Android 媒体:网络视频播放器的基本设计
16lz
2021-01-23
效果图
代码
MainActivity:
/* Activity实现SurfaceHolder.Callback */public class bof extends Activity implements SurfaceHolder.Callback { private TextView mTextView; private EditText mEditText; private MediaPlayer mMediaPlayer01; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; /* 标志MediaPlayer是否已被释放 */ private boolean bIsReleased = false; /* 标志MediaPlayer是否正处于暂停 */ private boolean bIsPaused = false; /* LogCat输出TAG filter */ private static final String TAG = "HippoMediaPlayer"; /** * 图像文件存放URL网址 */ private String strVideoURL = ""; private String mCurrentFilePath = ""; private String currentTempFilePath = ""; /* 播放,暂停,重置,停止 */ private ImageButton mPlay, mReset, mPause, mStop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initViews(); /* 将.3gp图像文件存放URL网址 */ strVideoURL = "http://www.xxx.com/xxx.3gp"; mEditText.setText(strVideoURL); /* 设置PixnelFormat */ getWindow().setFormat(PixelFormat.TRANSPARENT); /* 设置SurfaceHolder为Layout SurfaceView */ mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); /* 由于原有的影片Size较小,故指定其为固定比例 */ mSurfaceHolder.setFixedSize(160, 128); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); /* 播放按钮 */ mPlay.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View view) { if (checkSDCard()) { strVideoURL = mEditText.getText().toString(); playVideo(strVideoURL); mTextView.setText(R.string.str_play); } else { mTextView.setText(R.string.str_err_nosd); } } }); /* 重新播放按钮 */ mReset.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View view) { if (checkSDCard()) { if (bIsReleased == false) { if (mMediaPlayer01 != null) { mMediaPlayer01.seekTo(0); mTextView.setText(R.string.str_play); } } } else { mTextView.setText(R.string.str_err_nosd); } } }); /* 暂停按钮 */ mPause.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View view) { if (checkSDCard()) { if (mMediaPlayer01 != null) { if (bIsReleased == false) { if (bIsPaused == false) { mMediaPlayer01.pause(); bIsPaused = true; mTextView.setText(R.string.str_pause); } else if (bIsPaused == true) { mMediaPlayer01.start(); bIsPaused = false; mTextView.setText(R.string.str_play); } } } } else { mTextView.setText(R.string.str_err_nosd); } } }); /* 终止按钮 */ mStop.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View view) { if (checkSDCard()) { try { if (mMediaPlayer01 != null) { if (bIsReleased == false) { mMediaPlayer01.seekTo(0); mMediaPlayer01.pause(); mTextView.setText(R.string.str_stop); } } } catch (Exception e) { mTextView.setText(e.toString()); Log.e(TAG, e.toString()); e.printStackTrace(); } } else { mTextView.setText(R.string.str_err_nosd); } } }); } /* 自定义下载URL影片并播放 */ private void playVideo(final String strPath) { try { /* 若传入的strPath为现有播放的连接,则直接播放 */ if (strPath.equals(mCurrentFilePath) && mMediaPlayer01 != null) { mMediaPlayer01.start(); return; } else if (mMediaPlayer01 != null) { mMediaPlayer01.stop(); } mCurrentFilePath = strPath; /* 重新构建MediaPlayer对象 */ mMediaPlayer01 = new MediaPlayer(); /* 设置播放音量 */ mMediaPlayer01.setAudioStreamType(2); /* 设置显示于SurfaceHolder */ mMediaPlayer01.setDisplay(mSurfaceHolder); 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"); mTextView.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 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]; do { int numread = is.read(buf); if (numread <= 0) { break; } fos.write(buf, 0, numread); } while (true); mMediaPlayer01.setDataSource(currentTempFilePath); try { is.close(); } catch (Exception ex) { Log.e(TAG, "error: " + ex.getMessage(), ex); } } } } /** * 获取并返回文件扩展名 * * @param strFileName 视频的链接 */ private String getFileExtension(String strFileName) { File myFile = new File(strFileName); String strFileExtension = myFile.getName(); strFileExtension = (strFileExtension.substring (strFileExtension.lastIndexOf(".") + 1)).toLowerCase(); if (strFileExtension == "") { // 若无法顺利取得扩展名,默认为.dat strFileExtension = "dat"; } return strFileExtension; } /** * 判断SD卡是否存在 */ private boolean checkSDCard() { if (android.os.Environment.getExternalStorageState().equals (android.os.Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } private void initViews() { mTextView = (TextView) findViewById(R.id.myTextView1); mEditText = (EditText) findViewById(R.id.myEditText1); mPlay = (ImageButton) findViewById(R.id.play); mReset = (ImageButton) findViewById(R.id.reset); mPause = (ImageButton) findViewById(R.id.pause); mStop = (ImageButton) findViewById(R.id.stop); /* 绑定Layout上的SurfaceView */ mSurfaceView = (SurfaceView) findViewById(R.id.mSurfaceView1); } @Override public void surfaceChanged (SurfaceHolder surfaceholder, int format, int w, int h) { Log.i(TAG, "Surface Changed"); } @Override public void surfaceCreated(SurfaceHolder surfaceholder) { Log.i(TAG, "Surface Changed"); } @Override public void surfaceDestroyed(SurfaceHolder surfaceholder) { Log.i(TAG, "Surface Changed"); }}activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/white" android:orientation="vertical" android:padding="16dp" > <TextView android:id="@+id/myTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textColor="@drawable/blue"/> <EditText android:id="@+id/myEditText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textColor="#0000ff" android:text=""/> <SurfaceView android:id="@+id/mSurfaceView1" android:layout_width="320px" android:layout_height="240px" android:layout_margin="16dp" android:visibility="visible"> </SurfaceView> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dip" > <ImageButton android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/play" /> <ImageButton android:id="@+id/pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pause" /> <ImageButton android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/reset" /> <ImageButton android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/stop" /> </LinearLayout></LinearLayout>
更多相关文章
- android hander 线程用法
- android 弹出带按钮的对话框
- Android之多线程断点下载
- 如何在Android平板电脑POWER按钮菜单中添加休眠功能
- Android非主线程更新UI
- Android按钮事件响应顺序
- android按钮的操作例子,简单大家看明白_基础篇