开发者

UILocalNotifications playing Custom sound

开发者 https://www.devze.com 2023-03-28 17:35 出处:网络
I implemented local notification in my app but I am just wondering is there a way to play a sound that is not part of the main bundle of iPhone App.

I implemented local notification in my app but I am just wondering is there a way to play a sound that is not part of the main bundle of iPhone App. Basically in my app, I want user to record a sound that gets played when the local notification is generated instead of playing a pre-recorded or default sound. As far as i know this can be implementable because i have seen 2-3 App in app store which is doing the same thing which i want to do

- (void)alertSelector:(NSString *)AlertTitle WithFiringTime:(NSDate *)date
{ 

UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];
   [localNotification setFireDate:date];
   [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
   NSDictionary *data = [NSDictionary dictionaryWithObject:date forKey:@"payload"];       
   [localNotification setUserInfo:data];[localNotification setAlertBody:AlertTitle];   
   [localNotification setAlertAction:@"View"]; [localNotification setHasAction:Y开发者_如何学运维ES]; 
   localNotification.soundName=@"voice.aif"; 

   if (!localNotification)
          return;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
}


My guess is your sound isn't formatted appropriately to play by the OS. Make sure your sound is formatted in IMA4 format. Also, make sure your file is in your application bundle, and you should be good to go.

For more reference and similar question, see this SO question.

Choose custom sound for Local Notifications

Here is a link for Apple's Local and Push Notification Guide. It explains the sounds in detail and the default sounds that can be used.


From the description of the soundName property -

For this property, specify the filename (including extension) of a sound resource in the application’s main bundle or UILocalNotificationDefaultSoundName to request the default system sound.

It doesn't seem that sounds other than those present in the bundle can be used. Are you sure that the apps are using local notifications with their custom sounds? Maybe they are playing sounds with alerts or something like that.

HTH,

Akshay


notif.soundName = @"sound.caf";

That should work. Make sure the sound is actually in your app’s bundle, is in the correct format (linear PCM or IMA4—pretty much anywhere that explains how to convert sounds for iOS will tell you how to do that), and is under 30 seconds.

You can convert from wav and mp3 using terminal :

afconvert -f caff -d LEI16@44100 -c 1 in.wav sound.caf

'in.wav' is input sound which u want to convert and 'sound.caf' is output file ,change directory to where u want to have the out put file


Make sure your sound file is added to the "Copy Bundle Resources" list under Project Settings->(Target)->Build Phases. This fixed the issue for me. At first I only got the default sound, even though I specified a custom file name (and the file was added to the project). But after I manually added the file to this list it started working.

I used a system sound from my mac to test with. I converted it using this command in a console:

afconvert /System/Library/Sounds/Basso.aiff ~/Downloads/alert2.caf -d ima4 -f caff -v


Well I was experiencing same Issue , It sounds like this was not supported before or not doable to play custom sound for local notification that is not included in App Bundle. But later this is supported and you can assign custom sounds from App container specifically in Library/Sounds directory . And it worked with me . you can either download sounds from server to this directory or test it by copying a .caf(Apple said other extensions may work) file from bundle to Library/sounds directory and then remove reference for the file from bundle so that you can make sure that if sounds played it is from directory out of bundle . This all works very well , the main issue with me if user force quoted the app it stops working for local notification while it still works with push kit notifications , Im scheduling local one though when push kit push arrives. Here is the code for testing playing sound from directory :

 func copyFileToDirectoryFromBundle()
{
    // get reference to library directory
    let LibraryDirectoryPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)

    // see what is in there 
    for path in LibraryDirectoryPaths
    {
        print("di=\(path)")
    }

    // prepare for sound directory
    let libraryDirectoryPath: AnyObject = LibraryDirectoryPaths[0]
    let soundsDirectoryPath = libraryDirectoryPath.stringByAppendingPathComponent("Sounds")

    // create sounds directory under library if not there already
    if !NSFileManager.defaultManager().fileExistsAtPath(soundsDirectoryPath)
    {
    do {
        try NSFileManager.defaultManager().createDirectoryAtPath(soundsDirectoryPath, withIntermediateDirectories: false, attributes: nil)
        print("sounds library created,,,")
    } catch let error as NSError {
        print("creating sounds directory error = \(error.localizedDescription)");
    }
    }
    else
    {
        print("Sounds directory is there already under Library")
    }

    do {
        // sounds directory should have been added under library
        let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL( NSURL(string: libraryDirectoryPath as! String)!, includingPropertiesForKeys: nil, options: [])
        print("Library directs=\(directoryContents)")


        // prepare for adding subdirectory with file name from bundle
        // here is where you should save files if downloaded from server 
        // here im just moving one from bundle 

        let subDirectoryPath = directoryContents.last!

        if let pathFromBundle = NSBundle.mainBundle().pathForResource("notification", ofType:"caf") {
            let myFilePathUnderSounds = "\(subDirectoryPath.path!)\("/notification.caf")"
            if !NSFileManager.defaultManager().fileExistsAtPath(myFilePathUnderSounds)
            {
            do {
                print("bundle path=\(pathFromBundle)and datacontainer path=\(subDirectoryPath.path!)")
                try NSFileManager.defaultManager().copyItemAtPath(pathFromBundle, toPath:myFilePathUnderSounds )
                print("item copied")
            } catch let error as NSError {
                print(error.localizedDescription);
            }
            }
            else
            {
                print("Your file from bundle path = \(pathFromBundle) is already there under Sounds")
            }
        }
        else
        {
            print("Item not found in bundle")
        }

        // see item there after you add it
        let subDirectories = try NSFileManager.defaultManager().contentsOfDirectoryAtURL( subDirectoryPath, includingPropertiesForKeys: nil, options: [])

        print("files under sounds")
        for fileDirectory in subDirectories
        {
            print(fileDirectory)
        }

    } catch let error as NSError {
        print(error.localizedDescription)
    }

}

After making sure .caf file is under Library/Sounds directory . All you have to do is merely assigning file name with extension to local notification soundName property like if the file is in bundle.


are you cleaning the old notifications with?

UIApplication* app = [UIApplication sharedApplication];
NSArray*  oldNotifications = [app scheduledLocalNotifications];
if ([oldNotifications count] > 0) {
    [app cancelAllLocalNotifications];
}

else you can, delete the app from the phone (maybe need a reboot too) the it should work :-)

beside that the code mentioned above should be correct. and this line localNotification.soundName=@"voice.aif"; should be enough to play custom sounds. but if you had notification tested first with no sound-file added, the default sound file (UILocalNotificationDefaultSoundName) is registered! and to re-register you have to cancel the previous notification (cancelAllLocalNotifications) or delete the app, to be able to "re"-register your notfication,

beside that, the apple doc says:

Sounds that last longer than 30 seconds are not supported. If you specify a file with a sound that plays over 30 seconds, the default sound is played instead.

hope it helps now :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消