注:SurfaceView+MediaPlayer来实现视频播放(播放网络视频的是会有一个加载的过程(也就是“黑屏”),后面我的解决办法是把视频下载下来,在加载。目前只想到这样办法,有其他办法希望来一起讨论)
一、加载本地视频(不会卡顿,也不会黑屏)
二、加载网络视频当界面背景(没下载的时候,有加载(也就是有所谓的黑屏))
三、加载网络视频当界面背景(提前下载视频到本地)不会出现黑屏,循环播放的时候也不会卡顿
1、主要先在MainActivity.class下载网络视频(
new load_video() .execute("http://ohfn56j2u.bkt.clouddn.com/584139b1ac294.mp4");//先下载网络视频)
/**
* 下载网络视频
*/
class load_video extends AsyncTask
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
int count;
for (int i = 0; i < params.length; i++) {
try {
URL url = new URL(params[i]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(
url.openStream(), 8192);// 1024*8
File f = new File(Environment.getExternalStorageDirectory()
+ "/jiaoju");
if (f.isDirectory()) {
System.out.println("exist!");
} else {
System.out.println("not exist!");
f.mkdirs();
}
// Output stream
OutputStream output = new FileOutputStream(Environment
.getExternalStorageDirectory().toString()
+ "/jiaoju/splash.mp4");
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// VideoView videoView = (VideoView) findViewById(R.id.videoview);
// File videoFile = new File(Environment.getExternalStorageDirectory()
// .toString() + "/jiaoju/splash.mp4");
// videoView.setVideoPath(videoFile.getPath());
// videoView.start();
}
}
2、然后在PhotographyNet.class播放本地视频
File videoFile = new File(Environment.getExternalStorageDirectory().toString() + "/jiaoju/splash.mp4");
mediaPlayer.setDataSource(videoFile.getPath());//加载网络视频
mediaPlayer.setLooping(true);//循环播放
四、用第三方框架(PLVideoTextureView)循环播放(循环播放的时候,会卡顿)
(由于时间比较急,没有整合在一起)第四种方法请看这个链接的的源码:
http://download.youkuaiyun.com/detail/qq_30543115/9744452
五、用VideoView播放(VideoView的本质就是SurfaceView)
源码下载:http://download.youkuaiyun.com/detail/qq_30543115/9744455
有更好的办法的盆友,欢迎一起讨论哈。欢迎指正