1、在对应模块的build.gradle文件中,添加依赖

//VlC
implementation "de.mrmaffen:vlc-android-sdk:2.0.6"

2、布局文件中加入SurfaceView要作为VLC的容器 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".VLCActivity"> <FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <SurfaceView
android:id="@+id/surView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
</FrameLayout>
</RelativeLayout>

3、VLC播放视频

  

    String url ="网络视频的路径";

        mSurface = findViewById(R.id.surView);
options.add("--aout=opensles");
options.add("--audio-time-stretch"); // time stretching
options.add("-vvv"); // verbosity
libVLC = new LibVLC(getApplication(), options);
try {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer = new MediaPlayer(libVLC); holder = mSurface.getHolder();
holder.setKeepScreenOn(true);
mediaPlayer.getVLCVout().setVideoSurface(holder.getSurface(), mSurface.getHolder());
mediaPlayer.getVLCVout().addCallback(this);
//播放前还要调用这个方法
mediaPlayer.getVLCVout().attachViews(); final Media media = new Media(libVLC, Uri.parse(url)); mediaPlayer.setEventListener(new MediaPlayer.EventListener() {
@Override
public void onEvent(MediaPlayer.Event event) {
Log.d("MediaPlayer", "onEvent: " + String.valueOf(event.type));
if (event.type == MediaPlayer.Event.EndReached) {
Log.d("MediaPlayer", "onEvent: MediaPlayer.Event.EndReached");
mediaPlayer.setMedia(media);
mediaPlayer.play();
}
}
});
mediaPlayer.setMedia(media);
mediaPlayer.play(); } catch (Exception e) {
e.printStackTrace();
}
@Override
protected void onPause() {
super.onPause();
if (mediaPlayer != null) {
mediaPlayer.pause();
}
} @Override
protected void onResume() {
super.onResume();
if (mediaPlayer != null) {
mediaPlayer.play();
}
} @Override
protected void onDestroy() {
super.onDestroy();
releasePlayer();
} // TODO: handle this cleaner
private void releasePlayer() {
if (libVLC == null)
return;
mediaPlayer.stop();
final IVLCVout vout = mediaPlayer.getVLCVout();
vout.removeCallback(this);
vout.detachViews();
holder = null;
libVLC.release();
libVLC = null; mVideoWidth = 0;
mVideoHeight = 0;
}

4、Activity 需要实现IVLCVout.Callback 接口,当屏幕切换的时候 改变SurfaceView的大小

  改变SurfaceView的大小的方法

/*************
* Surface
*************/
private void setSize(int width, int height) {
mVideoWidth = width;
mVideoHeight = height;
if (mVideoWidth * mVideoHeight <= 1)
return; if (holder == null || mSurface == null)
return; // get screen size
int w = getWindow().getDecorView().getWidth();
int h = getWindow().getDecorView().getHeight(); // getWindow().getDecorView() doesn't always take orientation into
// account, we have to correct the values
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
if (w > h && isPortrait || w < h && !isPortrait) {
int i = w;
w = h;
h = i;
} float videoAR = (float) mVideoWidth / (float) mVideoHeight;
float screenAR = (float) w / (float) h; if (screenAR < videoAR)
h = (int) (w / videoAR);
else
w = (int) (h * videoAR); // force surface buffer size
holder.setFixedSize(mVideoWidth, mVideoHeight); // set display size
ViewGroup.LayoutParams lp = mSurface.getLayoutParams();
lp.width = w;
lp.height = h;
mSurface.setLayoutParams(lp);
mSurface.invalidate();
}

  调用地方:

  @Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setSize(mVideoWidth, mVideoHeight);
}
 @Override
public void onNewLayout(IVLCVout vlcVout, int width, int height, int visibleWidth, int visibleHeight, int sarNum, int sarDen) {
if (width * height == 0)
return; // store video size
mVideoWidth = width;
mVideoHeight = height;
setSize(mVideoWidth, mVideoHeight);
} @Override
public void onSurfacesCreated(IVLCVout vlcVout) { } @Override
public void onSurfacesDestroyed(IVLCVout vlcVout) { } @Override
public void onHardwareAccelerationError(IVLCVout vlcVout) { }

Android内嵌VLC实现播放网络视频,网络音频的更多相关文章

  1. cocos2d-x调用android内嵌浏览器打开网页

    cocos2d-x调用android内嵌浏览器打开网页,能够从入口传入网址,C++调用android 的api就可以实现. 方法也非常easy 1. 改动"cocos2dx\platform ...

  2. Android开发 MediaPlayer入门_播放本地视频

    前言 MediaPlayer,可以播放视频/音频,并且它支持本地和网络文件的播放.本片博客作为入门教程,先以最通俗的方式解释播放文件本地视频.(如果你嫌MediaPlayer还是太麻烦可以试试选择Vi ...

  3. Android中webview html5 自动播放本地视频

    MainActivity代码 public class Html5VideoAutoPlay extends Activity { WebView webview = null; @Override ...

  4. Android内嵌网页webview点击其中的链接跳转到我们应用内的Activity

    在一个大的Android项目中,由于客户端来不及更新和实现,经常会内嵌一些网页(在一些大型的互联网公司,PC的产品总是跑在客户端的前面),比如活动页面,通常可以内嵌用html5实现的页面,可以适配手机 ...

  5. android内嵌H5页(webview)如何定位

    一.切换至webview后再定位元素 (1)获取页面上下文 contexts = driver.contexts (2)切换至webview driver.switch_to.context(cont ...

  6. SAP播放本地视频及音频(仅限于window MediaPlayer可播放文件)

    这个是从SCN上看到的,自己稍加修改,编制,做的还可以,可以播放视频,音频,唯一的不足就是不能控制播放视频的显示窗口大小,希望有人能帮忙解决,感激! 视频播放类:(新建类Z_CL_MEDIA,点击基于 ...

  7. 一处折腾笔记:Android内嵌html5加入原生微信分享的解决的方法

    有一段时间没有瞎折腾了. 这周一刚上班萌主过来反映说:微信里面打开聚客宝.分享功能是能够的(这里是用微信自身的js-sdk实现的).可是在android应用里面打开点击就没反应了:接下来狡猾的丁丁在产 ...

  8. Android内嵌PDF预览

    一.在对应模块的build.gradle文件中加入依赖 dependencies { implementation 'com.github.barteksc:android-pdf-viewer:3. ...

  9. [Unity3D]Unity3D游戏开发Android内嵌视图Unity查看

    ---------------------------------------------------------------------------------------------------- ...

随机推荐

  1. Ubuntu 18.04.1 搭建Java环境和HelloWorld

    一.搭建Java环境 系统环境 Ubuntu 18.04.1 JDK 8 IDEA 2018.2 1.下载JDK 官网地址:http://www.oracle.com/technetwork/java ...

  2. 浅谈缓存技术在ASP.NET中的运用

    本篇文章虽不谈架构,但是Cache又是架构中不可或缺的部分,因此,在讲解Cache的同时,将会提及到部分架构知识,关于架构部分,读者可以不用理解,或者直接跳过, 你只需关心Cache即可,具体的架构, ...

  3. Docker虚拟机实战

    安装Docker虚拟机 ◆ 先更新yum软件管理器,然后再安装Docker    #更新yum软件管理器    yum -y update    #安装Docker    yum install -y ...

  4. 8. 同步锁Lock

    package com.gf.demo07; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Ree ...

  5. D. GukiZ and Binary Operations(矩阵+二进制)

    D. GukiZ and Binary Operations   We all know that GukiZ often plays with arrays. Now he is thinking ...

  6. Java8 默认方法

    概述 Java8新增了接口的默认方法.使用default关键字. 默认方法就是接口可以有实现方法,而且不需要实现类来实现其方法.相对于JDK1.8之前的接口来说,新增了可以接口中实现方法. 可以说在接 ...

  7. JavaScript是如何工作的:事件循环和异步编程的崛起 + 5种使用 async/await 更好地编码方式!

    摘要: 深度理解JS事件循环!!! 原文:JavaScript是如何工作的:事件循环和异步编程的崛起+ 5种使用 async/await 更好地编码方式! 作者:前端小智 Fundebug经授权转载, ...

  8. JS中substring与substr的用法

    substring方法用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引javascript 参数 描述 start 必需.一个非负 ...

  9. Angular6 组件树结构优化

    本片博客主要是记录实际项目开发中使用Angular6框架,遇到的一个问题. 现象: Angular6框架写的前端web网页,在实际部署运行过程中遇到了一种现象,引入懒加载以后,加载登录面速度很快,但是 ...

  10. es6 语法 (symbol)

    { // 声明 let a1 = Symbol(); let a2 = Symbol(); console.log(a1 === a2); //false let a3 = Symbol.for('a ...