0

I've been working on an app that requires me to play the appropriate ringtone from native code. Till now i found no solution for my problem. Is there any known ways to get the ringtone bytes to decode and play them on i.e.: OpenSL ES?

My current impression is that those files are, in general, protected and require either root or some other shady mechanism to be accessed. Is this correct?

Thanks in advance.

Agah PT
  • 130
  • 1
  • 9

1 Answers1

0

I've got a working solution for this problem, it involves 1 JNI call to get a ParcelFileDescriptor in order to then get the appropriate FD that can be used on OpenSLES natively. Just need to keep in mind to save the ParcelFileDescriptor for closing the FD at the end and not getting garbage collected.

Note: The API Level required is 12 to get the FD

Note2: If you wnt you can detachFd() and then you must close the FD on native code and do not need to keep the reference.

static public ParcelFileDescriptor getFileDescriptor(Context context) {
    Uri uri =  RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

    ContentResolver contentResolver = context.getContentResolver();

    ParcelFileDescriptor openFileDescriptor;
    try {
        openFileDescriptor = contentResolver.openFileDescriptor(uri, "r");
    } catch (FileNotFoundException e) {
        return null;
    }

    return  openFileDescriptor;
}
Agah PT
  • 130
  • 1
  • 9