Android视频播放器代码.docx
- 文档编号:8994101
- 上传时间:2023-02-02
- 格式:DOCX
- 页数:22
- 大小:20.88KB
Android视频播放器代码.docx
《Android视频播放器代码.docx》由会员分享,可在线阅读,更多相关《Android视频播放器代码.docx(22页珍藏版)》请在冰豆网上搜索。
Android视频播放器代码
Java代码部分:
importjava.io.IOException;
importjava.util.Random;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.content.pm.PackageManager.NameNotFoundException;
importandroid.media.AudioManager;
importandroid.media.MediaPlayer;
importandroid.media.MediaPlayer.OnBufferingUpdateListener;
importandroid.media.MediaPlayer.OnCompletionListener;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.view.GestureDetector.OnGestureListener;
importandroid.view.Gravity;
importandroid.view.MotionEvent;
importandroid.view.SurfaceHolder;
importandroid.view.SurfaceView;
importandroid.view.View;
importandroid.view.Window;
importandroid.view.WindowManager;
importandroid.view.View.OnClickListener;
importandroid.view.View.OnTouchListener;
importandroid.widget.ImageButton;
importandroid.widget.LinearLayout;
importandroid.widget.LinearLayout.LayoutParams;
importandroid.widget.PopupWindow;
importandroid.widget.SeekBar;
importandroid.widget.SeekBar.OnSeekBarChangeListener;
importandroid.widget.TextView;
importandroid.widget.Toast;
publicclassPlayVideoextendsActivityimplements
OnGestureListener,
OnClickListener,//监听
OnBufferingUpdateListener,//当网络缓冲数据流变化的时候唤起的播放事件
OnCompletionListener,//当媒体资源在播放的时候到达终点时唤起的播放事件
MediaPlayer.OnPreparedListener,
SurfaceHolder.Callback//回调函数
{
//视频高和宽
intvideoWidth;
intvideoHeight;
//按钮
ImageButtonplay;
//定义快进按钮
ImageButtonfastPlay;
//定义快退按钮
ImageButtonfastBack;
//控制栏
LinearLayoutlayout_control;
LinearLayoutlayout_prograss;
LinearLayoutvideoBack;
//用来播放媒体
MediaPlayermediaPlayer;
//显示媒体
SurfaceViewsurView;
//用来控制SurfaceView
SurfaceHoldersurHolder;
//路径
Stringpath;
//是否是播放状态
booleanboTing=true;
//获取播放的位置
intnum;
//点击屏幕次数
intcount;
//第一次点击
intfirClick;
//第二次点击
intsecClick;
//通过flag判断是否全屏
booleanflag;
//播放进度条
SeekBarseekbar;
//显示时间组建
TextViewshowTime;
//播放文件的时间
intminute;
intsecond;
//进度条进度
intprogress;
//线程控制
MyThreadmt;
//声音控制
SeekBarsound;
//声音进度
intsoundId;
//显示音量
TextViewshowSound;
//接取拖动进度条
intvideoLength;
booleanf=true;
//播放尺寸
//按钮隐藏时间
inthint=5000;
//用于接取点击ListView位置
intposition;
//声称随机数
Randomrandom;
//通过buttonFlag判断按钮背景
booleanbuttonFlag=true;
//显示视频总时间
TextViewallTime;
TextViewdistant;
PopupWindowpopuWindow;
Viewview;
booleanpopFlag;
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.video);
//产生对象
view=this.getLayoutInflater().inflate(R.layout.popuwindow,null);
//得到布局的ID
videoBack=(LinearLayout)view.findViewById(R.id.videoback);
//产生对象
play=(ImageButton)view.findViewById(R.id.video_bu_bofang);
layout_control=(LinearLayout)findViewById(R.id.layout_control);
layout_prograss=(LinearLayout)findViewById(R.id.layout_prograss);
seekbar=(SeekBar)view.findViewById(R.id.seekbar);
showTime=(TextView)view.findViewById(R.id.showtime);
fastPlay=(ImageButton)view.findViewById(R.id.fastplay);
fastBack=(ImageButton)view.findViewById(R.id.fastback);
sound=(SeekBar)view.findViewById(R.id.sound);
showSound=(TextView)view.findViewById(R.id.showsound);
surView=(SurfaceView)findViewById(R.id.surfaceview_1);
allTime=(TextView)view.findViewById(R.id.alltime);
distant=(TextView)findViewById(R.id.distant);
surHolder=surView.getHolder();
popuWindow=newPopupWindow(view,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
//设置回调函数
surHolder.addCallback(this);
//设置风格
surHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//bu_boFang.setVisibility(View.INVISIBLE);
//设置按键监听
play.setOnClickListener(this);
//快进监听
fastPlay.setOnClickListener(this);
//快退按钮监听
fastBack.setOnClickListener(this);
//new随机数对象
random=newRandom();
//接取到播放列表中点击的位置
position=VideoList.position;
try{
//获取ShareActivity上下文
VideoList.context=createPackageContext(".iotek",Context.CONTEXT_IGNORE_SECURITY);
VideoList.share=VideoList.context.getSharedPreferences("setupadapter",VideoList.context.MODE_WORLD_READABLE);
VideoList.editor=VideoList.share.edit();
}catch(NameNotFoundExceptione){
e.printStackTrace();
}
//对读取的信息进行判断
if(VideoList.share.getString("hinttime","5秒").equals("5秒")){
hint=5000;
}
if(VideoList.share.getString("hinttime","5秒").equals("10秒")){
hint=10000;
}
if(VideoList.share.getString("hinttime","5秒").equals("15秒")){
hint=15000;
}
//设置全屏播放
surView.setOnTouchListener(newOnTouchListener(){
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
//TODOAuto-generatedmethodstub
//handler.sendEmptyMessageAtTime(0x11,3000);
if(MotionEvent.ACTION_DOWN==event.getAction()){
count++;
if(count==1){
newcountClear().start();
firClick=(int)System.currentTimeMillis();
if(!
popFlag){
//////popuWindow=newPopupWindow(view,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
popuWindow.showAtLocation(view,Gravity.BOTTOM,0,0);
popFlag=true;
handler.removeMessages(0x11);
handler.sendEmptyMessageDelayed(0x11,hint);
}
//设置layout显示
//if(!
flag){
/*layout_control.setVisibility(View.VISIBLE);
layout_prograss.setVisibility(View.VISIBLE);*/
//点击surfaceView延时3S发送信息
/*handler.removeMessages(0x11);
handler.sendEmptyMessageDelayed(0x11,hint);*/
//}
}elseif(count==2){
secClick=(int)System.currentTimeMillis();
if(secClick-firClick<1000){
flag=!
flag;
count=0;
}
if(flag){
distant.setHeight(0);
surView.setLayoutParams(newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
surHolder.setFixedSize(480,760);
if(!
popFlag){
//popuWindow=newPopupWindow(view,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
popuWindow.showAtLocation(view,Gravity.BOTTOM,0,0);
popFlag=true;
handler.removeMessages(0x11);
handler.sendEmptyMessageDelayed(0x11,hint);
}
//开始播放
//mePlayer.start();
}else{
if(VideoList.share.getString("size","4:
3").equals("4:
3")){
surView.setLayoutParams(newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,360));
surHolder.setFixedSize(480,360);
distant.setHeight(250);
if(!
popFlag){
//popuWindow=newPopupWindow(view,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
popuWindow.showAtLocation(view,Gravity.BOTTOM,0,0);
popFlag=true;
handler.removeMessages(0x11);
handler.sendEmptyMessageDelayed(0x11,hint);
}
}
if(VideoList.share.getString("size","4:
3").equals("16:
9")){
surView.setLayoutParams(newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,270));
surHolder.setFixedSize(480,270);
distant.setHeight(250);
if(!
popFlag){
//popuWindow=newPopupWindow(view,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
popuWindow.showAtLocation(view,Gravity.BOTTOM,0,0);
popFlag=true;
handler.removeMessages(0x11);
handler.sendEmptyMessageDelayed(0x11,hint);
}
}
}
count=0;
firClick=0;
secClick=0;
}
}
returntrue;
}
});
//监听进度条进度
seekbar.setOnSeekBarChangeListener(newOnSeekBarChangeListener(){
@Override
publicvoidonStopTrackingTouch(SeekBarseekBar){
videoLength=seekBar.getProgress();
//intq=mediaPlayer.getCurrentPosition();
mediaPlayer.seekTo(videoLength);
//获取进度条当前的位置
//intdest=seekBar.getProgress();
//设置播放器当期的播放位置
//mediaPlayer.seekTo(num);
}
@Override
publicvoidonStartTrackingTouch(SeekBarseekBar){
}
@Override
publicvoidonProgressChanged(SeekBarseekBar,intprogress,
booleanfromUser){
//得到seekbar的进度
/*seekbar.setProgress(progress);
videoLength=seekBar.getProgress();*/
}
});
sound.setOnSeekBarChangeListener(newOnSeekBarChangeListener(){
@Override
publicvoidonStopTrackingTouch(SeekBarseekBar){
//TODOAuto-generatedmethodstub
inti=seekBar.getProgress();
mediaPlayer.setVolume(i/100f,i/100f);
}
@Override
publicvoidonStartTrackingTouch(SeekBarseekBar){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonProgressChanged(SeekBarseekBar,intprogress,
booleanfromUser){
//TODOAuto-generatedmethodstub
inti=sound.getProgress();
showSound.setText("音量:
"+i);
handler.removeMessages(0x11);
handler.sendEmptyMessageDelayed(0x11,hint);
}
});
}
privatevoidplayVideo(){
//构建MediaPlayer对象
mediaPlayer=newMediaPlayer();
try{
//设置媒体文件路径
mediaPlayer.setDataSource(VideoList.path);
//设置通过SurfaceView来显示画面
mediaPlayer.setDisplay(surHolder);
//准备
mediaPlayer.prepare();
distant.setHeight(200);
//设置事件监听
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
seekbar.setMax(mediaPlayer.getDuration());
//设置当前播放音量最大
soundId=sound.getProgress();
mediaPlayer.setVolume(soundId,soundId);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 视频 播放 代码