java安卓播放器添加选集_Android使用vitamio插件实现视频播放器

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

官网地址:官网地址

效果展示

效果

10cec003bd1c92d6b3dbcd5fced3b6fd.gif

项目结构

93b0d6866e6f48171647ffbfed6b134c.png

代码:

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> getData() {

List> myData = new ArrayList>();

// 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> data, String name, Intent intent) {

Map temp = new HashMap();

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 map = (Map) 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

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">

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" />

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"

/>

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" />

list_item_main.xml

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

package="com.example.www.app">

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">

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" />

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值