gpt4 book ai didi

android - react native 系统声音/铃声

转载 作者:行者123 更新时间:2023-11-29 02:26:52 36 4
gpt4 key购买 nike

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

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

最佳答案

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

Implement Native Module

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

并使用此代码进行更正:

@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;
}

在您的应用中,这样调用它:

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

});

类型 是 RingtoneManager.TYPE_NOTIFICATION、RingtoneManager.TYPE_ALARM、RingtoneManager.TYPE_RINGTONE 或 RingtoneManager.TYPE_ALL。

然后您就可以在手机上找到铃声了:

enter image description here

关于android - react native 系统声音/铃声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51787902/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com