GithubHelp home page GithubHelp logo

wanghuaichen / rxmusicplayer-android Goto Github PK

View Code? Open in Web Editor NEW

This project forked from orfium/rxmusicplayer-android

0.0 1.0 0.0 170 KB

An android music player using ExoPlayer and RxJava2

License: Apache License 2.0

Kotlin 98.73% Java 1.27%

rxmusicplayer-android's Introduction

Android Arsenal

RxMusicPlayer

An android music player using ExoPlayer and RxJava2.

Introduction

RxMusicPlayer is a part of our music player in Orfium new android application.
Using subjects and sealed classes, media manager can emit the latest state of exoplayer, allowing views and classes to observe it and react according to its state, without the need to have a reference to the media manager or exoplayer itself.

Initialization

Before start using RxMusicPlayer you have to call the method that starts the Media Service

RxMusicPlayer.start(context)

or

RxMusicPlayer.start(context, Intent(this, MainActivity::class.java))

if you want to specify the activity you want to launch from notification

Usage

Create a Media item class that contains all the necessary data information and call playStop extension function on it.

Example:

val media = Media(
            id = 1, title = "Closer and Closer . . . 25.11.2018", artist = "Strobi-wan", duration = 7861, 
            image = "https://s3-us-west-2.amazonaws.com/orfium-public/tracks/artwork/45c4ad6b21dc4aecad4bee0bafefb613.jpg",
            streamUrl = "https://s3-us-west-2.amazonaws.com/orfium-public/tracks/8c7465df1f0c4e48af10ad4f6c17a2ef.mp3"
        )
media.playStop()

You can also check if a Media item is currently playing

if (media.isPlaying())

or add/remove it from queue

media.addQueue()

media.removeQueue()

If you wish not to use the extension functions you can use RxMusicPlayer action to emit a new action that the MediaManager will handle. Here is the full list of actions that the MediaManager can handle

Example:

RxMusicPlayer.action.onNext(Action.pause())

or

RxMusicPlayer.action.onNext(Action.seek(position))

To observe the changes on media player state just subscribe to RxMusicPlayer state, that will emit the current PlaybackState along with the current Media item on Queue. When a new observer subscribes to RxMusicPlayer state, it immediately gets notified about the current PlaybackState. You can find all states here

RxMusicPlayer.state
            .distinctUntilChanged()
            .subscribe { state ->
                when (state) {
                    is PlaybackState.Buffering -> /* Your code */
                    is PlaybackState.Playing -> showPlaying(state.media)
                }
            }

A notification is provided by default when the state gets changes. You can configure notification's icons by overriding them.

RxMusicPlayer allows you to observe changes in media queue list and the current playback position of exoplayer

RxMusicPlayer.queue
                .subscribe { queueData -> /* Your code */ }

RxMusicPlayer.position
                .observeOn(AndroidSchedulers.mainThread())
                .distinctUntilChanged()
                .subscribe { position -> /* Your code */ }

Setup

The latest library version is

Add the JitPack repository in your build.gradle (top level module):

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

And add the dependency in the build.gradle of the module:

implementation 'com.github.Orfium:RxMusicPlayer-android:LATEST_VERSION'

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

rxmusicplayer-android's People

Contributors

dimkonomis avatar

Watchers

 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.