Android内嵌VLC实现播放网络视频,网络音频
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实现播放网络视频,网络音频的更多相关文章
- cocos2d-x调用android内嵌浏览器打开网页
cocos2d-x调用android内嵌浏览器打开网页,能够从入口传入网址,C++调用android 的api就可以实现. 方法也非常easy 1. 改动"cocos2dx\platform ...
- Android开发 MediaPlayer入门_播放本地视频
前言 MediaPlayer,可以播放视频/音频,并且它支持本地和网络文件的播放.本片博客作为入门教程,先以最通俗的方式解释播放文件本地视频.(如果你嫌MediaPlayer还是太麻烦可以试试选择Vi ...
- Android中webview html5 自动播放本地视频
MainActivity代码 public class Html5VideoAutoPlay extends Activity { WebView webview = null; @Override ...
- Android内嵌网页webview点击其中的链接跳转到我们应用内的Activity
在一个大的Android项目中,由于客户端来不及更新和实现,经常会内嵌一些网页(在一些大型的互联网公司,PC的产品总是跑在客户端的前面),比如活动页面,通常可以内嵌用html5实现的页面,可以适配手机 ...
- android内嵌H5页(webview)如何定位
一.切换至webview后再定位元素 (1)获取页面上下文 contexts = driver.contexts (2)切换至webview driver.switch_to.context(cont ...
- SAP播放本地视频及音频(仅限于window MediaPlayer可播放文件)
这个是从SCN上看到的,自己稍加修改,编制,做的还可以,可以播放视频,音频,唯一的不足就是不能控制播放视频的显示窗口大小,希望有人能帮忙解决,感激! 视频播放类:(新建类Z_CL_MEDIA,点击基于 ...
- 一处折腾笔记:Android内嵌html5加入原生微信分享的解决的方法
有一段时间没有瞎折腾了. 这周一刚上班萌主过来反映说:微信里面打开聚客宝.分享功能是能够的(这里是用微信自身的js-sdk实现的).可是在android应用里面打开点击就没反应了:接下来狡猾的丁丁在产 ...
- Android内嵌PDF预览
一.在对应模块的build.gradle文件中加入依赖 dependencies { implementation 'com.github.barteksc:android-pdf-viewer:3. ...
- [Unity3D]Unity3D游戏开发Android内嵌视图Unity查看
---------------------------------------------------------------------------------------------------- ...
随机推荐
- 【ASP.NET MVC系列】浅谈ASP.NET MVC 视图与控制器传递数据
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
- ZooKeeper系列(2):ZooKeeper命令行工具zkCli.sh
ZooKeeper系列文章:https://www.cnblogs.com/f-ck-need-u/p/7576137.html#zk 1.简介 ZooKeeper提供了一个非常简单的命令行客户端zk ...
- C# 从网站下载图片
Image _image = Image.FromStream(WebRequest.Create(severPath).GetResponse().GetResponseStream());
- 结构型---外观模式(Facade Pattern)
定义 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用.使用外观模式时,我们创建了一个统一的类,用来包装子系统中一个或多个复杂的类,客户端可以直接通 ...
- WPF 自定义Command
无参Command: internal class DelegateCommand : ICommand { private readonly Action _execute; private rea ...
- [PHP]代码执行和生命周期
PHP代码的执行:1.和大部分程序一样,接收数据,处理数据,输出结果2.编写的代码就是输入的数据,php内核进行处理,返回相应的输出3.php作为业务程序和编译语言的区别就是,php多了一步把用户代码 ...
- Add Again(重复元素排序) UVA11076
Add Again Summation of sequence of integers is always a common problem in Computer Science. Rather t ...
- 【转】为什么要使用ModelDriven
---恢复内容开始--- package cn.com.leadfar.struts2.actions; public class User { private int id ; private St ...
- 微信小程序性能优化技巧
摘要: 如果小程序不够快,还要它干嘛? 原文:微信小程序性能优化方案--让你的小程序如此丝滑 作者:杜俊成要好好学习 Fundebug经授权转载,版权归原作者所有. 微信小程序如果想要优化性能,有关键 ...
- 我是这样搞懂一个神奇的BUG
摘要: 通过分析用户的行为,才想得到为什么会出现这种情况! 前两天在BearyChat收到这样的一个报警消息: 409 ?Conflict ? 平时很少遇到这样的错误,貌似很严重的样子,吓得我赶紧查看 ...