当前位置 : 主页 > 手机开发 > android >

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

来源:互联网 收集:自由互联 发布时间:2021-05-17
使用第三方的vitamio插件实现简易的播放器。vitamio版本(5.2.3) 官网地址: 官网地址 效果展示 效果 项目结构 代码: MainActivity package com.example.www.app;import android.app.ListActivity;import andro

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

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

网友评论