Xamarin.Android 10 Q how to store/access custom sound using MediaStore

Cdn_EuroCdn_Euro Member ✭✭✭
edited October 13 in Xamarin.Android

My goal is to store a sound in MediaStore and then use that sound for push notifications.

I need some help using the MediaStore. Reading the documentation for Android Q I understand that I cannot just store a sound in External Storage like I did before (and it worked) and then use that sound for my push notifications channel. Now access to external storage is limited. I understand that one should use MediaStore instead. I am trying to store a sound in the MediaStore.

Questions:
1. How do I copy the sound into the media store, because my method below is incomplete.
2. How do I retrieve/access tthat sound from the mediastore in order to set it as the default sound for a push notifications channel.Is the access still done via Uri...or by cursor?
3. How to set it as default sound for a notification channel.

This is my code so far:

// param filePath is the path of the file in the AppData Directory from Xamarin Essentials (cache directory the temporary location)
// and looks like: /data/user/0/com.company.appname/files/customalert.wav
// I want to return a Uri of the file after inserted in the MediaStore if thats' possible in order to set it for the notification channel

public Android.Net.Uri InsertCustomSoundInMediaStore(string filePath)
        {
            ContentValues values = new ContentValues();
            ContentResolver contentResolver = CrossCurrentActivity.Current.AppContext.ContentResolver;

            var appName = AppInfo.Name;

            values.Put(MediaStore.MediaColumns.Title, "customalert");
            values.Put(MediaStore.MediaColumns.MimeType, "audio/wav");
            values.Put(MediaStore.MediaColumns.Size, GetDuration(filePath));
            values.Put(MediaStore.Audio.Media.InterfaceConsts.Artist, appName);
            values.Put(MediaStore.Audio.Media.InterfaceConsts.IsRingtone, true);
            values.Put(MediaStore.Audio.Media.InterfaceConsts.IsNotification, true);
            values.Put(MediaStore.Audio.Media.InterfaceConsts.IsAlarm, true);
            values.Put(MediaStore.Audio.Media.InterfaceConsts.IsMusic, false);

            Android.Net.Uri newUri = contentResolver.Insert(MediaStore.Audio.Media.ExternalContentUri, values);   

            try
            {
                   // copy file from cache to OS (MediaStore)?
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return newUri;
        }

Best Answer

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited October 14

    If you want to store a sound in External Storage like you did before, you can add the android:requestLegacyExternalStorage = “true” to tab in AndroidManifest.xml file.

    If you need to know more details about it, you can refer to the following link.
    https://developer.android.com/training/data-storage/files/external-scoped

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @Cdn_Euro Do you have some update for this issue? If the reply is helpful, please mark it as an answer, it will help others who have similar issue.

  • Cdn_EuroCdn_Euro Member ✭✭✭

    @LeonLu havent had a chance to work on it following your advice, it’s Thanksgiving here. Thanks for giving me that info and I sure will apply it soon and mark the answer.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Ok, waitting for your update.

  • Cdn_EuroCdn_Euro Member ✭✭✭

    @LeonLu Thanks so much it works, I have spend literally days on trying to figure this out.

Sign In or Register to comment.