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

时间:2020-10-22 11:12:21

标签: java android system

现在,通过在查询NOT LIKE ...的同时在SQLite ContentResolver语句中对它们进行硬编码,可以避免为设备和模拟器加载系统音频。我发现系统音频的存储位置(如警报音和出厂时加载的所有音频)在所有android设备中均没有标准位置。

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

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

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

3 个答案:

答案 0 :(得分:3)

使用MediaStore查询音频文件,然后使用AudioColumns过滤音频文件,更具体地使用IS_ALARM来警报声音。

如您所说:

我发现存储系统音频的位置在所有android设备中都不是标准位置。

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

答案 1 :(得分:2)

您可以使用类似这样的内容:

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

注意:对于外部存储,请使用: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

https://stackoverflow.com/a/64539937/3541319

答案 2 :(得分:1)

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

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" />

相关问题