欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-07 17:33:40
使用第三方的vitamio插件实现简易的播放器。vitamio版本(5.2.3) 官网地址:官网地址 效果展示 效果 项目结构 代码: mainacti...

使用第三方的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>

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