标签:
activity代码
package com.hck.player.ui; import io.vov.utils.StringUtils; import io.vov.vitamio.LibsChecker; import io.vov.vitamio.MediaPlayer; import io.vov.vitamio.MediaPlayer.OnBufferingUpdateListener; import io.vov.vitamio.MediaPlayer.OnCompletionListener; import io.vov.vitamio.MediaPlayer.OnErrorListener; import io.vov.vitamio.MediaPlayer.OnInfoListener; import io.vov.vitamio.MediaPlayer.OnPreparedListener; import io.vov.vitamio.MediaPlayer.OnSeekCompleteListener; import io.vov.vitamio.MediaPlayer.OnVideoSizeChangedListener; import io.vov.vitamio.widget.VideoView; import java.sql.Timestamp; import java.util.ArrayList; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.graphics.Color; import android.media.AudioManager; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Display; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import com.hck.myplayer.R; import com.zdp.aseo.content.AseoZdpAseo; public class PlayActivity extends BaseActivity implements OnClickListener, OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback, OnErrorListener, OnInfoListener, OnSeekCompleteListener { private String path; private ImageView imageView; private TextView nametTextView; private TextView timeTextView; private Button chiceButton; private Button fontbButton, playbButton, nextButton; private VideoView videoView; private FrameLayout fLayout; private LinearLayout layout; private boolean isPlay; private SeekBar seekBar; private upDateSeekBar update; private boolean isLock; private String timeString; private TextView play_tiem, endTime; private String name; private ImageView mOperationBg; private ImageView mOperationPercent; private View mVolumeBrightnessLayout; private AudioManager mAudioManager; /** 最大声音 */ private int mMaxVolume; /** 当前声音 */ private int mVolume = -1; /** 当前亮度 */ private float mBrightness = -1f; private GestureDetector mGestureDetector; private View bar; private TextView textViewBF; private TextView sudu; private TextView sudu2; private PopupWindow pWindow; private Button qingxiButton, liuchangButton, gaoqingButton; private boolean isFinish; private long pauseSize; private long size; private ArrayList<String> urls; private static int flag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 检测Vitamio是否解压解码包 if (!LibsChecker.checkVitamioLibs(this)) return; // 去除标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 定义布局 setContentView(R.layout.play); // 初始化布局 initView(); // 获得数据 getInitDate(); // 设置数据 setInitDate(); nextButton.setEnabled(false); fontbButton.setEnabled(false); play(); AseoZdpAseo.initType(this, AseoZdpAseo.INSERT_TYPE); } private void getInitDate() { // path = getIntent().getStringExtra("path"); // 获取视频的名称 name = getIntent().getStringExtra("name"); // 获取视频的url urls = getIntent().getStringArrayListExtra("url"); } private void setInitDate() { // 设置视频的名称,并显示在屏幕上 nametTextView.setText("视频名字:" + name); } private void initView() { // 地址集合 urls = new ArrayList<String>(); // 视频播放进度条 seekBar = (SeekBar) findViewById(R.id.seekBar); // 视频信息 ,设置为不可见 fLayout = (FrameLayout) findViewById(R.id.title_fl); fLayout.setVisibility(View.GONE); // 视频屏幕下方的控制菜单,设置为不可见 layout = (LinearLayout) findViewById(R.id.buttom_lin); layout.setVisibility(View.GONE); // 视频播放框 videoView = (VideoView) findViewById(R.id.surface_view); // 屏幕是否解锁,并设置不可见,实现监听 imageView = (ImageView) findViewById(R.id.image_lock); imageView.setOnClickListener(this); imageView.setVisibility(View.GONE); // 视频下载的速度kb/s sudu = (TextView) findViewById(R.id.sudu); sudu2 = (TextView) findViewById(R.id.sudu2); // 视频缓冲进度条 bar = findViewById(R.id.pb); // 视频缓冲的进度,百分比 textViewBF = (TextView) bar.findViewById(R.id.buff); // 视频已经播放的时间 play_tiem = (TextView) findViewById(R.id.play_time); // 视频播放总时间 endTime = (TextView) findViewById(R.id.play_end_time); // 视频名称,并设置监听 nametTextView = (TextView) findViewById(R.id.movie_name); nametTextView.setOnClickListener(this); // 视频的时间 timeTextView = (TextView) findViewById(R.id.movie_time); // 得到当前时间(通过时间戳的装换函数Timestamp) timeString = (new Timestamp(System.currentTimeMillis())).toString() .substring(11, 16); timeTextView.setText(timeString); // 视频质量设定按钮,并设置监听按钮 chiceButton = (Button) findViewById(R.id.zhiliang); chiceButton.setOnClickListener(this); // 快退按钮 ,并设置监听 fontbButton = (Button) findViewById(R.id.font); fontbButton.setOnClickListener(this); // 播放按钮,并设置监听 playbButton = (Button) findViewById(R.id.play); playbButton.setOnClickListener(this); // 快进按钮并设置监听 nextButton = (Button) findViewById(R.id.next); nextButton.setOnClickListener(this); // 控制喇叭音量 mVolumeBrightnessLayout = findViewById(R.id.operation_volume_brightness); // 喇叭图片 mOperationBg = (ImageView) findViewById(R.id.operation_bg); // 音量控制的进度条 mOperationPercent = (ImageView) findViewById(R.id.operation_percent); // 手势操作类,并设置手势监听 mGestureDetector = new GestureDetector(this, new MyGestureListener()); // 获取系统服务 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); // 获取系统的最大音量 mMaxVolume = mAudioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 控制屏幕翻转 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 更新seekbar,即视频播放进度监听 setListener(); // 更新seekbar时,根据视频播放进度跟新时间等数据 update = new upDateSeekBar(); new Thread(update).start(); } private class MyGestureListener extends SimpleOnGestureListener { @Override // 当用户在屏幕上"滚动"时触发该方法 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // 得到x,y坐标当前值,相对于当前view float mOldX = e1.getX(), mOldY = e1.getY(); // getRawY()是表示相对于屏幕左上角的y坐标值 int y = (int) e2.getRawY(); // 获得屏幕的宽高 Display disp = getWindowManager().getDefaultDisplay(); int windowWidth = disp.getWidth(); int windowHeight = disp.getHeight(); // 右边滑动 if (mOldX > windowWidth * 4.0 / 5) onVolumeSlide((mOldY - y) / windowHeight); // 左边滑动 else if (mOldX < windowWidth / 5.0) onBrightnessSlide((mOldY - y) / windowHeight); return super.onScroll(e1, e2, distanceX, distanceY); } } /** * 滑动改变声音大小 * * @param percent */ private void onVolumeSlide(float percent) { if (mVolume == -1) { mVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); if (mVolume < 0) mVolume = 0; // 显示声音的大小 mOperationBg.setImageResource(R.drawable.video_volumn_bg); mVolumeBrightnessLayout.setVisibility(View.VISIBLE); } int index = (int) (percent * mMaxVolume) + mVolume; if (index > mMaxVolume) index = mMaxVolume; else if (index < 0) index = 0; // 变更声音 mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0); // 变更进度条 ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams(); lp.width = findViewById(R.id.operation_full).getLayoutParams().width * index / mMaxVolume; mOperationPercent.setLayoutParams(lp); } /** * 滑动改变亮度 * * @param percent */ private void onBrightnessSlide(float percent) { if (mBrightness < 0) { mBrightness = getWindow().getAttributes().screenBrightness; if (mBrightness <= 0.00f) mBrightness = 0.50f; if (mBrightness < 0.01f) mBrightness = 0.01f; // 显示 mOperationBg.setImageResource(R.drawable.video_brightness_bg); mVolumeBrightnessLayout.setVisibility(View.VISIBLE); } WindowManager.LayoutParams lpa = getWindow().getAttributes(); lpa.screenBrightness = mBrightness + percent; if (lpa.screenBrightness > 1.0f) lpa.screenBrightness = 1.0f; else if (lpa.screenBrightness < 0.01f) lpa.screenBrightness = 0.01f; getWindow().setAttributes(lpa); // 变更进度条 ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams(); lp.width = (int) (findViewById(R.id.operation_full).getLayoutParams().width * lpa.screenBrightness); mOperationPercent.setLayoutParams(lp); } //播放视频 private void play() { Log.i("hck", "play flag1: " + urls.get(flag)); isPlay = true; try { videoView.setVideoURI(Uri.parse(urls.get(flag))); videoView.setOnCompletionListener(this); videoView.setOnBufferingUpdateListener(this); videoView.setOnErrorListener(this); videoView.setOnInfoListener(this); videoView.setOnPreparedListener(this); } catch (Exception e) { Log.i("hck", "PlayActivity " + e.toString()); } } class upDateSeekBar implements Runnable { @Override public void run() { if (!isFinish) { mHandler.sendMessage(Message.obtain()); mHandler.postDelayed(update, 1000); } } } //通过用户指定的播放进度更新时间等信息 @SuppressLint("HandlerLeak") Handler mHandler = new Handler() { public void handleMessage(Message msg) { if (videoView == null) { return; } timeString = (new Timestamp(System.currentTimeMillis())).toString() .substring(11, 16); timeTextView.setText(timeString); play_tiem.setText(StringUtils.generateTime(videoView .getCurrentPosition())); if (videoView != null) { seekBar(videoView.getCurrentPosition()); } }; }; private void seekBar(long size) { if (videoView.isPlaying()) { long mMax = videoView.getDuration(); int sMax = seekBar.getMax(); seekBar.setProgress((int) (size * sMax / mMax)); } } private void setListener() { seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int value = (int) (seekBar.getProgress() * videoView.getDuration() / seekBar.getMax()); videoView.seekTo(value); videoView.start(); isPlay = true; } @Override public void onStartTrackingTouch(SeekBar seekBar) { isPlay = false; videoView.pause(); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); } @Override public boolean onTouchEvent(MotionEvent event) { if (mGestureDetector.onTouchEvent(event)) return true; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (!isLock) { fLayout.setVisibility(View.VISIBLE); layout.setVisibility(View.VISIBLE); } imageView.setVisibility(View.VISIBLE); break; case MotionEvent.ACTION_UP:
// 处理手势结束
endGesture(); break; default: break; } return super.onTouchEvent(event); } /** 手势结束 */ private void endGesture() { mVolume = -1; mBrightness = -1f;
// 隐藏 disHandler.removeMessages(0); disHandler.sendEmptyMessageDelayed(0, 1000); disHandler.removeMessages(1); disHandler.sendEmptyMessageDelayed(1, 5000); } private Handler disHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0) { mVolumeBrightnessLayout.setVisibility(View.GONE); } else { fLayout.setVisibility(View.GONE); layout.setVisibility(View.GONE); imageView.setVisibility(View.GONE); if (pWindow != null && pWindow.isShowing()) { pWindow.dismiss(); } } } }; @SuppressLint("HandlerLeak") @Override public void onClick(View v) { switch (v.getId()) { case R.id.play: if (isPlay) { videoView.pause(); playbButton .setBackgroundResource(R.drawable.player_play_highlight); isPlay = false; seekBar.setEnabled(false); } else { playbButton .setBackgroundResource(R.drawable.player_pause_highlight); videoView.start(); isPlay = true; seekBar.setEnabled(true); } break; case R.id.image_lock: if (isLock) { isLock = false; imageView.setBackgroundResource(R.drawable.lock_off); } else { isLock = true; imageView.setBackgroundResource(R.drawable.lock_on); } break; case R.id.next: videoView.pause(); if (videoView != null) { size = videoView.getCurrentPosition() + videoView.getDuration() / 10; videoView.seekTo(size); videoView.start(); } break; case R.id.font: videoView.pause(); if (videoView != null) { size = videoView.getCurrentPosition() - videoView.getDuration() / 10; videoView.seekTo(size); videoView.start(); } break; case R.id.zhiliang: Log.i("hck", "zhiliang"); showPop(v); break; } } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { } @Override public void surfaceCreated(SurfaceHolder arg0) { Log.i("hck", "surfaceCreated"); } @Override public void surfaceDestroyed(SurfaceHolder arg0) { } @Override public void onVideoSizeChanged(MediaPlayer arg0, int arg1, int arg2) { } @Override public void onPrepared(MediaPlayer arg0) { nextButton.setEnabled(true); fontbButton.setEnabled(true); bar.setVisibility(View.GONE); if (urls.get(flag).startsWith("http")) { videoView.setBufferSize(1024 * 1000); } else { videoView.setBufferSize(0); } endTime.setText(StringUtils.generateTime(videoView.getDuration())); bar.setVisibility(View.GONE); if (pauseSize > 0) { videoView.seekTo(pauseSize); } pauseSize = 0; } @Override public void onCompletion(MediaPlayer arg0) { videoView.stopPlayback(); } @Override public void onBufferingUpdate(MediaPlayer arg0, int arg1) { textViewBF.setTextColor(Color.CYAN); textViewBF.setText(videoView.getBufferPercentage() + "%"); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); videoView.setVideoLayout(3, 0); } @Override public boolean onError(MediaPlayer arg0, int arg1, int arg2) { Log.i("hck", "error :" + arg1 + arg2); return false; } @Override public boolean onInfo(MediaPlayer arg0, int arg1, int arg2) { switch (arg1) { case MediaPlayer.MEDIA_INFO_BUFFERING_START: if (isPlay) { bar.setVisibility(View.VISIBLE); videoView.pause(); isPlay = false; } break; case MediaPlayer.MEDIA_INFO_BUFFERING_END: if (!isPlay) { bar.setVisibility(View.GONE); videoView.start(); isPlay = true; } break; case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED: sudu.setTextColor(Color.CYAN); sudu.setText(arg2 + "kb/s"); sudu2.setText(arg2 + "kb/s"); break; } return true; } @Override protected void onPause() { super.onPause(); videoView.pause(); } @Override protected void onResume() { super.onResume(); if (isPlay) { videoView.start(); } } @Override protected void onDestroy() { super.onDestroy(); if (videoView != null) { videoView.stopPlayback(); videoView = null; } if (pWindow != null && pWindow.isShowing()) { pWindow.dismiss(); } isPlay = false; isFinish = true; System.gc(); } @Override public void onSeekComplete(MediaPlayer arg0) { } private void showPop(View view2) { // View view = LayoutInflater.from(this).inflate(R.layout.pop, null); // qingxiButton = (Button) view.findViewById(R.id.qingxi); // gaoqingButton = (Button) view.findViewById(R.id.gaoqing); // liuchangButton = (Button) view.findViewById(R.id.liuchang); // pWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, // LayoutParams.WRAP_CONTENT); // pWindow.setOutsideTouchable(true); // pWindow.showAtLocation(view2, Gravity.LEFT, Gravity.LEFT, 1); // setListener2(); } private void setListener2() { qingxiButton.setOnClickListener(this); gaoqingButton.setOnClickListener(this); liuchangButton.setOnClickListener(this); } }
缺少 :/** 当前缩放模式 */
private int mLayout = VideoView.VIDEO_LAYOUT_ZOOM;、
参考:http://www.cnblogs.com/over140/archive/2012/05/22/2473019.html
xml代码
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#000000" > <io.vov.vitamio.widget.VideoView android:id="@+id/surface_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 视频信息 --> <FrameLayout android:id="@+id/title_fl" android:layout_width="fill_parent" android:layout_height="50dp" android:background="@drawable/test_widget_bg" android:visibility="gone" > <TextView android:id="@+id/movie_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#ffffff" android:textSize="16sp" /> <TextView android:id="@+id/movie_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:layout_marginRight="10dp" android:text="11:34" android:textColor="#ffffff" /> </FrameLayout> <!-- 屏幕是否解锁 --> <ImageView android:id="@+id/image_lock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_marginLeft="15dp" android:layout_marginTop="15dp" android:src="@drawable/lock_off" android:visibility="gone" /> <LinearLayout android:id="@+id/buttom_lin" android:layout_width="fill_parent" android:layout_height="70dp" android:layout_gravity="bottom" android:background="@drawable/test_widget_bg" android:gravity="center_vertical" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 视频已经播放的时间 --> <TextView android:id="@+id/play_time" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="3dp" android:text="20:12:33" android:textColor="#ffffff" android:textSize="12sp" /> <!-- 视频播放进度条 --> <SeekBar android:id="@+id/seekBar" style="@style/player_progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_toLeftOf="@+id/play_end_time" android:layout_toRightOf="@+id/play_time" /> <!-- 视频播放总时间 --> <TextView android:id="@+id/play_end_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="3dp" android:text="20:12:33" android:textColor="#ffffff" android:textSize="12sp" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <!-- 视频质量设定按钮 --> <Button android:id="@+id/zhiliang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:background="@drawable/player_quality_current_background" android:onClick="showP" android:text="质量" android:textColor="#ffffff" android:textSize="12sp" /> <FrameLayout android:layout_width="200dp" android:layout_height="wrap_content" android:layout_centerInParent="true" > <!-- 快退按钮 --> <Button android:id="@+id/font" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:background="@drawable/player_prev_highlight" /> <!-- 播放,停止按钮 --> <Button android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/player_pause_highlight" /> <!-- 快进按钮 --> <Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:background="@drawable/test_player_next_highlight" /> </FrameLayout> <TextView android:id="@+id/sudu2" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_marginRight="2dp" android:gravity="center_vertical" android:text="显示" android:textColor="#ffffff" android:textSize="14sp" /> </RelativeLayout> </LinearLayout> </FrameLayout> <!-- 控制喇叭音量 --> <FrameLayout android:id="@+id/operation_volume_brightness" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#00000000" android:orientation="horizontal" android:padding="0dip" android:visibility="invisible" > <ImageView android:id="@+id/operation_bg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/video_volumn_bg" /> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:paddingBottom="25dip" > <ImageView android:id="@+id/operation_full" android:layout_width="94dip" android:layout_height="wrap_content" android:layout_gravity="left" android:src="@drawable/video_num_bg" /> <ImageView android:id="@+id/operation_percent" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_gravity="left" android:scaleType="matrix" android:src="@drawable/video_num_front" /> </FrameLayout> </FrameLayout> <!-- 视频缓冲进度条 --> <LinearLayout android:id="@+id/pb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="horizontal" > <TextView android:id="@+id/sudu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginRight="5dp" android:textColor="#ffffff" android:textSize="18sp" /> <TextView android:id="@+id/buff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="0%" android:textSize="18sp" /> <ProgressBar style="@android:style/Widget.ProgressBar.Small" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout> </RelativeLayout>
标签:
原文地址:http://www.cnblogs.com/zhujiabin/p/4645961.html