Skip to content

Commit

Permalink
filter sounds by: notification, ringtone, alarm. Thanks to @ccvlad.
Browse files Browse the repository at this point in the history
  • Loading branch information
saadqbal committed Sep 2, 2020
1 parent fd2ddf0 commit 88b6e20
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
3 changes: 1 addition & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ declare module 'react-native-notification-sounds' {
}

export function playSampleSound(s: Sound): void
export function stopSampleSound(): void

type NotificationSounds = { getNotifications(): Promise<Sound[]> }
type NotificationSounds = { getNotifications(type: string): Promise<Sound[]> };
export default NotificationSounds
}
26 changes: 23 additions & 3 deletions ios/NotificationSounds.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 88b6e20

Please sign in to comment.