0

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?

David Callanan
  • 5,601
  • 7
  • 63
  • 105
  • AudioPlayer is not a general android class from my search of the docs. What class are you using ? – JoxTraex Jan 02 '20 at 18:28
  • @JoxTraex "I have a custom AudioPlayer class" – David Callanan Jan 02 '20 at 18:30
  • Ah I see, but what is its base class? I ask because that is what will define the standard for how you interact with your media via provided APIs from the super unless you are doing all the low level decoding. One other problem is its unclear who generates MediaDataSources, since the APIs provided on the docs are very simplistic https://developer.android.com/reference/android/media/MediaDataSource?hl=en . You could use MediaPlayer (https://developer.android.com/reference/kotlin/android/media/MediaPlayer?hl=en#setDataSource(java.io.FileDescriptor)) and translate the resource to URI. – JoxTraex Jan 02 '20 at 18:33
  • @JoxTraex I have edited my question. I am using the android `MediaPlayer` under the hood – David Callanan Jan 02 '20 at 18:36

1 Answers1

1

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.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • This nearly works. I now have a `URI` called `uri`. Using `MediaPlayer.create(context, uri)` works, but I don't want to create a new instance every time, so when I use `mediaPlayer.setDataSource(uri.toString())` no audio is played. Maybe I'm using `setDataSource` wrong so I'll keep looking at it – David Callanan Jan 02 '20 at 18:58
  • @DavidCallanan I believe once you create it you only need to call setDataSource again and it'll reuse the same object and decode the new data source. You don't need to call create each time. The difference is when you use create its already setting up the datasource TO that URI. No rule saying you can't have that object load a new datasource. – JoxTraex Jan 02 '20 at 19:01
  • Got it to work! I used `.setDataSource(ctx, uri)` instead of `.setDataSource(uri.toString())`. Really appreciate the help! :) – David Callanan Jan 02 '20 at 19:02
  • Nevermind, it's not working. My app is crashing and there is no error message. Probably just a small mistake – David Callanan Jan 02 '20 at 19:02
  • 1
    Right, finally got it to work, forgot to call `.prepare()`. Thanks again – David Callanan Jan 02 '20 at 19:08