没有从Android 11获得音频,甚至下载和其他音乐文件

发布于 2025-02-06 06:19:44 字数 186 浏览 2 评论 0原文

在这里,我在Android 11中仅收到媒体警报铃声,但在Android 10及以下的其他设备中找到了所有音频 在图片中,您可以看到我在Android 11 中只能得到警报铃

Here I am receiving only media Alarm Ringtones in android 11 but found all audios in other devices on android 10 and below versions
Here in picture you can see i am getting only Alarm bells in android 11

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光的暖冬 2025-02-13 06:19:45

我找到了解决这个问题的解决方案。在Android 11中,Android在Android中介绍了新表MediaStore.Downloads获取其他音频文件:

  @SuppressLint("Range")
    suspend fun getAllAudioFiles(): ArrayList<AudioModel> {
        val list = ArrayList<AudioModel>()
       val files = ArrayList<File>()
        list.clear()

        withContext(Dispatchers.IO) {
            try {
                val columns = arrayOf(
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.SIZE,
                    MediaStore.Audio.Media.DURATION,
                    MediaStore.Audio.Media._ID,
                )

                //Some audio may be explicitly marked as not being music

                //Some audio may be explicitly marked as not being music
                val selection = MediaStore.Audio.Media.IS_MUSIC + " == 0"


                //For Android 10 and Android 11
                val cursor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    MergeCursor(
                        arrayOf(
                            context.contentResolver.query(
                                MediaStore.Downloads.INTERNAL_CONTENT_URI,
                                columns, null,
                                null,
                                null
                            ),
                            context.contentResolver.query(
                                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                                columns, null,
                                null,
                                null
                            )
                        )
                    )
                } else {
                    //For Below Android 11
                    MergeCursor(
                        arrayOf(
                            context.contentResolver.query(
                                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                                columns, null,
                                null,
                                null
                            )
                        )
                    )
                }
                cursor?.moveToFirst()
//                files.clear()
                while (!cursor?.isAfterLast!!) {
                    val model = AudioModel()
                    val path =
                        cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))
//                    files.add(File(path))
                    val id =
                        cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID))
                    val title =
                        cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE))
                    var duration =""
                   try {
                        duration =
                           cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION))
                               .toLong().convertLongToDurationTime()
                   }catch (e:Exception){
                       duration = "-1"
                   }
                    val size =
                        cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE))
                            .toLong().convertToLongTMbSize()
                    model.duration = duration
                    model.path = path
                    model.id = id
                    model.title = title
                    model.size = size
                    list.add(model)
                    cursor.moveToNext()
                }
                cursor.close()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }    
        return list
    }

参考: https://developer.android.com/training/data-storage/shared/media

I found solution of this problem. In android 11 now Android introduces new Table MediaStore.Downloads to get other audio files:

  @SuppressLint("Range")
    suspend fun getAllAudioFiles(): ArrayList<AudioModel> {
        val list = ArrayList<AudioModel>()
       val files = ArrayList<File>()
        list.clear()

        withContext(Dispatchers.IO) {
            try {
                val columns = arrayOf(
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.SIZE,
                    MediaStore.Audio.Media.DURATION,
                    MediaStore.Audio.Media._ID,
                )

                //Some audio may be explicitly marked as not being music

                //Some audio may be explicitly marked as not being music
                val selection = MediaStore.Audio.Media.IS_MUSIC + " == 0"


                //For Android 10 and Android 11
                val cursor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    MergeCursor(
                        arrayOf(
                            context.contentResolver.query(
                                MediaStore.Downloads.INTERNAL_CONTENT_URI,
                                columns, null,
                                null,
                                null
                            ),
                            context.contentResolver.query(
                                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                                columns, null,
                                null,
                                null
                            )
                        )
                    )
                } else {
                    //For Below Android 11
                    MergeCursor(
                        arrayOf(
                            context.contentResolver.query(
                                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                                columns, null,
                                null,
                                null
                            )
                        )
                    )
                }
                cursor?.moveToFirst()
//                files.clear()
                while (!cursor?.isAfterLast!!) {
                    val model = AudioModel()
                    val path =
                        cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))
//                    files.add(File(path))
                    val id =
                        cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID))
                    val title =
                        cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE))
                    var duration =""
                   try {
                        duration =
                           cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION))
                               .toLong().convertLongToDurationTime()
                   }catch (e:Exception){
                       duration = "-1"
                   }
                    val size =
                        cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE))
                            .toLong().convertToLongTMbSize()
                    model.duration = duration
                    model.path = path
                    model.id = id
                    model.title = title
                    model.size = size
                    list.add(model)
                    cursor.moveToNext()
                }
                cursor.close()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }    
        return list
    }

reference here: https://developer.android.com/training/data-storage/shared/media

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文