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

 更新时间:2019年04月18日 10:47:50   作者:ITzhongzi  
这篇文章主要为大家详细介绍了Android使用vitamio实现视频播放器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

官网地址:官网地址

效果展示

效果

项目结构

代码:

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android中Activity常用功能设置小结(包括全屏、横竖屏等)

    Android中Activity常用功能设置小结(包括全屏、横竖屏等)

    这篇文章主要介绍了Android中Activity常用功能设置小结(包括全屏、横竖屏等),以简单实例形式分析了Android实现全屏、竖屏及一直显示等的技巧与注意事项,需要的朋友可以参考下
    2015-10-10
  • Android监听输入法弹窗和关闭的实现方法

    Android监听输入法弹窗和关闭的实现方法

    用过ios的都知道ios上输入法关闭的同时会自动关闭输入框,那么在android上如何实现监听输入法弹出和关闭呢?接下来通过本文给大家分享一种可靠的实现方式
    2016-11-11
  • Android开发之图形图像与动画(四)AnimationListener简介

    Android开发之图形图像与动画(四)AnimationListener简介

    就像Button控件有监听器一样,动画效果也有监听器,只需要实现AnimationListener就可以实现对动画效果的监听,感兴趣的朋友可以了解下啊,希望本文对你有所帮助
    2013-01-01
  • Flutter实现页面路由及404路由拦截

    Flutter实现页面路由及404路由拦截

    这篇文章介绍了Flutter实现页面路由及404路由拦截的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-11-11
  • Android Toast使用的简单小结(推荐)

    Android Toast使用的简单小结(推荐)

    这篇文章主要介绍了Android Toast使用的简单小结,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Android中RecycleView与ViewPager冲突的解决方法及原理

    Android中RecycleView与ViewPager冲突的解决方法及原理

    这篇文章主要给大家介绍了关于Android中RecycleView与ViewPager冲突的解决方法及原理的相关资料,以及ViewPager嵌套RecycleView卡顿问题的处理方法,文中通过示例代码介绍的非常狎昵,需要的朋友可以参考下
    2018-07-07
  • Android实现雷达View效果的示例代码

    Android实现雷达View效果的示例代码

    这篇文章主要介绍了Android实现雷达View效果,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • ANDROID应用程序的混淆打包分享

    ANDROID应用程序的混淆打包分享

    这篇文章主要介绍了ANDROID应用程序的混淆打包,有需要的朋友可以参考一下
    2014-01-01
  • 利用Kotlin的协程实现简单的异步加载详解

    利用Kotlin的协程实现简单的异步加载详解

    这篇文章主要给大家介绍了关于利用Kotlin的协程实现简单的异步加载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-03-03
  • Android 暂停和恢复Activity

    Android 暂停和恢复Activity

    在正常的应用程序使用,前台activity有时会被其他可视化组件遮挡,从而 造成activity的暂停。例如,当一个半透明的activity打开时(如在一个风格对话框),以前的activity就暂停了。只要 activity仍然是部分可见,但目前没有获得焦点,它就依然处于暂停状态
    2016-03-03

最新评论