<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(安卓)属性动画中心点无限循环
  2. Android(安卓)MediaPlayer Playback---多媒体开发应用程序接口
  3. 在Android的Notification中显示进度条
  4. Android(安卓)– Video/Music 视频音乐播放
  5. Android录音功能和播放录音功能的示例源码
  6. Android的Button监听
  7. 【Android】播放提示音
  8. Android(安卓)Intent用法详解
  9. android监听SD卡状态

随机推荐

  1. Android Dalvik初探(一)
  2. 短视频直播系统开发Android中常用控件
  3. does not specify a android.test.Instru
  4. Android TextView设置一个或多个关键字的
  5. android 调用系统的照相机和图库实例详解
  6. android 数据库初体验
  7. android里,addContentView()动态增加view
  8. Android 自定义系统菜单的背景源码
  9. android中获取wifi信息
  10. Android AlertDialog去除白色边框