Skip to content Skip to sidebar Skip to footer

React Native System Soungs/ringtones

How can i get the list of system tones in react native e.g ring tones, notification tones, so that i can give users choice to set them as notification tone? I looked into some libr

Solution 1:

I am surely late, this is a solution I wrote on medium :

Implement Native Module

Go to : D:\Projects\VSCode\liturgikMobile\node_modules\react-native-ringtone-manager\android\src\main\java\com\reactlibrary\RNRingtoneManagerModule.java

And do the correction with this code:

@ReactMethodpublicvoidgetRingtones(Callback successCallback) {
    getRingsByType(RingtoneManager.TYPE_ALL, successCallback);
}

@ReactMethodpublicvoidgetRingsByType(int ringtoneType, Callback successCallback) {
    RingtoneManagermanager=newRingtoneManager(this.reactContext);
    manager.setType(ringtoneType);
    Cursorcursor= manager.getCursor();

    WritableArrayresult= Arguments.createArray();
    int key= 0;
    while (cursor.moveToNext()) { 
        WritableMapdata= Arguments.createMap();
        StringnotificationTitle= cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
        UrinotificationUri= Uri.parse(cursor.getString(RingtoneManager.URI_COLUMN_INDEX) + "/"
                + cursor.getString(RingtoneManager.ID_COLUMN_INDEX));
        Stringnotification= getPathFromUri(this.reactContext, notificationUri);
        data.putInt("key", key);
        data.putString("title", notificationTitle);
        data.putString("uri", notification);
        result.pushMap(data);
        key=key+1;
    }
    successCallback.invoke(result);
}

@SuppressLint("NewApi")public String getPathFromUri(Context context, Uri uri) {
    finalbooleanneedToCheckUri= Build.VERSION.SDK_INT >= 19;
    Stringselection=null;
    String[] selectionArgs = null;
    // Uri is different in versions after KITKAT (Android 4.4), we need to// deal with different Uris.if (needToCheckUri && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
        if (isExternalStorageDocument(uri)) {
            finalStringdocId= DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            return Environment.getExternalStorageDirectory() + "/" + split[1];
        } elseif (isDownloadsDocument(uri)) {
            finalStringid= DocumentsContract.getDocumentId(uri);
            uri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
        } elseif (isMediaDocument(uri)) {
            finalStringdocId= DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            finalStringtype= split[0];
            if ("image".equals(type)) {
                uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } elseif ("video".equals(type)) {
                uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } elseif ("audio".equals(type)) {
                uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }
            selection = "_id=?";
            selectionArgs = newString[] { split[1] };
        }
    }
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursorcursor=null;
        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
            intcolumn_index= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
        }
    } elseif ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    returnnull;
}

In your app, call it like this :

getRingtones(type) {  
 RingtoneManager.getRingsByType(type, (data) => {  
   this.setState({ selectedType: type, datas: data });

});

}

Type is RingtoneManager.TYPE_NOTIFICATION, RingtoneManager.TYPE_ALARM, RingtoneManager.TYPE_RINGTONE OR RingtoneManager.TYPE_ALL.

Then you will be able to find Ringtones on your phone:

enter image description here

Post a Comment for "React Native System Soungs/ringtones"