Reputation: 6429
I need to browse the audio files on the user's device using the user's playlists and songs from their Music app, then pass the selected audio file to our player.
Does the Android MediaStore
contain the master copy of all audio files, and the user defined playlists they my set up from other apps? I'm looking for an API similar to the MediaLibrary
on iOS.
The MediaStore Reference Documentation doesn't give me enough detail on how to actually use the API, and it doesn't seem to be covered in the Training, API Guides or Samples section of the docs. The Random Music Player sample seems to be out of date, and doesn't compile with Android Studio.
I assume my browser code needs to live in a Service. Right? Are there examples/docs of this available?
What about the Content Provider
API? This sometimes shows up in code samples, but it isn't clear if it will give access to the user's audio files or not.
I tried to use the following Intents, but they didn't give me access to the audio files in the manner I need. Are there existing Intents that should be used?
Using Intent.ACTION_GET_CONTENT
brings up a file browser of the file system on the device, showing audio files. Close, but not quite what is needed here.
private fun pickAudioFileWithACTION_GET_CONTENT() {
val intent = Intent( Intent.ACTION_GET_CONTENT )
intent.setType("audio/*")
val chooserIntent = Intent.createChooser( intent, "Select soundfile")
startActivityForResult( chooserIntent, SELECT_MUSIC )
}
Using MediaStore.INTENT_ACTION_MEDIA_SEARCH
allows an app selection that has a picker but also plays. I don't have control over what happens, so I don't think this is what is needed either.
private fun pickAudioFileWithMediaStoreINTENT_ACTION_MEDIA_SEARCH() {
val mediaSearchIntent = Intent( MediaStore.INTENT_ACTION_MEDIA_SEARCH )
val mediaSearchChooserIntent = Intent.createChooser( mediaSearchIntent, "Select soundfile")
startActivityForResult( mediaSearchChooserIntent, SELECT_MUSIC )
}
Suggestions for how this should work and pointers to documentation and/or samples would be appreciated.
Upvotes: 0
Views: 1194
Reputation: 1006724
Does the Android MediaStore contain the master copy of all audio files
It has an index of all files that have been indexed. That may include audio files. There is no requirement that it include all audio files, as not all audio files are necessarily in places that MediaStore
can reach (e.g., private storage for individual apps).
and the user defined playlists they my set up from other apps?
There is a MediaStore.Audio.Playlists
provider that an app could use for centralized storage of playlists. No app is required to use it. I suspect that few use it. However, there are questions here on Stack Overflow that show how it is used, such as this one and this one.
I assume my browser code needs to live in a Service. Right?
No.
Are there examples/docs of this available?
Asking for off-site resources is considered to be off-topic for Stack Overflow.
What about the Content Provider API?
You use a ContentResolver
— or things wrapped around one, such as a CursorLoader
— to query the MediaStore
, if that's what you mean. This sample app queries the MediaStore
using a CursorLoader
for all video files, then displays them in a RecyclerView
-based list. Adapting that to show audio files would not be especially difficult.
Using Intent.ACTION_GET_CONTENT brings up a file browser of the file system on the device, showing audio files. Close, but not quite what is needed here.
That's about as good as you are going to get, with respect to a canned solution for obtaining audio content, without rolling your own UI. You could use ACTION_OPEN_DOCUMENT
on Android 4.4+ as well.
Upvotes: 1