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>
android实现控制视频播放次数的更多相关文章
- android 自己定义视频播放器之2/1
非常久没更新博客,相信大家年后都比較忙. 今天给大家带来了一款视频播放器,首先确认的得有几点. 1.首先得有个播放视频的view. 2.加点额外功能进去左边上下滑动调节亮度,右边上下滑动调节声量: 3 ...
- 开源安卓Android流媒体音视频播放器实现声音自动停止、恢复、一键静音功能源码
本文转自EasyDarwin团队John的博客:http://blog.csdn.net/jyt0551/article/details/60802145 我们在开发安卓Android流媒体音视频播放 ...
- Android html5 控制video currentTime不准确,精确,解决办法。
早在flash时代 我们控制视频播放指定时间位置的画面也会有不准确的情况, 具体情况表现为:video.seek(time) 而实际画面会跳到此时间附近(1-2秒)的画面 而HTML5 我们通过 ...
- Android(Linux)控制GPIO方法二
前文<Android(Linux)控制GPIO的方法及实时性分析>主要使用Linux shell命令控制GPIO,该方法可在调试过程中快速确定GPIO硬件是否有问题,即对应的GPIO是否受 ...
- Visual Studio跨平台开发实战(4) - Xamarin Android基本控制项介绍
原文 Visual Studio跨平台开发实战(4) - Xamarin Android基本控制项介绍 前言 不同于iOS,Xamarin 在Visual Studio中针对Android,可以直接设 ...
- Android几种视频播放方式,VideoView、SurfaceView+MediaPlayer、TextureView+MediaPlayer,以及主流视频播放器开源项目
简单的说下一Android的几种视频播放功能: 1.VideoView:最简单的视频播放 <FrameLayout xmlns:android="http://schemas.andr ...
- ionic3 生成android 如何控制versionCode版本号
ionic 项目中生成 android 如何控制版本号呢. 1.在项目的配置文件下的config.xml 来我们可以看到 <widget id="com.ionicframework. ...
- android线程控制UI更新(Handler 、post()、postDelayed()、postAtTime)
依照以下的理解就是handler与ui线程有一定的关联能够由于更新界面仅仅能在主线程中全部更新界面的地方能够在接受消息的handleMessage那里还有更新界面能够在handler.port(new ...
- 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(三)Android客户端功能实现
我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端(地址:http://blog.csdn.net/ouyang_pen ...
随机推荐
- Spring整合HBase
Spring整合HBase Spring HBase SHDP § 系统环境 § 配置HBase运行环境 § 配置Hadoop § 配置HBase § 启动Hadoop和HBase § 创建Maven ...
- DB
数据库(DB):1:降低存储数据的冗余度.2:更高的数据一致性.3:存储的数据可以共享.4:可以建立数据库所遵循的标准. 关系型数据库(RDBMS):基本单位就是表.一张表就是一个实体.MYSQL语句 ...
- 多线程完成socket
//服务器端代码 public class Service { //服务器 public static void main(String[] args) { ServerSocket serverSo ...
- spider_jpg
__author__ = 'sus' #urllib模块提供了读取Web页面数据的接口import urllib#re模块主要包含了正则表达式import re#定义一个getHtml()函数def ...
- UnityContainer 实现DI
DI(依赖注入) 的方式有很多种: 接口注入,属性注入,构造注入等.DI主要是为了实现代码的松耦合,方便代码的维护和扩展.(其实都是扯淡). 来说说我为啥要使用DI吧.公司有个项目,需要我一个人完成( ...
- js图片拖放原理(很简单,不是框架,入门基础)
<html> <meta> <script src='jquery-1.8.3.min.js'></script> <script> /* ...
- Titanium studio安装
在Win7 Titanium Studio的安装过程. 1.准备工作 Titanium存储空间的要求,Titanium Studio 需要1 GB.Android SDK需要1.5 GB.Blackb ...
- oracle 用户创建这个挺靠谱
CREATE TEMPORARY TABLESPACE test_tempTEMPFILE 'C:\oracle\product\10.1.0\oradata\orcl\test_temp01.dbf ...
- Perl技巧
项目里面一直用的是Perl,Perl里有各种小技巧就分享在这吧. push(@a, $b) 把b元素压入a数组中, 还可以有 push(@a, [@b]); 那a就成了二维数组了 scalar(@a) ...
- 对客户推荐产品模型+python代码
首先观看数据: l 数据的基本特征用 describe 描述每个基本特征 l 画图画出每个特征的基本统计图 应用import matplotlib.pylab as pl 画图显示 l 关 ...