exo视频
EXOPlayer是Google官方开源的一种播放器
效果
依赖
implementation 'com.google.android.exoplayer:exoplayer:r2.5.4'
代码
package com.example.mvpfour.activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.example.mvpfour.R;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Util;
/**
* Created by agen on 2018/8/31.
*/
public class ExoPlayerTwo extends AppCompatActivity implements View.OnClickListener {
private Button button9;
private SimpleExoPlayerView simpleExoPlayerView;
static Handler mainHandler = new Handler();
// step1. 创建一个默认的TrackSelector
// 创建带宽
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
// 创建轨道选择工厂
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
// 创建轨道选择器实例
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
//step2. 创建播放器
SimpleExoPlayer player;
private int resumeWindow;
private long resumePosition;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exoplayer_two);
initView();
initExoplayer();
}
private void initExoplayer() {
//step2. 创建播放器
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
// 创建加载数据的工厂
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "yourApplicationName"), (TransferListener<? super DataSource>) bandwidthMeter);
// 创建解析数据的工厂
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
// 传入Uri、加载数据的工厂、解析数据的工厂,就能创建出MediaSource
String url = "http://7xstkb.com1.z0.glb.clouddn.com/agen_apple.mp4";
Uri mp4VideoUri = Uri.parse(url);
MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri,
dataSourceFactory, extractorsFactory, null, null);
// Prepare
player.prepare(videoSource);
}
private void initView() {
button9 = (Button) findViewById(R.id.button9);
simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.simpleExoPlayerView);
button9.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button9:
startPlayer();
break;
}
}
private void startPlayer() {
simpleExoPlayerView.setPlayer(player);
player.setPlayWhenReady(true);
}
@Override
protected void onResume() {
super.onResume();
if (player != null&& player.getCurrentPosition()>0) {
player.setPlayWhenReady(true);
player.seekTo(resumePosition);
}
}
@Override
protected void onPause() {
super.onPause();
if (player != null && player.getPlayWhenReady()) {
resumeWindow = player.getCurrentWindowIndex();
resumePosition = Math.max(0, player.getContentPosition());
player.setPlayWhenReady(false);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//释放播放器
if (player != null) {
player.stop();
player.release();
player = null;
}
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/simpleExoPlayerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
参考
Android ExoPlayer实现网络URL、本地视频播放
ExoPlayer开发指南(官方文档翻译)
。。。
相关阅读
一、前言今天又到周末了,憋了好久又要出博客了,今天来介绍一下Android中的如何对Apk进行加固的原理。现阶段。我们知道Android中的
穿透内网,连接动态ip,内网ip打洞-----p2p实现原理
转自:http://www.cnblogs.com/eyye/archive/2012/10/23/2734807.html 网上找了很多,代码大堆,原理讲清楚透彻的不多。 本人找几篇讲
https://zhuanlan.zhihu.com/p/34805747 一个人做游戏然后实现盈利的经验 巫布斯 独立游戏开发者 关注他 伍一峰 等 117 人
Android位移动画之——TranslateAnimation实现大师哥
一、背景Android中的动画效果是一个非常强大的功能,在使用过程中极大地提高的用户的视觉以及操作体验,Animations是一个实现Android
这学期学习了python和javaWeb,所以就想着将javaWeb和python融合起来做个小项目,这里我实现的就是利用python爬取各个网站的视频。当