您的位置:首页 > 理论基础 > 计算机网络

Android中Vitamio的VideoView实现网络视频在线播放

2016-05-13 13:20 549 查看
效果图:



代码:

1、关联Vitamio的支持库

2、布局文件videoview.xml中

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<io.vov.vitamio.widget.VideoView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>

</LinearLayout></span>


3、activity中

<span style="font-size:18px;">public class VideoViewDemo extends Activity {

private VideoView mVideoView;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Vitamio.isInitialized(getApplicationContext());
setContentView(R.layout.videoview);
playfunction();
myOnClick();

}

@Override
public void onConfigurationChanged(Configuration newConfig) {
if (mVideoView != null)
mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_SCALE, 0);
super.onConfigurationChanged(newConfig);
}
/**
* 加载视频
*/
void playfunction() {
String path = "";
//		path = "http://gslb.miaopai.com/stream/oxX3t3Vm5XPHKUeTS-zbXA__.mp4";
path = "http://gslb.miaopai.com/stream/oxX3t3Vm5XPHKUeTS-zbXA__.mp4";
mVideoView = (VideoView) findViewById(R.id.surface_view);
if (path == "") {
// Tell the user to provide a media file URL/path.
Toast.makeText(VideoViewDemo.this, "路径错误", Toast.LENGTH_LONG)
.show();
return;
} else {
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();

mVideoView
.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// optional need Vitamio 4.0
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
}
}
/**
* 监听
*/
private void myOnClick(){
//设置一下监听:播放完成的监听,播放准备好的监听,播放出错的监听

mVideoView.setOnPreparedListener(new OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
//开始播放
progressDialog2.dismiss();
}
});

mVideoView.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(getApplicationContext(), "视频播放完成了", Toast.LENGTH_SHORT).show();
finish();//退出播放器
}
});

mVideoView.setOnErrorListener(new OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(getApplicationContext(), "视频播放出错了",  Toast.LENGTH_SHORT).show();
return true;
}
});
/**
* 缓冲设置
*
*/
mVideoView.setOnInfoListener(new OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
if (mVideoView.isPlaying()) {
mVideoView.pause();

}
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
mVideoView.start();

break;
case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:

break;
}
return true;
}

});
mVideoView.setBufferSize(1024);
}

//	显示界面后,展示dailog
ProgressDialog progressDialog2=null;
@Override
protected void onResume() {
super.onResume();
progressDialog2=new ProgressDialog(VideoViewDemo.this);
progressDialog2.setMessage("玩命加载中。。。");
progressDialog2.setCancelable(true);
progressDialog2.show();//对话框显示

}

}</span>
4、主清单配置文件中

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.vov.vitamio.demo"
android:versionCode="1"
android:versionName="0.0.1" >

<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/vitamio_demo_name"
>
<activity
android:name=".VideoViewDemo"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="io.vov.vitamio.activity.InitActivity"
android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
android:launchMode="singleTop"
android:theme="@android:style/Theme.NoTitleBar"
android:windowSoftInputMode="stateAlwaysHidden"
/>

</application>

</manifest></span>


源码及支持库下载:

http://download.csdn.net/detail/zhaihaohao1/9519049

官网

https://www.vitamio.org/docs/Tutorial/2013/0508/12.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: