Skip to content

Latest commit

 

History

History
182 lines (146 loc) · 6.4 KB

README.md

File metadata and controls

182 lines (146 loc) · 6.4 KB

ExoPlayer

ExoRecord

Current Version Codacy Badge

An Android library to record audio stream played by Exoplayer

Works on top of ExoPlayer

Because this library uses coroutine, it currently only supports Kotlin. Make sure your app is implementing the latest coroutine library:

    dependencies {
       implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:[latest-version]'
       implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:[latest-version]'
    }

follow the following steps to add the exorecord dependency to your app:

  • make sure to add jitpack to your repositories
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
  • implement this library
    dependencies {
        implementation 'com.github.brookmg.exorecord:exorecord:[latest-version]'
    }

Samples

  • More expressive sample application is located in the app module

  • Initialising the library

    // Preferably in the Application call
    val exoRecordInstance: ExoRecord by lazy { ExoRecord(instance) }

After initiation, we need to connect exoplayer and exorecord to be able to record the steaming feed that's going to be played.

    // ... Define other components necessary for ExoPlayer ( Bandwidth meter, Data source, Track selection ... ) 

    val renderersFactory = object : DefaultRenderersFactory(this) {
        override fun buildAudioSink(
            context: Context, enableFloatOutput: Boolean,
            enableAudioTrackPlaybackParams: Boolean, enableOffload: Boolean
        ): AudioSink {
            return DefaultAudioSink(
                AudioCapabilities.DEFAULT_AUDIO_CAPABILITIES,
                // We attach the AudioProcessor from exoRecord here
                DefaultAudioSink.DefaultAudioProcessorChain(exoRecordInstance.exoRecordProcessor),
                enableFloatOutput, enableAudioTrackPlaybackParams, enableOffload
            )
        }
    }

    // Create the ExoPlayer instance to be used for playing stream urls
    val exoPlayer = SimpleExoPlayer.Builder(applicationContext, renderersFactory)
                .setTrackSelector(trackSelector)
                .setBandwidthMeter(bandwidthMeter)
                .build()    

    exoPlayer.setMediaSource(mediaSource)
    exoPlayer.prepare()

    exoPlayer.playWhenReady = true  // Start playing 
  • Add state change listeners
    private val exoRecordListener = object: ExoRecord.ExoRecordListener {
        override fun onStartRecording(recordFileName: String) {
            // Recording wav started on file `recordFileName`
        }

        override fun onStopRecording(record: IExoRecord.Record) {
            // Recording finished. Details like sampleRate and channel count are 
            // located in the `record` variable
        }
    }

    // Add the listener to the exoRecord instance created
    exoRecordInstance.addExoRecordListener("MainListener", exoRecordListener)

    // Be sure to remove listener on lifecycle change to avoid memory leaks
    exoRecordInstance.removeExoRecordListener("MainListener")   
  • Simply starting or stopping recording from your activity or fragment like:
    // Starting the recording
    CoroutineScope(Dispatchers.Main).launch {
        App.exoRecordInstance.startRecording()
    }

    // Stopping the recording
    CoroutineScope(Dispatchers.Main).launch {
        val wavFilePath = App.exoRecordInstance.stopRecording()
    }

Audio conversion

Even though storing audio recording in a raw wave format might conserve the true quality and bitrate of the original stream, it's not truly efficient for normal consumption. ExoRecord provides optional modules for converting these wave files into other encoding. Be sure to use the latest distribution method from google ( App bundles ) to avoid huge size gains on your application if you use this method. Split apk distribution can also be applied

ExoRecordOGG

This module can convert any wave file into ogg file format.

  • To add this library implement
    dependencies {
        implementation 'com.github.brookmg.exorecord:exorecord:[latest-version]'
        implementation 'com.github.brookmg.exorecord:exorecordogg:[latest-version]'
    }
  • Simply convert the wave file
    CoroutineScope(Dispatchers.IO).launch {
        val converted = ExoRecordOgg.convertFile(
            applicationContext = instance, // The application instance
            fileName = "recording-0.wav", // File name ... This file should be present in the `/data/data/[app-package-name]/files` directory
            sampleRate = 44_100, // 44.1Hz
            channelCount = 2,  // Stereo
            quality = 1f
        ) { progress -> 
            Log.v("conversion", "Ogg file conversion at $progress%")
        }     
    }

Features in this lib:

  • Recording audio from exoplayer
  • Conversion to OGG

make sure you have enabled java8 in your project

    android {
        ...
        
        compileOptions {
            sourceCompatibility = '1.8'
            targetCompatibility = '1.8'
        }
    }

License

Copyright (C) 2021 Brook Mezgebu

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.