Android使用vitamio插件实现视频播放器

作者:ITzhongzi 时间:2022-09-23 02:40:55 

使用第三方的vitamio插件实现简易的播放器。vitamio版本(5.2.3)

官网地址:官网地址

效果展示

效果

Android使用vitamio插件实现视频播放器

项目结构

Android使用vitamio插件实现视频播放器

代码:

MainActivity


package com.example.www.app;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.vov.vitamio.Vitamio;

public class MainActivity extends ListActivity {

@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
//    setContentView(R.layout.activity_main);

Vitamio.isInitialized(getApplication());

setListAdapter(new SimpleAdapter(this, getData(), R.layout.list_item_main, new String[]{"title"}, new int[]{R.id.main_list_item}));

}

protected List<Map<String, Object>> getData() {
   List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();
//    addItem(myData, "MediaPlayer", new Intent(this, MediaPlayerDemo.class));
   addItem(myData, "VideoView", new Intent(this, VideoViewDemo.class));
//    addItem(myData, "MediaMetadata", new Intent(this, MediaMetadataRetrieverDemo.class));
//    addItem(myData, "VideoSubtitle", new Intent(this, VideoSubtitleList.class));
//    addItem(myData, "VideoViewBuffer", new Intent(this, VideoViewBuffer.class));
   return myData;
 }

protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
   Map<String, Object> temp = new HashMap<String, Object>();
   temp.put("title", name);
   temp.put("intent", intent);
   data.add(temp);
 }

@SuppressWarnings("unchecked")
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
   Map<String, Object> map = (Map<String, Object>) l.getItemAtPosition(position);
   Intent intent = (Intent) map.get("intent");
   startActivity(intent);
 }

}

VideoViewDemo


package com.example.www.app;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;

/**
* @author Administrator
* @name vitamioDemo
* @class name:com.example.www.app
* @class describe
* @time 2019/4/10 8:59
* @change
* @chang time
* @class describe
*/
public class VideoViewDemo extends AppCompatActivity {

private VideoView mVideoView;
 private Button mPlayBtn;
 private EditText mPlayUrl;

@Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

mVideoView = (VideoView) findViewById(R.id.surface_view);
   mPlayBtn = (Button) findViewById(R.id.playBtn);
   mPlayUrl = (EditText) findViewById(R.id.video_url);
   mPlayBtn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       playFunction(mPlayUrl.getText().toString());
     }
   });

playFunction("");
 }

void playFunction(String path){
   if(path.isEmpty()) {
     path = "http://gslb.miaopai.com/stream/3D~8BM-7CZqjZscVBEYr5g__.mp4";
   }

mVideoView.setVideoPath(path);

mVideoView.requestFocus();
   mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
     @Override
     public void onPrepared(MediaPlayer mp) {
       mp.setPlaybackSpeed(1.0f);

mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
         @Override
         public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
           MediaController controller = new MediaController(VideoViewDemo.this);
           mVideoView.setMediaController(controller);
           // and set its position on screen
           controller.setAnchorView(mVideoView);
         }
       });
     }

});
 }

}

activity_main.xml


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

<EditText
   android:id="@+id/video_url"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_marginStart="8dp"
   android:layout_marginTop="28dp"
   android:layout_marginEnd="8dp"
   android:ems="10"
   android:hint="请输入视频地址"
   android:inputType="textPersonName"
   app:layout_constraintEnd_toStartOf="@+id/playBtn"
   app:layout_constraintHorizontal_bias="1.0"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toTopOf="parent" />

<Button
   android:id="@+id/playBtn"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginEnd="8dp"
   android:text="play"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintTop_toTopOf="@+id/video_url"
   />

<io.vov.vitamio.widget.VideoView
   android:id="@+id/surface_view"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintHorizontal_bias="0.43"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toBottomOf="@+id/video_url" />

</android.support.constraint.ConstraintLayout>

list_item_main.xml


<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/main_list_item"
 android:layout_width="match_parent"
 android:layout_height="60dp"
 android:gravity="center_vertical"
 android:paddingStart="20dp"
 android:textAlignment="viewStart"
 android:textSize="24sp"
 android:textStyle="bold"
 tools:ignore="RtlCompat" />

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.www.app">

<uses-permission android:name="android.permission.WAKE_LOCK" />
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
   android:allowBackup="true"
   android:icon="@mipmap/ic_launcher"
   android:label="@string/app_name"
   android:roundIcon="@mipmap/ic_launcher_round"
   android:supportsRtl="true"
   android:theme="@style/AppTheme">
   <activity
     android:name="io.vov.vitamio.activity.InitActivity"
     android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
     android:launchMode="singleTop"
     android:theme="@android:style/Theme.NoTitleBar"
     android:windowSoftInputMode="stateAlwaysHidden" />
   <activity android:name=".MainActivity">
     <intent-filter>
       <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
   </activity>
   <activity android:name=".VideoViewDemo"></activity>
 </application>

</manifest>

来源:https://blog.csdn.net/ITzhongzi/article/details/89176668

标签:Android,vitamio,播放器
0
投稿

猜你喜欢

  • C++中求余运算符(%)示例详解

    2023-11-02 14:20:39
  • springboot整合EHCache的实践方案

    2023-08-23 23:48:31
  • c# 生成二维码的示例

    2021-09-17 14:03:26
  • 基于Java实现简单贪吃蛇游戏

    2022-08-07 02:09:44
  • C# 将透明图片的非透明区域转换成Region的实例代码

    2021-10-25 19:28:05
  • 全面分析Java文件上传

    2021-12-09 13:22:52
  • 基于@RequestBody和@ResponseBody及Stringify()的作用说明

    2021-12-14 01:47:38
  • Android内存优化杂谈

    2023-06-18 15:46:41
  • C#实现的json序列化和反序列化代码实例

    2022-04-05 22:24:08
  • C#函数式编程中的惰性求值详解

    2022-01-27 03:07:29
  • 使用C#实现读取系统配置文件的代码实例讲解

    2023-02-18 10:36:56
  • 注解处理器(APT)是什么

    2021-09-29 09:03:30
  • C#服务端图片打包下载实现代码解析

    2023-01-26 07:36:23
  • 教你如何使用Java输出各种形状

    2023-08-21 00:46:55
  • Android编程实现wifi扫描及连接的方法

    2022-11-21 21:11:36
  • android Handler详细使用方法实例

    2022-11-29 01:35:12
  • SpringBoot 读取yml文件的多种方式汇总

    2023-08-03 13:10:43
  • 为什么不要使用 async void的原因分析

    2023-11-24 21:10:27
  • Android RippleDrawable 水波纹/涟漪效果的实现

    2022-05-24 23:12:37
  • SpringBoot+easypoi实现数据的Excel导出

    2023-04-05 12:27:19
  • asp之家 软件编程 m.aspxhome.com