From 88b6e203e5c6ef0225d58975e5bbec8aa198f55d Mon Sep 17 00:00:00 2001 From: Asad Iqbal Date: Wed, 2 Sep 2020 12:35:07 -0500 Subject: [PATCH] filter sounds by: notification, ringtone, alarm. Thanks to @ccvlad. --- README.md | 6 +++- .../NotificationSoundsModule.java | 32 ++++++++++++------- index.d.ts | 3 +- ios/NotificationSounds.m | 26 +++++++++++++-- package.json | 2 +- 5 files changed, 51 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b1ea8e7..6e9b10d 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,12 @@ import NotificationSounds, { playSampleSound } from 'react-native-notification-s /* get the list of System notification sounds. This function returns an array the array contains Title, Url, SoundID +You can pass the following values to the getNotifications: + 1. notification: get the list of notifictaion sounds + 2. ringtone: get the list of ringtones + 3. alarm: get the list of alarm sounds (andoid only) */ -NotificationSounds.getNotifications().then(soundsList => { +NotificationSounds.getNotifications('notification').then(soundsList => { console.warn('SOUNDS', JSON.stringify(SoundsList)); /* Play the notification sound. diff --git a/android/src/main/java/com/reactlibrarynotificationsounds/NotificationSoundsModule.java b/android/src/main/java/com/reactlibrarynotificationsounds/NotificationSoundsModule.java index cb4bd52..1d149f4 100644 --- a/android/src/main/java/com/reactlibrarynotificationsounds/NotificationSoundsModule.java +++ b/android/src/main/java/com/reactlibrarynotificationsounds/NotificationSoundsModule.java @@ -1,6 +1,7 @@ package com.reactlibrarynotificationsounds; import android.database.Cursor; +import android.media.MediaPlayer; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; @@ -22,7 +23,7 @@ public class NotificationSoundsModule extends ReactContextBaseJavaModule { private final ReactApplicationContext reactContext; - private Ringtone r; + private MediaPlayer thePlayer; public NotificationSoundsModule(ReactApplicationContext reactContext) { super(reactContext); @@ -41,9 +42,21 @@ public void sampleMethod(String stringArgument, int numberArgument, Callback cal } @ReactMethod - public void getNotifications(final Promise promise) { + public void getNotifications(String soundType, final Promise promise) { RingtoneManager manager = new RingtoneManager(this.reactContext); - manager.setType(RingtoneManager.TYPE_NOTIFICATION); + Integer ringtoneManagerType; + + if (soundType.equals("alarm")) { + ringtoneManagerType = RingtoneManager.TYPE_ALARM; + } else if (soundType.equals("ringtone")) { + ringtoneManagerType = RingtoneManager.TYPE_RINGTONE; + } else if (soundType.equals("notification")) { + ringtoneManagerType = RingtoneManager.TYPE_NOTIFICATION; + } else { + ringtoneManagerType = RingtoneManager.TYPE_ALL; + } + + manager.setType(ringtoneManagerType); Cursor cursor = manager.getCursor(); WritableArray list = Arguments.createArray(); @@ -64,21 +77,18 @@ public void getNotifications(final Promise promise) { } - @ReactMethod public void playSample(String uri){ try { Uri notification; if (uri == null || uri.length() == 0) { notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - } - else { + } else { notification = Uri.parse(uri); } - if (r != null) - r.stop(); - Ringtone r = RingtoneManager.getRingtone(this.reactContext, notification); - r.play(); + if (thePlayer != null) thePlayer.stop(); + thePlayer = MediaPlayer.create(this.reactContext, notification); + thePlayer.start(); } catch (Exception e) { e.printStackTrace(); } @@ -87,7 +97,7 @@ public void playSample(String uri){ @ReactMethod public void stopSample() { try { - if (r != null) r.stop(); + if (thePlayer != null) thePlayer.stop(); } catch (Exception e) { e.printStackTrace(); } diff --git a/index.d.ts b/index.d.ts index 1b3575a..b23bf19 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,8 +6,7 @@ declare module 'react-native-notification-sounds' { } export function playSampleSound(s: Sound): void - export function stopSampleSound(): void - type NotificationSounds = { getNotifications(): Promise } + type NotificationSounds = { getNotifications(type: string): Promise }; export default NotificationSounds } diff --git a/ios/NotificationSounds.m b/ios/NotificationSounds.m index d327d42..d801211 100644 --- a/ios/NotificationSounds.m +++ b/ios/NotificationSounds.m @@ -11,12 +11,20 @@ @implementation NotificationSounds callback(@[[NSString stringWithFormat: @"numberArgument: %@ stringArgument: %@", numberArgument, stringArgument]]); } -RCT_REMAP_METHOD(getNotifications, loadSoundsWithResolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) +RCT_REMAP_METHOD(getNotifications, soundTypeParamentr:(NSString *)soundType loadSoundsWithResolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { + + NSURL *directoryURL; + + if ([soundType isEqualToString:@"ringtone"]) { + directoryURL = [NSURL URLWithString:@"/Library/Ringtones"]; + } else { + directoryURL = [NSURL URLWithString:@"/System/Library/Audio/UISounds"]; + } + NSMutableArray *audioFileList = [[NSMutableArray alloc] init]; NSFileManager *fileManager = [[NSFileManager alloc] init]; - NSURL *directoryURL = [NSURL URLWithString:@"/System/Library/Audio/UISounds"]; NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey]; NSDirectoryEnumerator *enumerator = [fileManager @@ -67,15 +75,27 @@ @implementation NotificationSounds } +SystemSoundID soundID = 0; -RCT_EXPORT_METHOD(playSample:(int)soundID) +RCT_EXPORT_METHOD(playSample:(NSString *) soundUrl) { + NSURL *url = [NSURL URLWithString:soundUrl]; + AudioServicesDisposeSystemSoundID(soundID); + //Register the sound to the system + AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID); AudioServicesPlaySystemSound(soundID); AudioServicesPlaySystemSoundWithCompletion(soundID, ^{ + AudioServicesRemoveSystemSoundCompletion(soundID); AudioServicesDisposeSystemSoundID(soundID); }); + } +RCT_EXPORT_METHOD(stopSample) +{ + AudioServicesRemoveSystemSoundCompletion(soundID); + AudioServicesDisposeSystemSoundID(soundID); +} @end diff --git a/package.json b/package.json index 8b17636..45e75e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-notification-sounds", - "version": "0.3.0", + "version": "0.5.0", "description": "Native notification sounds list. returns the id, title and url of the sounds", "main": "index.js", "type": "index.d.ts",