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. CodeForces #362 div2 B. Barnicle

    题目链接: B. Barnicle 题意:给出科学计数法 转化成十进制的整数或小数 并输出. 思路:暑假训练赛见过了,当时大腿A掉了,并表示是道水题. 刷CF再次遇见,毫不留情WA了几次.比如: 0. ...

  2. 移动web开发问题集

    一.让微信内置浏览器(x5)支持 flex .item-flex { display: -webkit-box; -webkit-box-pack: center; -webkit-box-align ...

  3. LLBLGen Pro v4.2_Patch+Keygen

    将dll文件覆盖安装目录下的文件,之后用算号器算出license文件,将license文件放在安装目录下即可. 算号器是在http://www.dxper.net/thread-408-1-1.htm ...

  4. 【转】Tomcat的默认访问路径

    放在外网的应用,用户多是直接输入域名访问,相信没有哪个后面还加个尾巴,而Tomcat的默认目录是ROOT,所以我们需要更改其默认目录. 更改Tomcat的默认目录很简单,只需要修改server.xml ...

  5. 编译内核实现iptables防火墙layer7应用层过滤 (三)

    在前面的两篇文章中我们主要讲解了Linux防火墙iptables的原理及配置规则,想博友们也都知道iptables防火墙是工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙.以 ...

  6. ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务解决

    先看oracle的监听和oracle的服务是否都启动了. 启动oracle监听:cmd命令行窗口下,输入lsnrctl start,回车即启动监听. 查看oracle的sid叫什么,比如创建数据库的时 ...

  7. jQuery lazyload 懒加载

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  8. 【python】闭包、@修饰符(装饰器)、

    闭包:(返回函数的行为叫闭包??) #函数也是对象,所以可以被传递 def line_conf(a,b): def line(x): return a*x+b return line line1=li ...

  9. __autoload的小tip

    __autoload魔法函数不仅在如 $a=new testClass();时可以触发.在 class a extends b时 如果b类的定义在当前页未找到,也可以触发这个函数

  10. C#为什么不采用多继承:

    C#为什么不采用多继承: 1.继承从来未被广泛使用. 2.多继承带来的问题往往多于其解决的问题. 3.多继承会引起潜在的歧义. C#核心编程