/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.android.media;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
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.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoViewDemo extends Activity {
private static final String TAG = "VideoViewDemo";
private VideoView mVideoView;
//private AudioPlayer mVideoView;
private EditText mPath;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
////////////////////////////////
private boolean wasPlaying;
private MediaPlayer mPlayer;
private static final int MIN_BUFF = 2000 * 1024;
private static final int REC_BUFF = 500 * 1024;
private Handler handler = new Handler();
private File DLTempFile;
private final String TEMP_DOWNLOAD_FILE_NAME = "tempMediaData";
private final String TEMP_BUFF_FILE_NAME = "tempBufferData";
private final String FILE_POSTFIX = ".mp4";
private final int PER_READ = 1024;
private boolean pause=false;
private boolean stop;
private final int UNKNOWN_LENGTH = -1;
private Handler mHandler = null;
private TextView playedTextView=null;
private TextView downtext =null;
private int VideoDuraton = 1; //总时间
private int curPosition;
private int mediaLength = 1; //文件总长度
private int totalKbRead = 0; //已经下载的长度
private boolean downloadOver = false;
private boolean wasPlayed = false;
private SeekBar seekbar = null;
private boolean localfile = false;
private double downper = 0.00; // 下载百分比
private double playper = 0.00; // 播放比例
private double loadper = 0.00;
////////////////////////////////
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
seekbar = (SeekBar) findViewById(R.id.seekbar);
seekbar.setMax(100);
playedTextView = (TextView) findViewById(R.id.has_played);
downtext = (TextView) findViewById(R.id.downtext);
mPath.setText("http://www.mis2036.com/0.mp4");
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mVideoView.setOnPreparedListener(prepareListener);
mVideoView.setOnCompletionListener(CompletionListener);
mVideoView.setOnErrorListener(ErrorListener);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
final String path = mPath.getText().toString();
localfile = false;
playnew(path);
//playVideo();
/*
try {
copyFile("/sdcard/0.mp4","/sdcard/a.mp4");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
playsdcardfile("/sdcard/0.mp4");
*/
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
localfile = true;
mVideoView.setVideoPath("/sdcard/0.mp4");
myHandler.sendEmptyMessage(PROGRESS_CHANGED);
//mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
//mVideoView.setVideoPath(DLTempFile.getAbsolutePath());
//mVideoView.seekTo(0);
mVideoView.stopPlayback();
DLTempFile.delete();
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
stop = true;
/*
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
*/
}
});
runOnUiThread(new Runnable(){
public void run() {
//playVideo();
}
});
}
//播放本地文件
private void playsdcardfile(String Source){
mVideoView.setVideoPath(Source);
mVideoView.start();
}
///////////////////////////////////////////
//线程下载部分
///////////////////////////////////////////
private MediaPlayer.OnPreparedListener prepareListener = new MediaPlayer.OnPreparedListener(){
public void onPrepared(MediaPlayer mp){
Toast.makeText(VideoViewDemo.this, "准备完成"+curPosition,Toast.LENGTH_SHORT).show();
VideoDuraton=mVideoView.getDuration()+1; //获取总时间
loadper = downper; //装置的部分,就是下载的部分
myHandler.sendEmptyMessage(PROGRESS_CHANGED);
mVideoView.seekTo(curPosition);
mp.start(); //开始播放
}
};
//视频播放完成
private MediaPlayer.OnCompletionListener CompletionListener=new MediaPlayer.OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
curPosition = 0;
mVideoView.stopPlayback();
}
};
//视频播放完成
private MediaPlayer.OnErrorListener ErrorListener=new MediaPlayer.OnErrorListener(){
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
mVideoView.stopPlayback();
Toast.makeText(VideoViewDemo.this, "发现错误:"+curPosition+"错误编号:"+what,Toast.LENGTH_SHORT).show();
pause = true;
return true;
}
};
public void playnew(final String path) {
downloadOver = false;
totalKbRead = 0;
try {
Log.v(TAG, "playing: " + path);
if (path.equals(current) && mPlayer != null) {
mPlayer.start();
return;
}
current = path;
mPlayer = null;
new PlayThread(current).start();
} catch (Exception e) {
}
}
public void setHandler(Handler handler) {
mHandler = handler;
}
//创建一个消息处理
private final static int PROGRESS_CHANGED = 0;
private final static int HIDE_CONTROLER = 1;
private final static int CHANGE_FULLSCREEN =2;
private final static int VIDEO_READY =3;
private final static int VIDEO_BUFFERFULL =4;
private final static int DOWN_REFRESH = 5;
Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
c