Android使用Vitamio来打造自己的视频播放器

Vitamio是一个优秀的视频框架,全面支持硬件解码与 GPU 渲染,下面就来介绍一下如何接入并使用。

1. 导入Vitamio

因为是Android Studio接入,所以直接下载这个库 https://github.com/yixia/VitamioBundleStudio

下载完成后将vitamio目录解压出来

进入vitamio目录,修改build.gradle文件,源文件的 ANDROID_BUILD_SDK_VERSION和ANDROID_BUILD_TOOLS_VERSION 我们是没有配置的,所以需要修改为与自己项目中app/build.gradle一样,不过 targetSdkVersion 需要<=22,兼容某些机型

compileSdkVersion 30
// buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
    minSdkVersion 21
    targetSdkVersion 22
}

接下来就可以导入到Android Studio,File - New - Import Module

2. 封装Vitamio

因为需要加一个进度条功能,所以将Vitamio封装起来更方便扩展。

public class VitamioManager {

	private static final int PROGRESS = 0x01;

	private VideoView mVideoView;
	private OnVideoProgressListener mVideoProgressListener;

	// 更新进度条
	private Handler mHandler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(@NonNull Message msg) {
			switch (msg.what) {
				case PROGRESS:
					if (mVideoProgressListener != null) {
						mVideoProgressListener.onProgress(getCurrentPosition());
						mHandler.sendEmptyMessageDelayed(PROGRESS, 1000);
					}
					break;
			}
			return false;
		}
	});

    // 视频播放进度接口
	public interface OnVideoProgressListener {
		void onProgress(long progress);
	}

	public VitamioManager(VideoView mVideoView) {
		this.mVideoView = mVideoView;
	}

	/**
	 * 设置文件路径
	 */
	public void setVideoPath(String path) {
		mVideoView.setVideoPath(path);
	}

	/**
	 * 设置网络路径
	 */
	public void setVideoURI(Uri uri) {
		mVideoView.setVideoURI(uri);
	}

	/**
	 * 设置播放速度 [0.5 - 2.0]
	 */
	public void setPlaybackSpeed(MediaPlayer mediaPlayer, float speed) {
		mediaPlayer.setPlaybackSpeed(speed);
	}

	/**
	 * 获取当前视频播放的位置
	 */
	public long getCurrentPosition() {
		return mVideoView.getCurrentPosition();
	}

	/**
	 * 获取当前视频总长度
	 */
	public long getDuration() {
		return mVideoView.getDuration();
	}

	/**
	 * 是否播放
	 */
	public boolean isPlaying() {
		return mVideoView.isPlaying();
	}

	/**
	 * 播放
	 */
	public void start() {
		mVideoView.start();
		mHandler.sendEmptyMessage(PROGRESS);
	}

	/**
	 * 暂停
	 */
	public void pause() {
		mVideoView.pause();
	}

	/**
	 * 重新播放
	 */
	public void resume() {
		mVideoView.seekTo(0);
	}

	/**
	 * 停止播放 释放资源
	 */
	public void stopPlayback() {
		mVideoView.stopPlayback();
		mHandler.removeMessages(PROGRESS);
	}

	/**
	 * 从第几毫秒开始播放
	 */
	public void seekTo(int msec) {
		mVideoView.seekTo(msec);
	}

	/**
	 * 视频播放完毕的监听
	 */
	public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
		mVideoView.setOnCompletionListener(listener);
	}

	/**
	 * 发生错误的监听
	 */
	public void setOnErrorListener(MediaPlayer.OnErrorListener listener) {
		mVideoView.setOnErrorListener(listener);
	}

	/**
	 * 视频加载完成的监听
	 */
	public void setOnPreparedListener(MediaPlayer.OnPreparedListener listener) {
		mVideoView.setOnPreparedListener(listener);
	}

	/**
	 * 视频进度监听
	 */
	public void setOnVideoProgress(OnVideoProgressListener listener) {
		this.mVideoProgressListener = listener;
	}
}
3. 使用

布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	tools:context=".MainActivity">

	<io.vov.vitamio.widget.VideoView
		android:id="@+id/vv_video"
		android:layout_width="match_parent"
		android:layout_height="match_parent"/>

	<androidx.constraintlayout.widget.ConstraintLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:background="#000"
		app:layout_constraintBottom_toBottomOf="parent">

		<ImageView
			android:id="@+id/iv_control"
			android:src="@drawable/mediacontroller_play"
			android:layout_width="40dp"
			android:layout_height="40dp"
			android:layout_margin="5dp"
			app:layout_constraintTop_toTopOf="parent"
			app:layout_constraintBottom_toBottomOf="parent"
			app:layout_constraintLeft_toLeftOf="parent"/>

		<SeekBar
			android:id="@+id/sb_bar"
			android:layout_width="0dp"
			android:layout_height="wrap_content"
			app:layout_constraintTop_toTopOf="parent"
			app:layout_constraintBottom_toBottomOf="parent"
			app:layout_constraintLeft_toRightOf="@id/iv_control"
			app:layout_constraintRight_toLeftOf="@id/tv_video_time"/>

		<TextView
			android:id="@+id/tv_video_time"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_margin="5dp"
			android:textColor="#fff"
			app:layout_constraintTop_toTopOf="parent"
			app:layout_constraintBottom_toBottomOf="parent"
			app:layout_constraintRight_toRightOf="parent"
			tools:text="10:30"/>

	</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

使用前一定要初始化Vitamio

Vitamio.isInitialized(getApplicationContext()); // 初始化Vitamio

接下来设置播放路径,监听视频加载完毕后就播放视频

mVitamioManager = new VitamioManager(mVvVideo);
mVitamioManager.setVideoPath(mVideoPath); // 设置播放路径

// 视频加载完成监听
mVitamioManager.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
	@Override
	public void onPrepared(MediaPlayer mp) {
		Log.i(TAG, "onPrepared: 视频加载完毕");
		mVitamioManager.setPlaybackSpeed(mp, 1.0f); // 设置播放速度
		long allDuration = mVitamioManager.getDuration(); // 获取视频总时长
		mVideoAllDuration = Utils.formatTime(allDuration); // 将视频总时长转换成 h:m:s
		mSbBar.setMax((int) allDuration); // 设置进度
		mVitamioManager.start(); // 播放视频
		setIvControl(false);
	}
});

别忘了清理资源

@Override
protected void onDestroy() {
	super.onDestroy();
	if (mVitamioManager != null) {
		mVitamioManager.stopPlayback(); // 停止播放
	}
}

Demo在此

其实有个问题,Vitamio在app返回后就会释放相关资源,点回app后就会自动加载资源并重新开始播放视频。还没仔细研究是什么原因造成的,所以没有实现app返回后暂停视频播放,点回app后点击播放按钮再继续播放视频。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android Vitamio是一个开源的跨平台多媒体播放框架,它支持播放多种音频格式,包括WAV格式。要在Android Vitamio中播放WAV文件,可以按以下步骤进行: 第一步:添加Vitamio库文件到项目中。在build.gradle文件中添加Vitamio库的依赖项。 第二步:在代码中初始化Vitamio库。在App的入口处(如MainActivity的onCreate方法中),调用Vitamio库的初始化方法,确保Vitamio库已经正确初始化。 第三步:创建一个MediaPlayer对象,用于播放音频文件。可以使用VitamioVitamio.createPlayer方法创建一个MediaPlayer对象。 第四步:设置音频文件的路径。使用MediaPlayer对象的setDataSource方法,传入要播放的WAV文件的路径。 第五步:准备MediaPlayer对象。调用MediaPlayer对象的prepareAsync方法,准备音频文件的播放。 第六步:设置MediaPlayer的监听器。可以注册一个MediaPlayer.OnPreparedListener监听器,用于在音频准备完成后播放音频。 第七步:开始播放音频。调用MediaPlayer对象的start方法,开始播放音频。 第八步:在适当的时机释放MediaPlayer对象。当音频播放完成或不再需要播放时,调用MediaPlayer对象的release方法,释放资源。 通过上述步骤,就可以使用Android Vitamio播放WAV文件了。需要注意的是,确保WAV文件的路径正确,并且要在AndroidManifest.xml文件中添加适当的权限,以允许访问存储器中的音频文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值