Skip to content

Android Audio Library, leaning towards a functional programming style, written in Kotlin. Uses ExoPlayer.

License

Notifications You must be signed in to change notification settings

muddassir235/faudio

Repository files navigation

Faudio

Release

Android Audio Library, leaning towards a functional programming style, written in Kotlin. Written on top of ExoPlayer.

Unique Features

  • A simple API is provided which leans towards a functional programming style.
  • A whole host of arbitrary custom functions can be written to perform actions on the audio.
  • Hides away all of the video related features of ExoPlayer so you can focus on your audio application.

Requirements

  • Android 5+

Add Dependencies

Add the following in your project level build.gradle

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

and the following in your app level build.gradle

dependencies {
    implementation 'com.github.muddassir235:faudio:2.1'
}

Use The Library

Create an audio object

val audio = listOf(
    "https://site.com/audio1.mp3",
    "https://site.com/audio2.mp3",
    "https://site.com/audio3.mp3"
) asAudioWith(this)

audio should (start then download)

You can optionally provide the lifecycleOwner if this is not an AppCompatActivity and you want to have your own lifecycleOwner.

val audio = Audio(context = this, lifecycleOwner = yourLifecycleOwner)

Available audio actions

Perform any common action on your audio

On the main thread

audio should start
audio should pause
audio should stop
audio should moveToNext
audio should moveToPrev
.
.
.

// If status of the action (success or failure) is required use the following
audio.changeStateAsync(start) { success ->
    // Check the operation completion status if required.
}

Using coroutines

audio shouldPerform {
    // suspend methods
    this needsTo download
    this needsTo start
    this needsTo shuffle

    val success = this needsTo moveToNext // Check status if required
    .
    .
    .
}

Custom actions

Perform a custom action on your audio using a lambda

Main thread

audio should { actualState ->
    // Your own custom action of the audio
    ExpectedAudioState(
        uris = actualState.uris,
        index = /* new index */,
        paused = /* should it be paused */,
        progress = /* new progress */,
        speed = /* updated speed */,
        stopped = /* should it stop */
    )
}

Coroutines

audio shouldPerform {
    this needsTo {
        // Your own custom action of the audio
        ExpectedAudioState(
            uris = actualState.uris,
            index = /* new index */,
            paused = /* should it be paused */,
            progress = /* new progress */,
            speed = /* updated speed */,
            stopped = /* should it stop */
        )
    }
}

Observe state

Observe the audio state or state diffs on every change of state.

State changes

audio.state.observe(lifecycleScope) { actualState ->
    // Your logic here
    // Fields available...
    // actualState.uris, actualState.index, actualState.paused, actualState.progress
    // actualState.speed, actualState.bufferedPosition, actualState.currentIndexDuration,
    // actualState.stopped, actualState.error
}

State diffs

audio.stateDiff.observe(lifecycleScope) { diff ->
    // Your logic here
    // Fields available...
    // diff.prev Previous state
    // diff.next Next state
    // diff.changeType (Can be one of the those defined in AudioStateChangeTypes)
}

The following are the available state change types

AudioStateChangeTypes.START
AudioStateChangeTypes.START_AND_DOWNLOAD
AudioStateChangeTypes.DOWNLOAD_CURRENT
AudioStateChangeTypes.PAUSE
AudioStateChangeTypes.STOP
AudioStateChangeTypes.NEXT
AudioStateChangeTypes.NEXT_AND_DOWNLOAD
AudioStateChangeTypes.PREV
AudioStateChangeTypes.PREV_AND_DOWNLOAD
AudioStateChangeTypes.SEEK
AudioStateChangeTypes.MOVE_TO_INDEX
AudioStateChangeTypes.MOVE_TO_INDEX_AND_DOWNLOAD
AudioStateChangeTypes.DOWNLOAD_INDEX
AudioStateChangeTypes.RESTART
AudioStateChangeTypes.URIS_CHANGED
AudioStateChangeTypes.UNCHANGED
AudioStateChangeTypes.UNKNOWN

Credits:

This library used the following projects.

Muddassir Ahmed Links: