正在 Android 上浏览音频文件

Browsing for audio files on Android

我需要使用用户的播放列表和音乐应用程序中的歌曲浏览用户设备上的音频文件,然后将选定的音频文件传递给我们的播放器。

Android MediaStore 是否包含所有音频文件的主副本,以及他们从其他应用程序设置的用户定义的播放列表?我正在寻找类似于 iOS 上的 MediaLibrary 的 API。

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 似乎已过时,无法使用 Android Studio 进行编译。

我假设我的浏览器代码需要存在于服务中。正确的?有 examples/docs 个可用吗?

Content Provider API呢?这有时会出现在代码示例中,但不清楚它是否会允许访问用户的音频文件。

我尝试使用以下 Intents,但它们没有让我以我需要的方式访问音频文件。是否存在应使用的现有 Intent?

使用Intent.ACTION_GET_CONTENT打开设备上文件系统的文件浏览器,显示音频文件。接近,但不是这里所需要的。

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 )
}

使用 MediaStore.INTENT_ACTION_MEDIA_SEARCH 允许选择具有选择器但也可以播放的应用程序。我无法控制发生的事情,所以我认为这也不是必需的。

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 )
}

关于这应该如何工作的建议和指向文档 and/or 示例的指针将不胜感激。

Does the Android MediaStore contain the master copy of all audio files

它有一个所有已编入索引的文件的索引。这可能包括音频文件。不要求它包含 所有 个音频文件,因为并非所有音频文件都必须位于 MediaStore 可以到达的地方(例如,各个应用程序的私有存储)。

and the user defined playlists they my set up from other apps?

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?

没有

Are there examples/docs of this available?

请求站外资源被认为是 Stack Overflow 的题外话。

What about the Content Provider API?

您使用 ContentResolver — 或围绕一个的东西,例如 CursorLoader — 来查询 MediaStore,如果这是您的意思。 This sample app 使用 CursorLoader 查询 MediaStore 的所有视频文件,然后将它们显示在基于 RecyclerView 的列表中。调整它以显示音频文件不会特别困难。

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.

关于获取音频内容的固定解决方案,这与您将要获得的效果差不多,无需自己动手 UI。您也可以在 Android 4.4+ 上使用 ACTION_OPEN_DOCUMENT