<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
>


<VideoView
android:id="@+id/activity_video_player_vd"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
/>
<include layout="@layout/contron_player"/>


</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/control_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_player_bottom_seekbar"
android:gravity="center_vertical"
android:orientation="horizontal" >


<TextView
android:id="@+id/current_tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text="当前时间"
android:textColor="#ffffff" />


<SeekBar
android:id="@+id/current_sk_seekbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_weight="1"
android:maxHeight="5dip"
android:minHeight="5dip"
android:progress="20"
android:progressDrawable="@drawable/progress_horizontal"
android:thumb="@drawable/progress_thumb" />


<TextView
android:id="@+id/current_tv_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:text="总时间"
android:textColor="#ffffff" />
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_player_bottom_control"
android:gravity="center_horizontal"
android:orientation="horizontal" >


<Button
android:id="@+id/current_btn_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/play_bg" />


<Button
android:id="@+id/current_btn_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/switch_player_bg" />
</LinearLayout>


</LinearLayout>




public class VideoPlayerActivity extends Activity {


private Boolean isDestroy = false;
protected static final int PROGRESS = 0;
private Uri uri = null;
private VideoView activity_video_player_vd = null;
private TextView current_tv_time;
private TextView current_tv_duration;
private SeekBar current_sk_seekbar;
private Button current_btn_pause;
private Button current_btn_player;
private Boolean myPlaying = true;
private Utils utils = null;


Handler handler = new Handler() {
@SuppressLint("NewApi")
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case PROGRESS:


//的到当前进度
int currentPosition = activity_video_player_vd
.getCurrentPosition();


current_tv_time
.setText(utils
.stringForTime(currentPosition));
//得到总时长
int duration = activity_video_player_vd
.getDuration();
current_tv_duration
.setText(utils
.stringForTime(duration));
//更新seeKBar的进度
current_sk_seekbar
.setProgress(currentPosition);


if (!isDestroyed()) {
handler
.sendEmptyMessageDelayed(
PROGRESS,
1 * 1000);


}


break;


}


};
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
init();//初始化控件
Listener();//VideoView监听事件
current_btn_pause.setOnClickListener(myListoner);//播放暂停监听事件


}


@Override
protected void onDestroy() {
isDestroy = true;
super.onDestroy();
}


/**
* VideoView监听事件
*/
private void Listener() {
activity_video_player_vd.setVideoURI(uri); //设置播放的绝对地址
/*
* 开始播放监听
*/
activity_video_player_vd.setOnPreparedListener(new OnPreparedListener() {


@Override
public void onPrepared(MediaPlayer mp) {
activity_video_player_vd.start();
myPlaying = activity_video_player_vd.isPlaying();
if (myPlaying) {
current_btn_pause.setBackgroundResource(R.drawable.pause_bg);
} else {
current_btn_pause.setBackgroundResource(R.drawable.play_bg);
}
/*发消息开始刷新进度**/
handler.sendEmptyMessage(PROGRESS);
current_sk_seekbar.setMax(activity_video_player_vd.getDuration());


}
});
/*
* 完成播放监听
*/
activity_video_player_vd.setOnCompletionListener(new OnCompletionListener() {


@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(getApplicationContext(), "播放结束", 1).show();
finish();
}
});
/*
* 播放出错
*/
activity_video_player_vd.setOnErrorListener(new OnErrorListener() {


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


Toast.makeText(getApplicationContext(), "播放出错", 1).show();
return true;
}
});
//SeekBar进度的监听
current_sk_seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
//当手指离开的时候回调这个方法
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}


//当手指开始触摸的时候回调这个方法
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}


//状态发送变化的时候回调这个方法
//progress 指定的视频位置
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {//用户滑动的才响应
activity_video_player_vd.seekTo(progress);
}
}
});


}


/**
* 按钮的监听事件
*/
OnClickListener myListoner = new OnClickListener() {


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.current_btn_player:


break;
case R.id.current_btn_pause:
if (myPlaying) {
activity_video_player_vd.pause();


current_btn_pause
.setBackgroundResource(R.drawable.play_bg);


} else {
activity_video_player_vd.start();
current_btn_pause
.setBackgroundResource(R.drawable.pause_bg);


}
myPlaying = !myPlaying;


break;


}
}
};


/**
* 初始化控件的方法
*/
private void init() {


Intent intent = getIntent();
uri = intent.getData();
activity_video_player_vd = (VideoView) findViewById(R.id.activity_video_player_vd);
current_tv_time = (TextView) findViewById(R.id.current_tv_time);
current_tv_duration = (TextView) findViewById(R.id.current_tv_duration);
current_sk_seekbar = (SeekBar) findViewById(R.id.current_sk_seekbar);
current_btn_pause = (Button) findViewById(R.id.current_btn_pause);
current_btn_player = (Button) findViewById(R.id.current_btn_player);
utils = new Utils();
}


}



更多相关文章

  1. Android中使用AndroidTestCase的方法实例
  2. android中动态实现全屏和动态退出全屏方法
  3. Android延时执行的几种方法
  4. android listview check 事件
  5. Android 获取当前语言的方法1
  6. android.util.Log常用的方法
  7. 在Android的Notification中显示进度条
  8. Android应用程序获取ROOT权限的方法(android中如何通过代码检测
  9. android 添加预装的方法

随机推荐

  1. Android之Service与IntentService的比较
  2. Android开发环境配置简介
  3. Ubuntu 10.04下Android开发环境的搭建
  4. android 线程优先级设置
  5. 用于替代 Android 自带 Dialog 和 PopupW
  6. Android从零开始-Android工程的目录结构
  7. Android核心技术——Android入门-威哥_马
  8. android 在子线程中使用Toast等功能
  9. Android(安卓)数据存数---SQLite数据库
  10. 升级Android内置apk版本