Android使用MediaPlayer播放流媒体,支持远程以及本地流媒体,一行代码实现
时间:2022-03-13 19:54
HDMediaPlayer
该类包依托于谷歌Mediaplayer二次封装。
谷歌中国API链接:
目录
如何导入到项目
支持jcenter方式导入。
支持本地Module方式导入。
jcenter方式导入
- 在需要用到这个库的module中的build.gradle中的dependencies中加入
dependencies {
compile ‘com.yhd.hdmediaplayer:hdmediaplayer:1.0.1‘
}
Module方式导入
- 下载整个工程,将hdmediaplayer拷贝到工程根目录,settings.gradle中加入
include ‘:hdmediaplayer‘
- 在需要用到这个库的module中的build.gradle中的dependencies中加入
dependencies {
compile project(‘:hdmediaplayer‘)
}
如何使用
该类支持音乐(.mp3、.wav、.ogg)和视频(.mp4、.3gp)以及对应格式的网络流媒体的播放。
如果要播放视频的话需要控件:SurfaceView。
- 一行代码实现播放Assets目录下的mp4:
MediaPlayerHelper.getInstance().setSurfaceView((SurfaceView)findViewById(R.id.surfaceView)).playAsset(MainActivity.this,"test.mp4");
- 一行代码实现播放Assets目录下的mp3:
MediaPlayerHelper.getInstance().playAsset(MainActivity.this,"test.mp3");
- 一行代码实现播放网络流媒体或者本地全路径流媒体:
/**
* urlString 可为网络流媒体连接,也可以为本地存储全路径链接
*/
MediaPlayerHelper.getInstance().setSurfaceView((SurfaceView)findViewById(R.id.surfaceView)).play(urlString);
- 如果想得到详细的回调信息:
MediaPlayerHelper.getInstance().setMediaPlayerHelperCallBack(new MediaPlayerHelper.MediaPlayerHelperCallBack() {
@Override
public void onCallBack(MediaPlayerHelper.CallBackState state, MediaPlayerHelper mediaPlayerHelper, Object... args) {
Log.v(TAG,"--"+state.toString());
if(state== MediaPlayerHelper.CallBackState.PROGRESS){
int percent=(int)args[0];
Log.v(TAG,"--progress:"+percent);
}
}
});
- 更多的用法请查看demo:
/** 设置播放进度回调时间间隔 */
MediaPlayerHelper.getInstance().setProgressInterval(msec);
/** 获得MediaPlayer对象,进行更多的自定义的操作 */
MediaPlayerHelper.getInstance().getMediaPlayer().pause();
MediaPlayerHelper.getInstance().getMediaPlayer().start();
MediaPlayerHelper.getInstance().getMediaPlayer().stop();
关于我
欢迎 Star Fork
交流地址:尹海德(123302687@qq.com)
License
Copyright 2017 Yinhaide
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
相关推荐
- Android系统编程入门系列之界面Activity交互响应
- 新型横向移动工具原理分析、代码分析、优缺点以及检测方案
- uni-app滚动视图容器(scroll-view)之监听上拉事件
- uniapp h5,app两端复制文本
- Android系统编程入门系列之界面Activity响应丝滑的传统动画
- 【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn
- 诺基亚短信生成!太好玩了
- iOS 跳转App Store进行评分
- 开发一个即时通讯App
- 关闭苹果IOS app自动更新
电脑软件
本类排行
- 1关闭苹果IOS app自动更新
- 2iOS 跳转App Store进行评分
- 3诺基亚短信生成!太好玩了
- 4Android系统编程入门系列之界面Activity响应丝滑的传统动画
- 5uniapp h5,app两端复制文本
- 6uni-app滚动视图容器(scroll-view)之监听上拉事件
- 7新型横向移动工具原理分析、代码分析、优缺点以及检测方案
- 8Android系统编程入门系列之界面Activity交互响应
- 9开发一个即时通讯App
- 10【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn