首页 > 其他分享 >Android开发学习之路--基于vitamio的视频播放器(二)

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

时间:2023-01-15 15:00:11浏览次数:61  
标签:layout -- binding vitamio id content import Android android


  终于把该忙的事情都忙得差不多了,接下来又可以开始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​


标签:layout,--,binding,vitamio,id,content,import,Android,android
From: https://blog.51cto.com/u_15940062/6008721

相关文章

  • 第8章 使用标记帮助工具构建表单(ASP.NET Core in Action, 2nd Edition)
    本章包括使用TagHelpers轻松构建表单使用锚标记帮助程序生成URL使用TagHelpers为Razor添加功能在第7章中,您了解了Razor模板以及如何使用它们为应用程序生成视图。......
  • 服务调用OpenFeign
    1、介绍①什么是OpenFeignOpenFeign是在Feign的基础上进行了加强使用在Client-Consuemr(消费者客户端)Fiegn是一个声明式的Web服务客户端,让编写Web服务客户端非常容易,只......
  • 二穗短柄草
    禾本科木聚糖中的一些阿拉伯呋喃糖是间隔均匀的,葡萄糖醛酸的修饰是成簇分布的。在芒属茎秆木聚糖中,阿拉伯呋喃糖的平均取代频率是11%,葡萄糖醛酸的替代是5%左右,但是这种取......
  • 中科大每日健康打卡平台的自动打卡脚本
    健康打卡脚本基本就是复现该大佬的工作,这是博客用Python实现中科大健康打卡脚本-逸風亭(fyz666.xyz)这是GitHub源码库GitHub-windshadow233/USTC-Auto-Health-Repor......
  • 迭代加深
    迭代加深迭代加深是用于优化搜索的,因为dfs的过程中是选择搜索的一个分支,不断地深入,直到我们达到递归的边界时才会返回。这样的话,如果搜索树的分支比较多,但答案在比较浅的......
  • Linux后台运行
    title:Linux后台运行date:2022-09-0312:51:35tags:-Linuxcategories:-Linux如何后台运行脚本方法1:nohup在执行命令前面加nohup但是CTRL+C就......
  • Web 开发人员日常工作的一天记录 All In One
    Web开发人员日常工作的一天记录AllInOneWeb开发人员每天做在什么?如果您要成为Web开发人员,您可能想知道会发生什么!你每天的生活会是什么样子?你会和谁一起工作?你......
  • spring boot——请求与参数校验——spring-mvc——通过形参获取请求参数——使用 @Req
     之前有一个遗留问题,如下:              packageorg.example.controller.requestparam;importorg.springframework.stereotype.Contr......
  • 中科大邮箱系统的附件上传的控件安装问题
    今天发现用邮箱上传附件的时候出现了附件大小的限制,超过48M的附件就会上传失败提示了不能使用拖拽上传,但是明明是用“添加附件”上传却依然是同样的提示,然后就注意到了......
  • C++相关总结
    在学习C++的时候,最初是在VS上输出HELLOWORLD,然后开始了C++学习之路,然后开始在b站等一些地方看相关视频,在过了一遍基础之后开始学习C++Primer.C++Primer这本书很厚,在看的......