android 6 videoview,android VideoView播放视频-source

【实例简介】android VideoView播放视频-source

【实例截图】

【核心代码】

package com.example.shenxiaolei.myapplication;

import android.app.Activity;

import android.app.Notification;

import android.content.Intent;

import android.media.MediaPlayer;

import android.net.Uri;

import android.os.Bundle;

import android.provider.MediaStore;

import android.util.DisplayMetrics;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.view.MotionEvent;

import android.view.View;

import android.widget.MediaController;

import android.widget.ProgressBar;

import android.widget.RelativeLayout;

import android.widget.VideoView;

public class MainActivity extends Activity implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener, View.OnTouchListener {

/**

* View播放

*/

private VideoView videoView;

/**

* 加载预览进度条

*/

private ProgressBar progressBar;

/**

* 设置view播放控制条

*/

private MediaController mediaController;

/**

* 标记当视频暂停时播放位置

*/

private int intPositionWhenPause=-1;

/**

* 设置窗口模式下的videoview的宽度

*/

private int videoWidth;

/**

* 设置窗口模式下videoview的高度

*/

private int videoHeight;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// initVideoView();

Uri uri=Uri.parse("http://123.150.52.227/0725695b00000000-1415769042-1960016430/data5/vkplx.video.qq.com/flv/74/164/a0015193bxf.p203.1.mp4");

Intent intent=new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(uri,"video/*");

startActivity(intent);

}

/**

*初始化videoview播放

*/

public void initVideoView(){

//初始化进度条

progressBar= (ProgressBar) findViewById(R.id.progressBar);

//初始化VideoView

videoView= (VideoView) findViewById(R.id.videoView);

//初始化videoview控制条

mediaController=new MediaController(this);

//设置videoview的控制条

videoView.setMediaController(mediaController);

//设置显示控制条

mediaController.show(0);

//设置播放完成以后监听

videoView.setOnCompletionListener(this);

//设置发生错误监听,如果不设置videoview会向用户提示发生错误

videoView.setOnErrorListener(this);

//设置在视频文件在加载完毕以后的回调函数

videoView.setOnPreparedListener(this);

//设置videoView的点击监听

videoView.setOnTouchListener(this);

//设置网络视频路径

// http://123.150.52.227/0725695b00000000-1415769042-1960016430/data5/vkplx.video.qq.com/flv/74/164/a0015193bxf.p203.1.mp4

// http://120.41.1.158/vkplx.video.qq.com/flv/74/164/a0015193bxf.p203.1.mp4

// http://vkplx.video.qq.com/flv/74/164/a0015193bxf.p203.1.mp4

// http://221.204.220.17//data5/cdn_transfer/36/4C/364f19b5662e054057fc9851703a03f3b0f4ea4c.flv

// http://106.38.249.144/youku/67739CD2F943E7AB77EFF6511/03000810005429C7E703C905CF07DD613756C8-634B-EB1E-9628-F2F38A38C261.mp4

Uri uri=Uri.parse("http://123.150.52.227/0725695b00000000-1415769042-1960016430/data5/vkplx.video.qq.com/flv/74/164/a0015193bxf.p203.1.mp4");

videoView.setVideoURI(uri);

//设置为全屏模式播放

setVideoViewLayoutParams(1);

}

@Override

protected void onStart() {

super.onStart();

//启动视频播放

videoView.start();

//设置获取焦点

videoView.setFocusable(true);

}

/**

* 设置videiview的全屏和窗口模式

* @param paramsType 标识 1为全屏模式 2为窗口模式

*/

public void setVideoViewLayoutParams(int paramsType){

if(1==paramsType) {

RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

videoView.setLayoutParams(LayoutParams);

}else{

//动态获取宽高

DisplayMetrics DisplayMetrics=new DisplayMetrics();

this.getWindowManager().getDefaultDisplay().getMetrics(DisplayMetrics);

videoHeight=DisplayMetrics.heightPixels-50;

videoWidth=DisplayMetrics.widthPixels-50;

RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(videoWidth,videoHeight);

LayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

videoView.setLayoutParams(LayoutParams);

}

}

/**

* 视频播放完成以后调用的回调函数

*/

@Override

public void onCompletion(MediaPlayer mp) {

}

/**

* 视频播放发生错误时调用的回调函数

*/

@Override

public boolean onError(MediaPlayer mp, int what, int extra) {

switch (what){

case MediaPlayer.MEDIA_ERROR_UNKNOWN:

Log.e("text","发生未知错误");

break;

case MediaPlayer.MEDIA_ERROR_SERVER_DIED:

Log.e("text","媒体服务器死机");

break;

default:

Log.e("text","onError " what);

break;

}

switch (extra){

case MediaPlayer.MEDIA_ERROR_IO:

//io读写错误

Log.e("text","文件或网络相关的IO操作错误");

break;

case MediaPlayer.MEDIA_ERROR_MALFORMED:

//文件格式不支持

Log.e("text","比特流编码标准或文件不符合相关规范");

break;

case MediaPlayer.MEDIA_ERROR_TIMED_OUT:

//一些操作需要太长时间来完成,通常超过3 - 5秒。

Log.e("text","操作超时");

break;

case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:

//比特流编码标准或文件符合相关规范,但媒体框架不支持该功能

Log.e("text","比特流编码标准或文件符合相关规范,但媒体框架不支持该功能");

break;

default:

Log.e("text","onError " extra);

break;

}

//如果未指定回调函数, 或回调函数返回假,VideoView 会通知用户发生了错误。

return false;

}

/**

* 视频文件加载文成后调用的回调函数

* @param mp

*/

@Override

public void onPrepared(MediaPlayer mp) {

//如果文件加载成功,隐藏加载进度条

progressBar.setVisibility(View.GONE);

}

/**

* 对videoView的触摸监听

* @param v

* @param event

* @return

*/

@Override

public boolean onTouch(View v, MotionEvent event) {

return false;

}

/**

* 页面暂停效果处理

*/

@Override

protected void onPause() {

super.onPause();

//如果当前页面暂定则保存当前播放位置,并暂停

intPositionWhenPause=videoView.getCurrentPosition();

//停止回放视频文件

videoView.stopPlayback();

}

/**

* 页面从暂停中恢复

*/

@Override

protected void onResume() {

super.onResume();

//跳转到暂停时保存的位置

if(intPositionWhenPause>=0){

videoView.seekTo(intPositionWhenPause);

//初始播放位置

intPositionWhenPause=-1;

}

}

@Override

protected void onDestroy() {

super.onDestroy();

if(null!=videoView){

videoView=null;

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值