android - 如何获取系统音频的位置

android - how to get location of sytem audio

现在,我正在避免为设备和模拟器加载系统音频 我有 通过在查询 ContentResolver,但是,我发现系统音频的存储位置(如警报音和加载的所有音频工厂)在所有 android 设备中都没有标准位置。

所以,为了避免加载所有系统音频,我需要一种方法来获取这些音频所在的目录,到目前为止,我已经收集了系统音频可能位于的这些位置的数据:

"/system%",
"/storage/emulated/legacy/Ringtones/%",
"/storage/emulated/0/Ringtones%",
"/product/media/audio%"  

但是,由于这些可能还不够,有没有办法获取系统音频所在的目录路径android?

MediaStore , then filter audio files with AudioColumns , and more specific for alarm sounds use IS_ALARM查询音频文件。

如你所说:

I have found that the location for the storage of sytem audio doesn't have a standard location in all android device.

MediaStore 是一个保存文件引用的数据库,它是查询不同系统存储位置的文件的解决方案。

你可以这样使用:

String[] mProjection =
                {
                        MediaStore.Audio.Media._ID,
                        MediaStore.Audio.Media.DATA,       # path
                        MediaStore.Audio.Media.IS_ALARM,   
                        MediaStore.Audio.Media.DURATION,   
                        MediaStore.Audio.Media.SIZE
                };
Cursor mCursor = getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,
                mProjection,
                null,
                null);

Note: for external storage use: MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

int index_data = mCursor.getColumnIndex(MediaStore.Audio.Media.DATA);

if (mCursor != null) {
    while (mCursor.moveToNext()) {

        // Gets the value from the column.
        newData = mCursor.getString(index_data);

        // Insert code here to process the retrieved data.

        // end of while loop
    }
} else {

    // Insert code here to report an error if the cursor is null or the provider threw an exception.
}

参考文献:

https://developer.android.com/guide/topics/providers/content-provider-basics#java

列出片段中所有警报的代码:

override fun onAttach(context: Context) {
        super.onAttach(context)
        val projection = arrayOf(
            MediaStore.Audio.Media.IS_ALARM,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.DATE_ADDED
        )

        val audio: Uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI

        val cur = context.contentResolver.query(
            audio,
            projection,  // Which columns to return
            "${MediaStore.Audio.Media.IS_ALARM}>0",  // Which rows to return (all rows)
            null,  // Selection arguments (none)
            null // Ordering
        ) ?: return

        if (cur.moveToFirst()) {

            val displayNameColumn: Int = cur.getColumnIndex(
                MediaStore.Audio.Media.DISPLAY_NAME
            )
            val dateColumn: Int = cur.getColumnIndex(
                MediaStore.Audio.Media.DATE_ADDED
            )
            do {
                val displayName = cur.getString(displayNameColumn)
                val date = cur.getString(dateColumn)

                Log.i(
                    "Listing Alarms",
                    " displayName=$displayName , date=$date"
                )
            } while (cur.moveToNext())
        }

您需要添加 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />