我试图从Android设备上的音乐目录查询所有音频文件。由于某种原因,但我没有得到任何,我不知道为什么。
这是我尝试获取音频列表的主要活动:
import android.content.Context
import android.media.AudioAttributes
import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.example.musicplayer.Song
import com.example.musicplayer.components.activities.MainActivityScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val audioList = querySongData(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, applicationContext)
setContent {
}
}
private fun querySongData(uri: Uri, context: Context) : List<Song> {
val audioCursor = context.contentResolver
.query(
uri,
null,
null,
null,
MediaStore.Audio.Media.TITLE + " ASC"
)
val files = mutableListOf<Song>()
if (audioCursor != null && audioCursor.moveToFirst()) {
do {
val audioTitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)
val audioArtist = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)
val audioDuration = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)
val audioUri = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)
files.add(
Song(
title = audioCursor.getString(audioTitle),
artist = audioCursor.getString(audioArtist),
lengthInSeconds = audioCursor.getInt(audioDuration),
image = Uri.parse(audioCursor.getString(audioUri)),
)
)
} while (audioCursor.moveToNext())
}
audioCursor?.close()
return files
}
}
字符串
这是一个简单的Song数据类:
data class Song(
val title : String,
val artist : String,
val image : Uri,
val lengthInSeconds : Int
)
型
当使用MediaStore.Audio.Media.INTERNAL_CONTENT_URI
而不是MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
时,我至少可以获得所有的铃声和闹钟铃声,但不能获得我想要的音频文件。这些文件位于“storage/emulated/0/Music”,并且都是.mp3文件。
我还尝试像这样预先声明投影,选择和sortOrder:
val projection = arrayOf(
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA
)
val selection = "${MediaStore.Audio.Media.IS_MUSIC} != 0"
val sortOrder = "${MediaStore.Audio.Media.TITLE} ASC"
val audioCursor = context.contentResolver
.query(
uri,
projection,
selection,
null,
sortOrder
)
型
我也试着在清单中设置权限:
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
型
但都没有成功。
多谢帮忙!
13:16:37.671 D No Network Security Config specified, using platform default
13:16:37.671 D No Network Security Config specified, using platform default
13:16:37.686 D loaded /vendor/lib/egl/libEGL_emulation.so
13:16:37.687 D loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
13:16:37.688 D loaded /vendor/lib/egl/libGLESv2_emulation.so
13:16:37.710 I START
13:16:37.724 I END
13:16:37.771 D HostConnection::get() New Host Connection established 0xec1ad260, tid 11244
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
13:16:37.778 D eglCreateContext: 0xec1adab0: maj 3 min 0 rcv 3
13:16:37.778 D eglMakeCurrent: 0xec1adab0: ver 3 0 (tinfo 0xec4f6e50) (first time)
13:16:37.791 I mapper 4.x is not supported
13:16:37.817 D createUnique: call
13:16:37.817 D HostConnection::get() New Host Connection established 0xec1ad880, tid 11244
13:16:37.817 D allocate: Ask for block of size 0x100
13:16:37.817 D allocate: ioctl allocate returned offset 0x3f7ffe000 size 0x2000
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
13:16:39.271 W A resource failed to call close.
13:16:43.562 D Installing profile for com.example.musicplayer
型
1条答案
按热度按时间pdtvr36n1#
如果你使用的是Android 30+设备,那么你应该试试这个
字符串
这里
MediaStore.Audio.Media.RELATIVE_PATH
用于作用域存储: