Giter VIP home page Giter VIP logo

playerlib's Introduction

playerlib - ExoPlayer HTTP audio streaming with foreground service, Android O+ compliant

This is a simple bare-bone module which allows background playing from a HTTP stream, using ExoPlayer. It is compliant with all Android O+ requirements as foreground services are concerned.

Features

  • One hundred percent Kotlin.
  • Uses foreground service to ensure playback is preserved when in background.
  • Uses audio focus wrapping, to perform handling of audio focus in Android O+ style.
  • Shows an ongoing notification for the user to control playback, using PlayerNotificationManager from ExoPlayer.
  • It is completely decoupled from whichever implementation any users wants for the player activity.
  • This is untested.

How to get it

  • Set up a main app project as usual
  • Clone into a separate folder from the main project
  • Use Import module... function from AS
  • Go to Project structure and add playerlib as a dependency

How to use it

An activity which launches the service, and a broadcast receiver to handle the launch of the activity from the player notification (when it gets clicked). It is possible to launch the stream with a custom url to the stream.

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        tools:ignore="GoogleAppIndexingWarning">
        <activity
            android:name="com.beraldo.myapp.PlayerActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
            android:label="PlayerActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="com.beraldo.playerlib.PlayerService" />

        <receiver
            android:name=".LaunchPlayerBroadcastReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.beraldo.playerlib.LAUNCH_PLAYER_ACTIVITY" />
            </intent-filter>
        </receiver>
    </application>

LaunchPlayerBroadcastReceiver.kt

class LaunchPlayerBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val notifyIntent = Intent(context, PlayerActivity::class.java)
        context.startActivity(notifyIntent)
    }
}

PlayerActivity.kt

class PlayerActivity : AppCompatActivity(), AnkoLogger {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_player)

        //Start the service
        val intent = Intent(this, PlayerService::class.java).apply {
            putExtra(STREAM_URL, "http://my.stream.url")
        }
        bindService(intent, connection, Context.BIND_AUTO_CREATE)
    }

    /**
     * Create our connection to the service to be used in our bindService call.
     */
    private val connection = object : ServiceConnection {
        override fun onServiceDisconnected(name: ComponentName?) {}

        /**
         * Called after a successful bind with our PlayerService.
         */
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            if (service is PlayerService.PlayerServiceBinder) {
                service.getPlayerHolderInstance() // use the player and call methods on it to start and stop
            }
        }
    }
}

build.gradle of module app after importing the module (see there's implementation project(path: ':playerlib'))

[...]

android {
    [...]
}

dependencies {
    [...]
    implementation project(path: ':playerlib')
}

Acknowledgments

I took inspiration from the Google codelabs for ExoPlayer https://codelabs.developers.google.com/codelabs/exoplayer-intro/#0 and mixed it up with Architecting Video Playback through a Service https://proandroiddev.com/architecting-video-playback-through-a-service-501c7bd158fa.

Apps

I have used this lib to develop my streaming app for a web radio called RadioUci Get it on Google Play

playerlib's People

Contributors

asos-filippob avatar beraldofilippo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.