在Android上从外部存储查询音频文件在Kotlin不工作

wgmfuz8q  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(109)

我试图从Android设备上的音乐目录查询所有音频文件。由于某种原因,但我没有得到任何,我不知道为什么。
这是我尝试获取音频列表的主要活动:

  1. import android.content.Context
  2. import android.media.AudioAttributes
  3. import android.media.MediaPlayer
  4. import android.net.Uri
  5. import android.os.Bundle
  6. import android.provider.MediaStore
  7. import androidx.activity.ComponentActivity
  8. import androidx.activity.compose.setContent
  9. import com.example.musicplayer.Song
  10. import com.example.musicplayer.components.activities.MainActivityScreen
  11. class MainActivity : ComponentActivity() {
  12. override fun onCreate(savedInstanceState: Bundle?) {
  13. super.onCreate(savedInstanceState)
  14. val audioList = querySongData(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, applicationContext)
  15. setContent {
  16. }
  17. }
  18. private fun querySongData(uri: Uri, context: Context) : List<Song> {
  19. val audioCursor = context.contentResolver
  20. .query(
  21. uri,
  22. null,
  23. null,
  24. null,
  25. MediaStore.Audio.Media.TITLE + " ASC"
  26. )
  27. val files = mutableListOf<Song>()
  28. if (audioCursor != null && audioCursor.moveToFirst()) {
  29. do {
  30. val audioTitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)
  31. val audioArtist = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)
  32. val audioDuration = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)
  33. val audioUri = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)
  34. files.add(
  35. Song(
  36. title = audioCursor.getString(audioTitle),
  37. artist = audioCursor.getString(audioArtist),
  38. lengthInSeconds = audioCursor.getInt(audioDuration),
  39. image = Uri.parse(audioCursor.getString(audioUri)),
  40. )
  41. )
  42. } while (audioCursor.moveToNext())
  43. }
  44. audioCursor?.close()
  45. return files
  46. }
  47. }

字符串
这是一个简单的Song数据类:

  1. data class Song(
  2. val title : String,
  3. val artist : String,
  4. val image : Uri,
  5. val lengthInSeconds : Int
  6. )


当使用MediaStore.Audio.Media.INTERNAL_CONTENT_URI而不是MediaStore.Audio.Media.EXTERNAL_CONTENT_URI时,我至少可以获得所有的铃声和闹钟铃声,但不能获得我想要的音频文件。这些文件位于“storage/emulated/0/Music”,并且都是.mp3文件。
我还尝试像这样预先声明投影,选择和sortOrder:

  1. val projection = arrayOf(
  2. MediaStore.Audio.Media.TITLE,
  3. MediaStore.Audio.Media.ARTIST,
  4. MediaStore.Audio.Media.DURATION,
  5. MediaStore.Audio.Media.DATA
  6. )
  7. val selection = "${MediaStore.Audio.Media.IS_MUSIC} != 0"
  8. val sortOrder = "${MediaStore.Audio.Media.TITLE} ASC"
  9. val audioCursor = context.contentResolver
  10. .query(
  11. uri,
  12. projection,
  13. selection,
  14. null,
  15. sortOrder
  16. )


我也试着在清单中设置权限:

  1. <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
  2. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


但都没有成功。
多谢帮忙!

  1. 13:16:37.671 D No Network Security Config specified, using platform default
  2. 13:16:37.671 D No Network Security Config specified, using platform default
  3. 13:16:37.686 D loaded /vendor/lib/egl/libEGL_emulation.so
  4. 13:16:37.687 D loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
  5. 13:16:37.688 D loaded /vendor/lib/egl/libGLESv2_emulation.so
  6. 13:16:37.710 I START
  7. 13:16:37.724 I END
  8. 13:16:37.771 D HostConnection::get() New Host Connection established 0xec1ad260, tid 11244
  9. 13:16:37.775 D HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV_Cache ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_3_0
  10. 13:16:37.778 D eglCreateContext: 0xec1adab0: maj 3 min 0 rcv 3
  11. 13:16:37.778 D eglMakeCurrent: 0xec1adab0: ver 3 0 (tinfo 0xec4f6e50) (first time)
  12. 13:16:37.791 I mapper 4.x is not supported
  13. 13:16:37.817 D createUnique: call
  14. 13:16:37.817 D HostConnection::get() New Host Connection established 0xec1ad880, tid 11244
  15. 13:16:37.817 D allocate: Ask for block of size 0x100
  16. 13:16:37.817 D allocate: ioctl allocate returned offset 0x3f7ffe000 size 0x2000
  17. 13:16:37.849 D HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV_Cache ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_3_0
  18. 13:16:39.271 W A resource failed to call close.
  19. 13:16:43.562 D Installing profile for com.example.musicplayer

pdtvr36n

pdtvr36n1#

如果你使用的是Android 30+设备,那么你应该试试这个

  1. val projection = arrayOf(
  2. MediaStore.Audio.Media.TITLE,
  3. MediaStore.Audio.Media.ARTIST,
  4. MediaStore.Audio.Media.DURATION,
  5. MediaStore.Audio.Media.DATA
  6. )
  7. val selection = "${MediaStore.Audio.Media.IS_MUSIC} != 0 AND ${MediaStore.Audio.Media.RELATIVE_PATH} LIKE '%/Music/%'"
  8. val sortOrder = "${MediaStore.Audio.Media.TITLE} ASC"
  9. val audioCursor = context.contentResolver.query(
  10. MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  11. projection,
  12. selection,
  13. null,
  14. sortOrder
  15. )

字符串
这里
MediaStore.Audio.Media.RELATIVE_PATH
用于作用域存储:

展开查看全部

相关问题