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查看
---------------------------------------------------------------------------------------------------- ...
随机推荐
- 内核第三讲,进入ring0,以及编写第一个内核驱动程序.
内核第三讲,进入ring0,以及编写第一个内核驱动程序. PS: 请下配置双机调试,下方有可能用到.如果不配置,则你可以不用调试, 博客连接: http://www.cnblogs.com/iBina ...
- 计算n的阶乘有多少个尾随零
思路一: 计算出n!= nValue,然后 nValue % 10 == 0 则nCount自增1,nValue /= 10 直到条件为否,最后nCount就是我们想要的结果,代码如下: int Co ...
- Dubbo 源码解析四 —— 负载均衡LoadBalance
欢迎来我的 Star Followers 后期后继续更新Dubbo别的文章 Dubbo 源码分析系列之一环境搭建 Dubbo 入门之二 --- 项目结构解析 Dubbo 源码分析系列之三 -- 架构原 ...
- 分享一个爬取HUST(哈理工)学生成绩的Python程序(OCR自动识别验证码)
Python版本:3.5.2 日期:2018/1/21 __Author__ = "Lance#" # -*- coding = utf-8 -*- from urllib imp ...
- [转]ERROR: http://rancherserver/v1 is not accessible
本文转自:http://securityer.lofter.com/post/1d0f3ee7_10c465cc 安装rancher agent时出现以下报错 [root@localhost ~]# ...
- 【转载】ASP.NET以Post方式抓取远程网页内容类似爬虫功能
使用HttpWebRequest等Http相关类,可以在应用程序中或者网站中模拟浏览器发送Post请求,在请求带入相应的Post参数值,而后请求回远程网页信息.实现这一功能也很简单,主要是依靠Http ...
- nginx配置反向代理和负载均衡
一.反向代理 说明:应该有一个nginx服务器有多个应用服务器(可以是tomcat),本文使用一台虚拟机,安装一个nginx,多个tomcat,来模拟 upstream tomcats{ server ...
- 解决微信开发工具上trace无法检测到设备,一直停留在“正在搜索设备...”或者trace panel,choose device老出现device not found
性能 Trace 工具 微信 Andoid 6.5.10 开始,我们提供了 Trace 导出工具,开发者可以在开发者工具 Trace Panel 中使用该功能. 使用方法 PC 上需要先安装 adb ...
- 关于RecyclerView你知道的不知道的都在这了(上)
目录 前言 目录 正文 1. LayoutManager 2. ViewHolder 3. LayoutParams 4. Adapter 5. RecyclerView 6. Recycler 7. ...
- canvas-a12ellipse.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...