React native System Soungs/Ringtones

为君一笑 提交于 2019-12-14 01:52:58

问题


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 libraries like react-native-ringtone-manager and react-native-sound but did't find this functionality.


回答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:

@ReactMethod
public void getRingtones(Callback successCallback) {
    getRingsByType(RingtoneManager.TYPE_ALL, successCallback);
}

@ReactMethod
public void getRingsByType(int ringtoneType, Callback successCallback) {
    RingtoneManager manager = new RingtoneManager(this.reactContext);
    manager.setType(ringtoneType);
    Cursor cursor = manager.getCursor();

    WritableArray result = Arguments.createArray();
    int key= 0;
    while (cursor.moveToNext()) { 
        WritableMap data = Arguments.createMap();
        String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
        Uri notificationUri = Uri.parse(cursor.getString(RingtoneManager.URI_COLUMN_INDEX) + "/"
                + cursor.getString(RingtoneManager.ID_COLUMN_INDEX));
        String notification = 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) {
    final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19;
    String selection = 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)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            return Environment.getExternalStorageDirectory() + "/" + split[1];
        } else if (isDownloadsDocument(uri)) {
            final String id = DocumentsContract.getDocumentId(uri);
            uri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
        } else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            if ("image".equals(type)) {
                uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }
            selection = "_id=?";
            selectionArgs = new String[] { split[1] };
        }
    }
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    return null;
}

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:



来源:https://stackoverflow.com/questions/51787902/react-native-system-soungs-ringtones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!