David Callanan
David Callanan

Reputation: 5968

Android generic audio resource object

I have a custom AudioPlayer class to handle playing audio in my app, and it uses the android MediaPlayer class under the hood.

It currently has a method play(source: MediaDataSource). This needs to take in some generic source object that it can play, so I decided to use MediaDataSource which seems right.

How do I obtain a MediaDataSource from a resource, e.g. R.raw.my_audio_file, so that my custom AudioPlayer class can use it? Or is MediaDataSource the wrong type to use here, and if so what other type should I use?

Upvotes: 0

Views: 115

Answers (1)

jtt
jtt

Reputation: 13541

One possiblity is to use:

https://developer.android.com/reference/android/media/MediaPlayer.html?hl=en#setDataSource(java.lang.String)

The string path for local resources/assets could probably be translated to a path/URI via: how to get an uri of an image resource in android

And if you have web endpoints this would also work for that as well.

Upvotes: 1

Related Questions