ユーザーがリソースからサウンド ファイルをダウンロードし、それらを着信音 (具体的には通知音) として使用できるようにするアプリを作成しています。
これまでのところ、アプリは指定したディレクトリに mp3 ファイルを正常に作成し、携帯電話のファイル エクスプローラーを使用してファイルを開くことができ、期待どおりに再生されますが、システムのサウンド設定では数値としてのみ表示され、uri のid と仮定し、サウンドを再生しません。着信音を手動で設定しようとすると、利用可能なサウンド ファイルに表示されません。
私のコード:
//this is just here temporarily, after this is working it will be moved to onCreate on the main activity.
if(!Settings.System.canWrite(holder.itemView.getContext())){
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + context.getPackageName()));
context.startActivity(intent);
}
//tmpFile is the sound file that is successfuly created right before these lines of code
//this part is used to get the length in seconds of the soundfile - not sure if is important to add it to MediaStore
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(tmpFile.getAbsolutePath());
String durationStr = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
assert durationStr != null;
int seconds = parseInt(durationStr);
seconds = seconds / 1000;
//adding contentvalues to the newly created file
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, tmpFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, fileTitle);
values.put(MediaStore.MediaColumns.SIZE, tmpFile.length());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.DURATION, seconds);
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//adding the uri to MediaStore with the above extra values then passing it to RingtoneManager
Uri uri = MediaStore.Audio.Media.getContentUriForPath(tmpFile.getAbsolutePath());
assert uri != null;
context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + tmpFile.getAbsolutePath() + "\"", null);
Uri newUri = context.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION, newUri);
何か不足していますか?
SDK 30