Giter VIP home page Giter VIP logo

hdmediaplayer's Introduction

Platform Feature LICENSE

HDMediaPlayer

该类包依托于谷歌Mediaplayer二次封装,一行代码就可以实现播放的逻辑。
谷歌**API链接: Google官网API

特性

  • 该类支持播放格式有:音乐(.mp3、.wav、.ogg)、视频(.mp4、.3gp)

  • 支持播放本地音频、视频

  • 支持播放Assets目录下的音频、视频

  • 支持播放网络流媒体(仅限上述格式)

  • 支持对字节流的播放,用于视频的加解密方案(不会产生缓存文件)

  • 提供完整的视频流加解密输出与播放的案例

如何快速集成

导入方式

在工程级别的build.gradle添加

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

在应用级别的build.gradle添加

api 'com.github.yinhaide:HDMediaPlayer:0.1.2'

如何使用

  • 一行代码实现播放Assets目录下的mp4:
MediaPlayerHelper.getInstance().setSurfaceView(surfaceView).playAssetVideo(context,"test.mp4");
  • 一行代码实现播放Assets目录下的mp3:
MediaPlayerHelper.getInstance().playAssetMusic(context,"test.mp3");
  • 一行代码实现播放网络流媒体或者本地全路径流媒体:
/**
 * urlString 可为网络流媒体连接,也可以为本地存储全路径链接
 */
MediaPlayerHelper.getInstance().setSurfaceView(surfaceView).playLocal(context,urlString);
MediaPlayerHelper.getInstance().setSurfaceView(surfaceView).playUrl(context,urlString);
  • 一行代码实现播放字节流(用于读取加密字节流,经过解密之后直接播放,不产生缓存文件):
MediaPlayerHelper.getInstance().setSurfaceView(surfaceView).playVideoDataSource (byte[] videoBuffer);
  • 如果想得到详细的回调信息:
MediaPlayerHelper.getInstance().setOnStatusMediaCallBack(new MediaPlayerHelper.MediaPlayerHelperCallBack() {
    @Override
    public void onCallBack(MediaPlayerHelper.CallBackState state, 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();

注意点

1、播放结束后到下次播放有黑屏现象

在部分配置比较低的电视或者手机设备中,视频播放完毕到下次播放之间可能存在1s左右的黑屏现象,原因是部分机器软解吗能力较弱,需要将数据解码放在内存当中导致黑屏。解决方案是开启手机的硬件加速能力,让硬件协助解码,消除黑屏现象。

<application
    android:hardwareAccelerated="true"
    ...
</application>

2、部分机型/电视存在黑屏有声音的现象

在部分机型中,会有播放没画面的,原因是因为SurfaceHolder可能在创建的时候尺寸不对,需要动态赋值。这个操作必须在主线程中进行。

@Override
public void surfaceCreated(SurfaceHolder holder) {
    ...
    if(holder != null && uiHolder.surfaceView != null){
        uiHolder.surfaceView.post(() -> holder.setFixedSize(uiHolder.surfaceView.getWidth(),uiHolder.surfaceView.getHeight()));
    }
    ...
}

3、低配置机型只能播放一次,下次再次播放直接黑屏

部分底端机型(小米电视32寸1G+4G配置),只能播放一次,第二次尝试柏昂都是失败的。解决办法:每次播放之前需要释放之前的MediaPlayer,重新创建新的对象。MediaPlayer需要比较多的内存,特别是低端机出问题的时候尝试释放资源再重新创建是有效的。

mediaPlayerHelper.reCreateMediaPlayer();

4、低配置机型在播放完毕之后会黑屏

部分底端机型(天猫魔盒),在视频播放完毕之后直接黑屏,而不是停留在最后一帧。解决办法就是不让它播放完毕,在播放到最后几十到一百毫秒的时候执行pause()操作。

//进度间隔
mediaPlayerHelper.setProgressInterval(50);
//进度回调
mediaPlayerHelper.setOnStatusCallbackListener((state, args) -> {
    if(state == MediaPlayerHelper.CallBackState.PROGRESS){
        int videoDuration = mediaPlayerHelper.getMediaPlayer().getDuration();
        int videoPositionDuration = mediaPlayerHelper.getMediaPlayer().getCurrentPosition();
        int differ = videoDuration - videoPositionDuration;
        //播放到左后前面指定时间停止,防止黑屏
        if(differ < 180){
            this.mediaPlayerHelper.getMediaPlayer().pause();
        }
    }
});

这个项目会持续更新中...

都看到这里了,如果觉得写的可以或者对你有帮助的话,顺手给个星星点下Star~

这个控件内部采用一个Fragment框架,如果有兴趣的话可以去了解一下

关于我

LICENSE

Copyright 2019 haide.yin([email protected])

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.

hdmediaplayer's People

Contributors

yinhaide avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.