Giter VIP home page Giter VIP logo

glplayer's Introduction

GLPlayer

LICENSE

GLPlayer是一个在Android平台上工作的音频/视频播放器。"GL"两个字母取自"OpenGL",表明这是利用OpenGL的相应API进行视频渲染的媒体播放器。它的主要功能实现依赖于Android平台内的基础Media组件:解封装媒体文件基于MediaExtractor,音频帧/视频帧基于MediaCodec进行硬解码,视频渲染基于OpenGL ES,音频渲染基于AudioTrack

GLPlayer的API设计相当程度上参考了ExoPlayer,对于有音视频播放需求的开发者,强烈建议使用ExoPlayer而不是GLPlayer。GLPlayer目前不能保证实际应用环境中的稳定性,也缺少ExoPlayer所提供的许多高级功能

如何添加该库

Groovy

settings.gradle添加:

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

然后,在对应模块内的的build.gradle添加:

dependencies {
    implementation 'com.github.usagisang:GLPlayer:0.1.0'
}

Kotlin Script

settings.gradle.kts添加:

dependencyResolutionManagement {
    ...
    repositories {
        ...
        maven(url = "https://jitpack.io")
    }
}

然后,在对应模块内的的build.gradle.kts添加:

dependencies {
    implementation("com.github.usagisang:GLPlayer:0.1.0")
}

快速使用指南

快速使用指南(Kotlin):

// 构造Player实例
val player: Player = GLPlayerBuilder(context).setPlayAfterLoading(true).build()
// PlayerView是GLPlayer提供的视频渲染顶层View
val playerView = findViewById<PlayerView>(R.id.player_view)
// 绑定Lifecycle
playerView.bindLifecycle(lifecycle)
// 将Player绑定到PlayerView以进行视频渲染
playerView.setPlayer(player)
// 设置需要加载的媒体,这里采用网络url
player.setMediaItem(MediaItem.fromUrl(videoUrl))
// 开始准备媒体资源
player.prepare()

快速使用指南(Java):

private Player player;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    // ....
    String mediaUrl = "...";
    // 构造Player实例
    player = new GLPlayerBuilder(this).setPlayAfterLoading(true).build();
    // PlayerView是GLPlayer提供的视频渲染顶层View
    PlayerView playerView = findViewById(R.id.player_view);
    // 绑定Lifecycle
    playerView.bindLifecycle(getLifecycle());
    // 将Player绑定到PlayerView以进行视频渲染
    playerView.setPlayer(player);
    // 设置需要加载的媒体
    player.setMediaItem(MediaItem.Companion.fromUrl(mediaUrl));
    // 开始准备媒体资源
    player.prepare();
}

为什么建议为PlayerView绑定Lifecycle

GLPlayer使用OpenGL ES进行视频渲染,当PlayerView离开屏幕(例如,回到桌面或者锁屏),OpenGL所依赖的EGL Display等资源会被自动释放,这时如果仍试图更新纹理就会抛出异常,会导致后续PlayerView无法再次渲染视频。PlayerView自动处理了锁屏事件,但不绑定Lifecycle则无法监听与生命周期相关的事件。因此,如果希望播放器进入后台时也继续播放音频,并能够在应用回到前台后恢复视频渲染,建议为PlayerView绑定Lifecycle

如果确认播放器不需要在后台播放视频的音频轨道,那么,只要在相应事件(onPause等)发生后调用player.pause()来停止播放,也可以避免EGL Display被释放的异常,这种情况下不需要为PlayerView绑定Lifecycle

如果只打算播放纯音频,那么不需要使用PlayerView,安全忽略绑定Lifecycle的要求即可。

LICENSE

MIT License

Copyright (c) 2022 usagisang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

glplayer's People

Contributors

usagisang avatar

Stargazers

AHuier avatar dennis avatar mahongyin avatar

Watchers

 avatar

glplayer's Issues

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.