终于把该忙的事情都忙得差不多了,接下来又可以开始good good study,day day up了。在Android开发学习之路–基于vitamio的视频播放器(一)中,主要讲了播放器的界面的简单实现,以及扫描视频文件,获取视频文件的部分信息,还没开始讲解如何使用vitamio这个库,这里就开始讲解下最简单的使用方法吧。

1.接口的简单使用

  layout界面:

<?xml version="1.0" encoding="utf-8"?>
<layout>
<data class="LocalPlayerBinding"></data> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<io.vov.vitamio.widget.CenterLayout
android:id="@+id/dd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <io.vov.vitamio.widget.VideoView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</io.vov.vitamio.widget.CenterLayout> <TextView
android:id="@+id/subtitle_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:text="111" />
</LinearLayout>
</layout>

  io.vov.vitamio.widget.CenterLayout是vitamio提供的widget居中布局,然后io.vov.vitamio.widget.VideoView就是显示的播放界面了,其实主要就是个surfaceview。接着看下activity代码:

package com.jared.jplayer.ui;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.widget.Toast; import com.jared.jplayer.R;
import com.jared.jplayer.app.BaseActivity;
import com.jared.jplayer.common.MyMediaController;
import com.jared.jplayer.databinding.LocalPlayerBinding; import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.Vitamio; /**
* Created by jared on 2016/9/29.
*/
public class LocalVideoPlayer extends BaseActivity { private String subtitle_path = "";
private long mPosition = 0; private LocalPlayerBinding binding; public static void launch(Context context, String url) {
Intent intent = new Intent(context, LocalVideoPlayer.class);
intent.putExtra("url", url);
context.startActivity(intent);
} @Override
protected void initParams() {
Vitamio.isInitialized(getActivity());
binding = DataBindingUtil.setContentView(getActivity(), R.layout.activity_local_player);
} @Override
protected void initViews() {
super.initViews();
String url = getActivity().getIntent().getStringExtra("url");
initVideo("file://"+url);
} @Override
protected void onPause() {
mPosition = binding.surfaceView.getCurrentPosition();
binding.surfaceView.stopPlayback();
super.onPause();
} @Override
protected void onResume() {
if (mPosition > 0) {
binding.surfaceView.seekTo(mPosition);
mPosition = 0;
}
super.onResume();
binding.surfaceView.start();
} private void initVideo(String path) { if (path == "") {
// Tell the user to provide a media file URL/path.
Toast.makeText(getActivity(), "Please edit VideoViewSubtitle Activity, and set path" + " variable to your media file URL/path", Toast.LENGTH_LONG).show();
return;
} else { binding.surfaceView.setVideoPath(path); MyMediaController myMediaController = new MyMediaController(getActivity());
binding.surfaceView.setMediaController(myMediaController);
binding.surfaceView.requestFocus(); binding.surfaceView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// optional need Vitamio 4.0
mediaPlayer.setPlaybackSpeed(1.0f);
binding.surfaceView.addTimedTextSource(subtitle_path);
binding.surfaceView.setTimedTextShown(true); }
});
binding.surfaceView.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() { @Override
public void onTimedText(String text) {
binding.subtitleView.setText(text);
} @Override
public void onTimedTextUpdate(byte[] pixels, int width, int height) { }
});
}
}
}

  这里看下initVideo代码,binding.surfaceView.setVideoPath(path);设置播放的路径,这里的路径是之前的播放列表中已经扫描出来的然后解析出来通过launch方法传递给LocalVideoPlayer的。这里自定义了controller,然后通过controller就可以控制播放暂停等功能了,接着来看controller。

2.自定义MediaController

package com.jared.jplayer.common;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View; import com.jared.jplayer.R; import io.vov.vitamio.widget.MediaController; /**
* Created by jared on 2016/9/28.
*/
public class MyMediaController extends MediaController { Context mContext; public MyMediaController(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
} public MyMediaController(Context context) {
super(context);
mContext = context;
} @Override
protected View makeControllerView() {
return ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.mymediacontroller, this);
//return super.makeControllerView();
} }

  继承了MediaController,重新的布局了界面,接着看下新的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="74dp"
android:background="@color/mediacontroller_bg"
android:orientation="vertical" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageButton
android:id="@+id/mediacontroller_play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@drawable/mediacontroller_button"
android:contentDescription="@string/mediacontroller_play_pause"
android:src="@drawable/mediacontroller_pause" /> <TextView
android:id="@+id/mediacontroller_time_current"
style="@style/MediaController_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/mediacontroller_play_pause" /> <TextView
android:id="@+id/mediacontroller_time_total"
style="@style/MediaController_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp" /> <SeekBar
android:id="@+id/mediacontroller_seekbar"
style="@style/MediaController_SeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/mediacontroller_time_total"
android:layout_toRightOf="@id/mediacontroller_time_current"
android:focusable="true"
android:maxHeight="4dp"
android:minHeight="4dp"
android:progressDrawable="@drawable/seekbar_define_color_style"
android:thumb="@drawable/seekbar_thumb"
android:max="1000" />
</RelativeLayout> <TextView
android:id="@+id/mediacontroller_file_name"
style="@style/MediaController_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:ellipsize="marquee"
android:singleLine="true" /> </LinearLayout>

  注意的是这里的id的名字必须不能变,因为vitamio的mediacontroller会根据id来做对应的事情的。

  vitamio的接口还是比较简洁的,省去了很多事情,需要考虑的事情基本上都做好了,它是基于ffmpeg做的,之后有机会也可以深入学习下。

参考源码:https://github.com/imchenjianneng/JPlayer

Android开发学习之路--基于vitamio的视频播放器(二)的更多相关文章

  1. Android开发学习之路--基于vitamio的视频播放器(一)

      之前也试过vitamio这个库,后来不知道被什么事情给耽搁了,就没继续下去.近来觉得视频还是需要学习一下的,谁让直播那么火呢,就想着写一个简单的视频播放的app先吧.好了那就开始吧,暂时取名为JP ...

  2. Android开发学习之路--Android系统架构初探

    环境搭建好了,最简单的app也运行过了,那么app到底是怎么运行在手机上的,手机又到底怎么能运行这些应用,一堆的电子元器件最后可以运行这么美妙的界面,在此还是需要好好研究研究.这里从芯片及硬件模块-& ...

  3. Android开发学习之路-RecyclerView滑动删除和拖动排序

    Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...

  4. Android开发学习之路--Android Studio cmake编译ffmpeg

      最新的android studio2.2引入了cmake可以很好地实现ndk的编写.这里使用最新的方式,对于以前的android下的ndk编译什么的可以参考之前的文章:Android开发学习之路– ...

  5. Android开发学习之路--网络编程之xml、json

    一般网络数据通过http来get,post,那么其中的数据不可能杂乱无章,比如我要post一段数据,肯定是要有一定的格式,协议的.常用的就是xml和json了.在此先要搭建个简单的服务器吧,首先呢下载 ...

  6. Android开发学习之路--Activity之初体验

    环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...

  7. Android开发学习之路--MAC下Android Studio开发环境搭建

    自从毕业开始到现在还没有系统地学习android应用的开发,之前一直都是做些底层的驱动,以及linux上的c开发.虽然写过几个简单的app,也对android4.0.3的源代码做过部分的分析,也算入门 ...

  8. Android开发学习之路-记一次CSDN公开课

    今天的CSDN公开课Android事件处理重难点快速掌握中老师讲到一个概念我觉得不正确. 原话是这样的:点击事件可以通过事件监听和回调两种方法实现. 我一听到之后我的表情是这样的: 这跟我学的看的都不 ...

  9. Android开发学习之路-Android Studio开发小技巧

    上一次发过了一个介绍Studio的,这里再发一个补充下. 我们都知道,Android Studio的功能是非常强大的,也是很智能的.如果有人告诉你学Android开发要用命令行,你可以告诉他Andro ...

随机推荐

  1. 如何彻底解决MySQL更改默认字符集以及字符乱码问题!!!

    在我们使用MySQL数据库时,字符乱码,对我们来说是一个很头疼的问题.今天笔者就来教大家如何彻底解决更改默认字符集以及字符乱码问题. 当我们使用压缩包进行MySQL安装后,系统会使用默认的字符集,这时 ...

  2. Web SCADA 电力接线图工控组态编辑器

    前言 SVG并非仅仅是一种图像格式, 由于它是一种基于XML的语言,也就意味着它继承了XML的跨平台性和可扩展性,从而在图形可重用性上迈出了一大步.如SVG可以内嵌于其他的XML文档中,而SVG文档中 ...

  3. java加密算法AES与RSA

    1.commons-codec使用 commons-codes 常用工具类:DigestUtils,Base64,Hex 1.1 md5 String text = "hello,md5&q ...

  4. Bootstrap File Input 中文文档

    手动安装 您也可以手动地安装插件到你的项目中.只要下载源ZIP或TAR球和提取资产(CSS和JS插件文件夹)到你的项目中. 使用 步骤1:在你页面头部加载以下类库. <link href=&qu ...

  5. RPO(Relative Path Overwrite)

    Conception(Relative vs Absolute) Abosolute Path: "/etc/hosts"(in Linux), "C:\Windows\ ...

  6. 页面js脚本与img等资源的下载顺序问题。

    引言问题 <img src="background.jpg"><script src="test.js"></script> ...

  7. [LeetCode] Set Intersection Size At Least Two 设置交集大小至少为2

    An integer interval [a, b] (for integers a < b) is a set of all consecutive integers from a to b, ...

  8. 网络安全实验室_上传关writeup

    请上传一张jpg格式的图片 先传个图片码试试 我肯定乖嘛(#`Д´)ノ 气到改后缀 请上传一张jpg格式的图片 我猜是00截断,不信来试试 先在赋值1.php .jpg,接着去hex中找到空格改成00 ...

  9. 详解vue移动端 下拉刷新

    看完这篇文章,相信大伙也一样可以,做出一个自己的刷新,加载的组件 说这个功能之前,大家要先了解一下,要怎么触发滚动条事件. 一定要注意,所有滚动事件都必须要满足这个条件,横向滚动条也一样, 只要满足子 ...

  10. [SDOI 2012]Longge的问题

    Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). Input 一 ...