Custom iOS UILocalNotification sound does not play (possibly related to Xcode update) - objective-c

I'm trying to get a custom sound working on a UILocalNotification, and I'm just getting no sound at all. If I use UILocalNotificationDefaultSoundName, I indeed get the default sound, but when the custom sound is specified, there is no sound, just the message. The sound is less than 30 seconds and it's in the right format, as far as I can tell. Here's a screenshot of the file info:
I've inspected the .app directory in XCode's DerivedData directory, and the alarm.caf file is at the root of the app, which I believe means it's in the bundle (right?).
I'm pretty sure this was working a while ago, and I've since upgraded Xcode. Maybe that is a hint?
I've also tried deleting/reinstalling/rebooting as mentioned in other answers. As you can see, I'm calling cancelAllLocalNotifications first.
Does anyone have any idea what could be wrong?
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSLog(#"installing alarm");
[arguments pop]; // name
[arguments pop]; // title
alarm.alertBody = [arguments pop];
alarm.fireDate = [[NSDate date] addTimeInterval:[[arguments pop] intValue]/1000];
//alarm.soundName = UILocalNotificationDefaultSoundName;
alarm.soundName = #"alarm.caf";
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];

Your code seems to be good.
Try to clean your project, uninstall your app from your device/simulator, then re-install it. It could help maybe :)

I don't know the reason (and I didn't read documentation too), I just turned on the action property notification setHasAction:YES and the sound began to play.

please make sure that the iPhone is not in silent mode( because your code seems to be good )
just check the button on the side of your iPhone

Ok, so here's what happened. I forgot how the app handles the notification itself if it is still running. My code was only displaying a UIAlertView and not playing the sound. I'm not sure why it worked with the default sound. In any case, I added code like this to my AppDelegate:
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(#"didReceiveLocalNotification");
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"MarkMyTime"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
NSString *soundFilePath = [[NSBundle mainBundle]
pathForResource:notification.soundName ofType:nil];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
[fileURL release];
player.delegate = self;
[player prepareToPlay];
[player play];
[alertView show];
if (alertView) {
[alertView release];
}
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(#"Releasing player");
[player release];
}
This will show a UIAlertView and play the sound on the notification object. You also need to add the AVAudioPlayerDelegate interface to the AppDelegate to be able to assign the delegat to the player. I think if you are using ARC, this code could be simplified a bit.
#interface AppDelegate : PhoneGapDelegate <AVAudioPlayerDelegate> {
I'm not sure if this is the best approach, so feel free to chime in with any improvements.

Maybe you do not add the sound file (*.caf) in Xcode project: Build Phases/Copy Bundle Resources.

Your code is good, but check your iPhone setting
setting -> Notification center -> Your App -> Sound - > "On"
the sound should be "On".
So, to enable this, checked Inter App Audio at Capabilities in Targets of the application and it was Off Capabilities in Inter-app audio
change this to On.
Then local notification sound is working.

Related

Application crashes while using SDWebImageCache in IOS 9 in tableview

i am using SDWebImageCache for loading thumbnail images in UITableview custom cell. But in ios 9 the application crashes when i scroll the UITable. Similar is the case with AFNetworking too(UIIMageView + AFNetworking) class.
The class which i used from SDWebImageCache is:
[cell.imgMain sd_setImageWithURL:imageLoadUrl
placeholderImage:[UIImage imageNamed:#"*placeholder image*"]
options:SDWebImageRefreshCached];
The class which i used from UIIMageView + AFNetworking is:
[cell.imgMain setImageWithURLRequest:[NSURLRequest requestWithURL:imageUrl]
placeholderImage:[UIImage imageNamed:#"wachizLogoIcon.png"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
cell.imgMain.image=image;
}
failure:nil];
But still the application crashes and shows the following screen:
APPLICATION CRASHES IN IOS 9 and sometimes for IOS 8.4
Please help me from this situation. Lots of pressure ...
use this code
NSMutableArray *Imagedata;
-
NSString *Banner = [NSString stringWithFormat:#"%#",[[imagedata valueForKey:#""]]objectAtIndex:indexpath.row];
NSLog(#"%#",Banner);
[cell.imgMain sd_setImageWithURL:[NSURL URLWithString:Banner] placeholderImage:[UIImage imageNamed:#"placehoder2.jpg"]];
I faced similar issue once. It was basically a low memory crash when the SDWebImageCache tries to cache images. Try implementing didReceiveMemoryWarning methods to debug it. Reset the cache on low memories. Also set a maxCacheSize for your images.
These methods didn't worked out well for me so i switched to Haneke. Still I had to set diskCapacity to a certain limit.
When u cache an image this will store in your device RAM... please check your RAM in debugger...if you cache lots of image this will terminate app due to Memory warning.
Use this
add code to Appdelegate
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
NSLog(#"Clearing cache-------------");
[[SDImageCache sharedImageCache] clearMemory];
[[SDImageCache sharedImageCache] cleanDisk];
[[SDImageCache sharedImageCache] clearDisk];
[[SDImageCache sharedImageCache] setValue:nil forKey:#"memCache"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"cache cleared"
message:Nil
delegate:Nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alert show];
}

App crashes when using AHAlertView project

I'm trying to display a custom UIAlertView and I'm using AHAlertView:
https://github.com/warrenm/AHAlertView
I added both AHAlertView.m and .h to my project and add the following to the viewDidLoad method:
NSString *title = #"Alert View Title";
NSString *message = #"This is a message that might prompt you to do something.";
AHAlertView *alert = [[AHAlertView alloc] initWithTitle:title message:message];
__weak AHAlertView *weakAlert = alert;
[alert setCancelButtonTitle:#"Cancel" block:^{
weakAlert.dismissalStyle = AHAlertViewDismissalStyleTumble;
}];
[alert addButtonWithTitle:#"OK" block:^{
weakAlert.dismissalStyle = AHAlertViewDismissalStyleZoomDown;
}];
[alert show];
The problem is when I'm tapping either one of the buttons, the app crashes with:
"Application windows are expected to have a root view controller at the end of application launch"
I don't know what I did wrong, I looked at the sample project and this is the way the alert is being used.
How can I implement it correctly?
Did you activate -fno-objc-arc option ? If you did, deactivate it, AHAlertView support ARC

AVAudioPlayer will not play when device is locked

I am using AVAudioPlayer in my application. When I select a song it plays, but my problem is, it is not playing when the device gets locked.
You need to read Executing Code in the Background in Apple's documentation. If you set the UIBackgroundModes key in you app's Info.plist to audio, audio will keep playing while backgrounded.
See the sections Declaring the Background Tasks You Support and Playing Background Audio in the aforementioned documentation.
All we need to make music play in the background with AVAudioPlayer by two step:
Step 1: Choose Project -> Capabilities -> Enable Background Modes -> Select Audio Mode
Step 2: Use this code:
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:#"demo" withExtension:#"mp3"];
avSound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
[avSound setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[avSound prepareToPlay];
[avSound setNumberOfLoops:-1];
[avSound setVolume:1.0];
Note that with this line the music can start even if the app was in the background:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
As per my knowledge it is not possible to play sound when device locked. Can't you just disallow device to lock and alive apps active always until user close it manually ?

iPod - Let the native music app to play sound in background

I am building an application for iPod.
When running it, the native iPod music app should continue play in background, but this is not happening.
I think it might have something to do with the sound that it is played by my app. For playing a short sound at a certain point, I am using AVAudioPlayer. The code looks like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"mp3"];
NSLog(#"%#", path);
player = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:path] error:nil];
[player prepareToPlay];
[player play];
Could this interfere with the native music player on iPod?
And another thing. In my app, I have integrated AdWhirl and it appears to use AudioToolbox.
Thanks a lot,
George
You must set up your AVAudioSession to use the Ambient category. This tells the system that your sound should blend with any already playing sound. The best way to do this in iOS 4 is in the app delegate, like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
// ...
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// reactivate audio session
[[AVAudioSession sharedInstance] setActive: YES error: nil];
}
Read up on Audio Sessions. If your app produces sound, you always want be conscious of and in control of your audio session.

how to play sound when alertview appears iphone sdk?

i want to play a sound file when alertview appears and plays continuously till user clicks on ok or cancel.how do i do this?
As Zoul says, you set-up and play your sound as you call [myAlert show] and cancel the sound in the alert view callback. Your code will look something like this:
AVAudioPlayer *myPlayer;
// ...
// create an alert...
NSError *error;
myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:mySoundFileURL error:&error];
// handle errors here.
[myPlayer setNumberOfLoops:-1]; // repeat forever
[myPlayer play];
[myAlert show];
// ...
// in alert callback.
[myPlayer stop];
[myPlayer release];
As you already call the show method to display the dialog, why don’t you simply start playing the sound there and stop in the alert view callback? For the sound itself you can use AVAudioPlayer.