反应本机系统声音/铃声 [英] React native System Soungs/Ringtones

查看:61
本文介绍了反应本机系统声音/铃声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取 react native 中的系统铃声列表,例如铃声、通知铃声,以便我可以让用户选择将它们设置为通知铃声?

我查看了一些库,如 react-native-ringtone-managerreact-native-sound,但没有找到这个功能.

解决方案

我肯定迟到了,这是我在medium上写的一个解决方案:

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.

解决方案

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:

这篇关于反应本机系统声音/铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆