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>

android实现控制视频播放次数的更多相关文章

  1. android 自己定义视频播放器之2/1

    非常久没更新博客,相信大家年后都比較忙. 今天给大家带来了一款视频播放器,首先确认的得有几点. 1.首先得有个播放视频的view. 2.加点额外功能进去左边上下滑动调节亮度,右边上下滑动调节声量: 3 ...

  2. 开源安卓Android流媒体音视频播放器实现声音自动停止、恢复、一键静音功能源码

    本文转自EasyDarwin团队John的博客:http://blog.csdn.net/jyt0551/article/details/60802145 我们在开发安卓Android流媒体音视频播放 ...

  3. Android html5 控制video currentTime不准确,精确,解决办法。

    早在flash时代 我们控制视频播放指定时间位置的画面也会有不准确的情况, 具体情况表现为:video.seek(time)   而实际画面会跳到此时间附近(1-2秒)的画面 而HTML5 我们通过 ...

  4. Android(Linux)控制GPIO方法二

    前文<Android(Linux)控制GPIO的方法及实时性分析>主要使用Linux shell命令控制GPIO,该方法可在调试过程中快速确定GPIO硬件是否有问题,即对应的GPIO是否受 ...

  5. Visual Studio跨平台开发实战(4) - Xamarin Android基本控制项介绍

    原文 Visual Studio跨平台开发实战(4) - Xamarin Android基本控制项介绍 前言 不同于iOS,Xamarin 在Visual Studio中针对Android,可以直接设 ...

  6. Android几种视频播放方式,VideoView、SurfaceView+MediaPlayer、TextureView+MediaPlayer,以及主流视频播放器开源项目

    简单的说下一Android的几种视频播放功能: 1.VideoView:最简单的视频播放 <FrameLayout xmlns:android="http://schemas.andr ...

  7. ionic3 生成android 如何控制versionCode版本号

    ionic 项目中生成 android 如何控制版本号呢. 1.在项目的配置文件下的config.xml 来我们可以看到 <widget id="com.ionicframework. ...

  8. android线程控制UI更新(Handler 、post()、postDelayed()、postAtTime)

    依照以下的理解就是handler与ui线程有一定的关联能够由于更新界面仅仅能在主线程中全部更新界面的地方能够在接受消息的handleMessage那里还有更新界面能够在handler.port(new ...

  9. 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(三)Android客户端功能实现

    我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端(地址:http://blog.csdn.net/ouyang_pen ...

随机推荐

  1. 常用的web功能测试方法

    功能测试就是对产品各功能进行验证,根据功能测试用例,逐项测试,检查产品是否达到用户要求功能,即是否满足需求.常用的测试方法如下: 1.页面连接检查:每一个连接是否都有对应的页面,并且页面之间切换正确. ...

  2. js变量及其作用域

    Javascript和Java.C这些语言不同,它是一种无类型.弱检测的语言.它对变量的定义并不需要声明变量类型,我们只要通过赋值的形式,可以将各种类型的数据赋值给同一个变量   一.js变量的类型及 ...

  3. Yii2.0 rules验证规则大全

    required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredV ...

  4. pod

    在运行 “sudo gem install cocoapods” 的时候出现问题:ERROR: While executing gem ... (Errno::EPERM)Operation not ...

  5. Flapper Bird的学习笔记(三)

    因为我有一个超屌的梦想,所以就绝不会做一个孬种的追梦人! 完成音效的添加 单例模式 游戏的状态切换 1. 单例模式 首先呢,说一下单例模式.何为单例?单例模式是一种常用的软件设计模式.在它的核心结构中 ...

  6. Paths_Quartz2D

    Paths中的几个重要元素 Points void CGContextMoveToPoint (    CGContextRef c,    CGFloat x,    CGFloat y ); 指定 ...

  7. TPCH Benchmark with Impala

    1. 生成测试数据在TPC-H的官网http://www.tpc.org/tpch/上下载dbgen工具,生成数据http://www.tpc.org/tpch/spec/tpch_2_17_0.zi ...

  8. M1/M2个人总结

    软件工程整个学期结束了,很开心学了这门课,在学到知识的同时也提高了自己的动手实践的能力,感觉自己在整个软件工程的各个环节中都能有所把握,可以将学到的知识运用到设计.实践更多的项目中去. M1阶段个人总 ...

  9. js动画之缓冲运动

    缓冲运动就是运动的速度与时间或者距离有关联,不是一般的匀速运动 <!DOCTYPE html> <html lang="en"> <head> ...

  10. Sprint 2(第一天)

    Sprint 2计划会议: 目标: 1.实现用户模块的权限控制,能够进行用户登录的功能 2.对菜单模块实现增加菜单列表详情,修改菜单列表详情,删除菜单列表详情,查询菜单列表详情的功能 3.实现菜品分类 ...