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

效果图:

Android中Vitamio的VideoView实现网络视频在线播放_第1张图片

代码:

1、关联Vitamio的支持库

2、布局文件videoview.xml中




    


3、activity中

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();//对话框显示
			
	}
	 

}
4、主清单配置文件中




    

    
    
    
    

    
        
            
                

                
            
        
        
      
    

源码及支持库下载:

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

官网
https://www.vitamio.org/docs/Tutorial/2013/0508/12.html





你可能感兴趣的:(VideoView,视频,第三方)