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. 读写锁ReentrantReadWriteLock:读读共享,读写互斥,写写互斥

    介绍 DK1.5之后,提供了读写锁ReentrantReadWriteLock,读写锁维护了一对锁:一个读锁,一个写锁.通过分离读锁和写锁,使得并发性相比一般的排他锁有了很大提升.在读多写少的情况下, ...

  2. Python异常处理详解

    在shell脚本中,常用if来判断程序的某个部分是否可能会出错,并在if的分支中做出对应的处理,从而让程序更具健壮性.if判断是异常处理的一种方式,所有语言都通用.对于特性完整的编程语言来说,都有专门 ...

  3. 分布式系统监视zabbix讲解九之使用snmp监控windows--技术流ken

    前言 使用zabbix监控windows主要有两种方法,一种是在windows端安装zabbix-agent客户端工具,安装麻烦.另外一种是使用snmp协议,配置简单快捷.两种配置都可以实现同样的效果 ...

  4. JS_object添加变量属性_动态属性

    总结,给对象动态添加变量属性的方法如下: obj[变量]=变量值; 备注: obj.属性=属性值 ; obj={属性:属性值}; 这两种方式添加的属性都不能使用变量作为属性. 犯过的错误: var t ...

  5. Redis学习笔记(1)-安装Oracle VM VirtualBox

    Oracle VM VirtualBox官网网址 打开安装包网址界面,如下所示,点击截图红框. 下载完成后,点击exe文件,不停的点击下一步. 因为是使用MarkDown编辑器书写的尝试,所以写的简单 ...

  6. 【Java每日一题】20170208

    20170207问题解析请点击今日问题下方的“[Java每日一题]20170208”查看(问题解析在公众号首发,公众号ID:weknow619) package Feb2017; public cla ...

  7. css重写checkbox样式

    一.前言 默认的checkbox长这样: <p> <span><input type="checkbox" /></span> &l ...

  8. Java中的强引用和弱引用

    旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/81114944 一.强引用 如下是强引用的经典 ...

  9. 2018年最值得关注的30个Vue开源项目

    译者按: 学习优秀的开源项目是提高代码水平最有效的方式. 原文: 30 Amazing Vue.js Open Source Projects for the Past Year (v.2018) 译 ...

  10. react学习(一)

    组件和属性(props) 函数式组件: function Welcome(props) { return <h1>Hello, {props.name}</h1>; } 渲染一 ...