package org.apache.android.media;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.net.URLConnection;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.webkit.URLUtil;import android.widget.EditText;import android.widget.ImageButton;import android.widget.Toast;import android.widget.VideoView;public class VideoViewDemo extends Activity {private static final String TAG = "VideoViewDemo";private VideoView mVideoView;private EditText mPath;private ImageButton mPlay;private ImageButton mPause;private ImageButton mReset;private ImageButton mStop;private String current;@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);setContentView(R.layout.main);mVideoView = (VideoView) findViewById(R.id.surface_view);mPath = (EditText) findViewById(R.id.path);mPath.setText("http://daily3gp.com/vids/747.3gp");mPlay = (ImageButton) findViewById(R.id.play);mPause = (ImageButton) findViewById(R.id.pause);mReset = (ImageButton) findViewById(R.id.reset);mStop = (ImageButton) findViewById(R.id.stop);mPlay.setOnClickListener(new OnClickListener() {public void onClick(View view) {playVideo();}});mPause.setOnClickListener(new OnClickListener() {public void onClick(View view) {if (mVideoView != null) {mVideoView.pause();}}});mReset.setOnClickListener(new OnClickListener() {public void onClick(View view) {if (mVideoView != null) {mVideoView.seekTo(0);}}});mStop.setOnClickListener(new OnClickListener() {public void onClick(View view) {if (mVideoView != null) {current = null;mVideoView.stopPlayback();}}});runOnUiThread(new Runnable(){public void run() {playVideo();}});}private void playVideo() {try {final String path = mPath.getText().toString();Log.v(TAG, "path: " + path);if (path == null || path.length() == 0) {Toast.makeText(VideoViewDemo.this, "File URL/path is empty",Toast.LENGTH_LONG).show();} else {// If the path has not changed, just start the media playerif (path.equals(current) && mVideoView != null) {mVideoView.start();mVideoView.requestFocus();return;}current = path;mVideoView.setVideoPath(getDataSource(path));mVideoView.start();mVideoView.requestFocus();}} catch (Exception e) {Log.e(TAG, "error: " + e.getMessage(), e);if (mVideoView != null) {mVideoView.stopPlayback();}}}private String getDataSource(String path) throws IOException {if (!URLUtil.isNetworkUrl(path)) {return path;} else {URL url = new URL(path);URLConnection cn = url.openConnection();cn.connect();InputStream stream = cn.getInputStream();if (stream == null)throw new RuntimeException("stream is null");File temp = File.createTempFile("mediaplayertmp", "dat");temp.deleteOnExit();String tempPath = temp.getAbsolutePath();FileOutputStream out = new FileOutputStream(temp);byte buf[] = new byte[128];do {int numread = stream.read(buf);if (numread <= 0)break;out.write(buf, 0, numread);} while (true);try {stream.close();} catch (IOException ex) {Log.e(TAG, "error: " + ex.getMessage(), ex);}return tempPath;}}}

 

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 我工作就是为了赚钱!
  2. android中使用okhttp实现文件上传
  3. 使用Android(安卓)Studio导入源码
  4. Android下Activity结束(finish())之后没有
  5. 通过SVN导入两个项目(从java移植到andorid
  6. 谈谈Android线程的认识
  7. Android开发之adb && 破解开屏图案 && 代
  8. Android查找未用类和方法
  9. Android(安卓)APK的签名与重新签名
  10. Java侵权诉讼Google获胜,Android厚积薄发