lincollincol / compose-audiowaveform

Audio waveform library for Jetpack Compose
Apache License 2.0
199 stars 12 forks source link

How can we play from the Url? #16

Closed Zaraki596 closed 1 year ago

Zaraki596 commented 1 year ago

Currently, the sample app contains the example which uses the ContentResolver to play the local files. We need some help to directly play media from the Url. how do we set up the MediaItem to play from the URL? From the Example, we can see it is using MediController to play the local file is there any example where we can play using URL?

lincollincol commented 1 year ago

@Zaraki596 Hello! Please, take a look at Amplituda library which is used sample app.

You can use code below to process audio from the URL and then pass it to the audio waveform library.

amplituda.processAudio("https://audio-url-example.com/amplituda.mp3", Cache.withParams(Cache.REUSE))

Otherwise, if you need to process more "advanced" URLs, you can try to use yt-dlp to download audio and then process with Amplituda or FFMPEG. Here is a brilliant android project Seal which uses yt-dlp

Zaraki596 commented 1 year ago

amplituda.processAudio("https://audio-url-example.com/amplituda.mp3", Cache.withParams(Cache.REUSE)) Will this function return the mediaItem ?

lincollincol commented 1 year ago

Sorry, I misunderstood you a bit. If you want to play (not process URL), then you can use ExoPlayer, Media3, or even MediaPlayer. In the sample app, playback is implemented in AudioPlaybackManager. You can try to add new setAudio() function with URL. (Create a simple exoplayer to stream video from url)

fun setAudio(url: String, title: String) {
    setAudio(getMediaItem(MediaItem.fromUri(url), title))
}
Zaraki596 commented 1 year ago

If we want to play the audio directly from URL, MediaItem.fromUri(url) is not working properly. This worked out.

 val rmd = MediaItem.RequestMetadata.Builder()
            .setMediaUri(Uri.parse(url))
            .build()

MediaItem.Builder().setRequestMetadata(rmd).build()

if we don't add Uri.parse(url) then this won't work...Maybe for this we need customDataFactory, But I tried to add this in the PlaybackService class in the Exoplayer but that also didn't worked out.

lincollincol commented 1 year ago

Yes, maybe something went wrong in PlaybackService. Please, check media items in onAddMediaItems() PlaybackService callback