Reputation: 1345
right now, I'm avoiding loading system audio for the devices and emulator I have by hardcoding them in SQLite NOT LIKE ...
statements while querying the ContentResolver
, but, what I have found that the location for the storage of sytem audio(like alarm tones and all the audio factory loaded) doesn't have a standard location in all android device.
So, to avoid loading all system audio, I need a way to fetch the directory where these audio are located, so far, I have gathered a data of these locations where the sytem audio might be:
"/system%",
"/storage/emulated/legacy/Ringtones/%",
"/storage/emulated/0/Ringtones%",
"/product/media/audio%"
but, since these might not be adequate, is there a way to fetch the directory path where the system audio is located in android?
Upvotes: 3
Views: 230
Reputation: 517
Code to List all alarms in fragment:
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())
}
You need to add
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Upvotes: 1
Reputation: 401
You can use something like this:
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.
}
References:
https://developer.android.com/guide/topics/providers/content-provider-basics#java
https://stackoverflow.com/a/64539937/3541319
Upvotes: 2
Reputation: 367
Query audio files with MediaStore , then filter audio files with AudioColumns , and more specific for alarm sounds use IS_ALARM.
As you say :
I have found that the location for the storage of sytem audio doesn't have a standard location in all android device.
MediaStore is a database which holds references of the files and it is a solution to query files in different system storage locations.
Upvotes: 3