android实现控制视频播放次数

android实现控制视频播放次数,实质就是每个视频片段播放完后,通过MediaPlayer设置监听器setOnCompletionListener监听视频播放完毕,用Handler发送消息再次激活视频播放,从而达到控制播放次数的效果。视频文件foot_2_foot_crunch.mp4请放到assets文件夹下。

主界面代码如下:

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<include
android:id="@+id/topbar"
layout="@layout/topbar"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:gravity="center"
android:background="#4682B4">
<Button
android:id="@+id/button_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/play"
android:layout_gravity="center"
android:layout_marginRight="8dp"
android:layout_marginLeft="6dp"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="@+id/textview_display_time"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:textSize="16sp"
android:text="时间显示"
android:textColor="#ffffff"
android:gravity="center"
android:layout_alignParentRight="true"/>
<ProgressBar
android:id="@+id/progressbar"
android:layout_toLeftOf="@id/textview_display_time"
style="@style/pb_vedio"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:progress="0"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_above="@id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<SurfaceView
android:id="@+id/surfaceview_vedio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/relativelayout1"
android:layout_above="@id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview_tip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="25sp"
android:text="提醒"
android:textColor="#ffffff"
android:gravity="center"
android:background="#80696969"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center">
<Button
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_prev"/>
</LinearLayout>
<TextView
android:id="@+id/textview_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:paddingLeft="10dp"
android:text="0/30"
android:textColor="#000000"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_alignParentRight="true">
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_next"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>

topbar.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="vertical" >
<TextView
android:id="@+id/topbar_textview"
android:layout_width="fill_parent"
android:layout_height="38dp"
android:text="加载中..."
android:textSize="16sp"
android:textColor="#ffffff"
android:paddingLeft="6dp"
android:gravity="center"
android:background="#4682B4"/>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#2F4F4F"/>
</LinearLayout>

MainActivity.java

 package com.example.playvediodemo;

 import java.io.IOException;

 import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager; public class MainActivity extends Activity implements SurfaceHolder.Callback{
private MediaPlayer player;
private SurfaceView surface;
private SurfaceHolder surfaceHolder;
private Button button,button_left,button_right;
private TextView tv_display_time,tv_tip,tv_count,tv_title;
private ProgressBar pb;
private Handler handler;
private int count;//播放动过个数
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button_play);
surface = (SurfaceView)findViewById(R.id.surfaceview_vedio);
tv_display_time = (TextView)findViewById(R.id.textview_display_time);
button_left = (Button)findViewById(R.id.btn_left);
button_right = (Button)findViewById(R.id.btn_right);
tv_title = (TextView)findViewById(R.id.topbar_textview);
tv_tip = (TextView)findViewById(R.id.textview_tip);
tv_count = (TextView)findViewById(R.id.textview_count);
pb = (ProgressBar)findViewById(R.id.progressbar); player=new MediaPlayer();
surfaceHolder=surface.getHolder();//SurfaceHolder是SurfaceView的控制接口
surfaceHolder.addCallback(this); //因为这个类实现了SurfaceHolder.Callback接口,所以回调参数直接this
//surfaceHolder.setFixedSize(720, 480);//显示的分辨率,不设置为视频默认
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//设置SurfaceView自己不管理缓冲区
surfaceHolder.setKeepScreenOn(true);//设置播放视频时保持屏幕常亮 pb.setMax(30);
//定义一个Hander实现自动播放N次对应的视频片段
handler = new Handler(){
@Override
public void handleMessage(Message e){
if(e.what==0x1000){
player.start();
button.setBackgroundResource(R.drawable.pause);
pb.setProgress(count);
//一项锻炼完成,不用再次播放动画,仅仅为了让进度条可以填满
}else if(e.what==0x1001){
pb.setProgress(30);
}
}
};
//监听播放一个视频片段完成
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer arg0) {
if(count<(30-1)){
handler.sendEmptyMessage(0x1000);
count++;
tv_count.setText(count+"/"+30);
}else{
count=0;
handler.sendEmptyMessage(0x1001);
button.setBackgroundResource(R.drawable.play);
tv_tip.setVisibility(View.VISIBLE);
tv_tip.setText(30+"/"+30+" 动作完成.");
tv_count.setText(30+"/"+30);
}
}
});
//控制播放暂停的按钮
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(player.isPlaying()){
player.pause();
button.setBackgroundResource(R.drawable.play);
tv_tip.setVisibility(View.VISIBLE);
tv_tip.setText("暂停中");
}else{
player.start();
button.setBackgroundResource(R.drawable.pause);
tv_tip.setVisibility(View.GONE);
} }
});
//上一个视频
button_left.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) { }
});
//下一个视频
button_right.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) { }
});
} @Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { } @Override
public void surfaceCreated(SurfaceHolder holder) {
AssetManager assetManager = this.getAssets();
//必须在surface创建后才能初始化MediaPlayer,否则不会显示图像
player.reset();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDisplay(surfaceHolder);
try {
AssetFileDescriptor fileDescriptor = assetManager.openFd("foot_2_foot_crunch.mp4");
//设置显示视频显示在SurfaceView上
player.setDataSource(fileDescriptor.getFileDescriptor(),
fileDescriptor.getStartOffset(),
fileDescriptor.getLength());
player.prepare();
player.start();
button.setBackgroundResource(R.drawable.pause);
count=0;
tv_count.setText(count+"/"+30);
tv_tip.setVisibility(View.GONE);
} catch (IOException e) {
e.printStackTrace();
}
} @Override
public void surfaceDestroyed(SurfaceHolder holder) { }
@Override
protected void onDestroy() {
super.onDestroy();
if(player.isPlaying()){
player.stop();
}
player.release();//Activity销毁时停止播放,释放资源。不做这个操作,即使退出还是能听到视频播放的声音
}
}

values文件夹下styles.xml

 <resources>
<style name="pb_vedio" parent="@android:style/Widget.ProgressBar.Horizontal">
<item name="android:maxHeight">50dip</item>
<item name="android:minHeight">10dip</item>
<item name="android:indeterminateOnly">false</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
<item name="android:progressDrawable">@drawable/pb_vedio</item>
</style> </resources>

以下是两个个自定义button和一个自定义progressbar样式的代码,都放在drawable文件夹下

自定义进度条样式

pd_vedio.xml

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/background">
<shape >
<!-- corners android:radius="2dip" /-->
<gradient
android:angle="270"
android:centerY="0.75"
android:endColor="#AFEEEE"
android:startColor="#AFEEEE" />
<stroke
android:width="1dp"
android:color="#ffffff"/>
</shape>
</item> <item android:id="@android:id/secondaryProgress"> <clip >
<shape >
<!--corners android:radius="2dip" /-->
<gradient
android:angle="270"
android:centerY="0.75"
android:endColor="#A04682B4"
android:startColor="#A04682B4" />
<stroke
android:width="1dp"
android:color="#ffffff"/>
</shape>
</clip>
</item> <item android:id="@android:id/progress"> <clip >
<shape >
<!--corners android:radius="2dip" /-->
<gradient
android:angle="270"
android:centerY="0.75"
android:endColor="#A04682B4"
android:startColor="#A04682B4"/>
<stroke
android:width="1dp"
android:color="#ffffff"/>
</shape> </clip>
</item>
</layer-list>

btn_next.xml

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/next_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/next" android:state_focused="false" android:state_pressed="false"/>
<item android:drawable="@drawable/next_pressed" android:state_focused="true"/>
<item android:drawable="@drawable/next" android:state_focused="false"/>
</selector>

btn_prev.xml

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/prev_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/prev" android:state_focused="false" android:state_pressed="false"/>
<item android:drawable="@drawable/prev_pressed" android:state_focused="true"/>
<item android:drawable="@drawable/prev" android:state_focused="false"/>
</selector>
上一篇:成都印迹婚纱摄影 | yinjilove.com


下一篇:C#基于wpf编写的串口调试助手