I have thoroughly looked through this forum and I have not found a solution to this issue. When my app launches during the splash screen I want a 5 sec sound clip to play then stop when the app is done launching and the home screen is shown.
In the AppDelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"bknewsroom2"
ofType:#"caf"]];
AVAudioPlayer *launchClip = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[launchClip play];
Now I have a whole bunch of stuff loading within didFinishLaunchingWithOptions this is just one of many things. The sound won't launch in the simulator nor will it launch on my phone. So am I missing something? Again it is a 5 second clip that I want to play when the app launches for the first time. Any advice would be greatly appreciated.
I fixed this issue:
in the AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//------ PLAY SOUND CLIP WHILE LOADING APP -----
NSLog(#"launch banner with clip");
NSURL *clip = [[NSBundle mainBundle] URLForResource: #"soundfile" withExtension:#"caf"];
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
[self.startupPlayer play];
Now when the application launches, the sound clip plays. It does hiccup once which I'm looking into, but the function works. Also, I put this at the top of didFinishLaunchingWithOptions if it was in the middle some place, it would work.
Related
I am working on a simple game and after every screen touch, an animation is happening on a small UIImageView and this happen very well and the app is running smoothly with CADisplayLink as the timer.
I added an mp3 file to play after every touch with 1 second lengh as AVAudioPlayer imagine a sound like : Bip
So the first time that I touch the screen first hiccup happens an the app freeze for less than a second that I can say it is ok coz it's the first time that the sound allocate memory.
The problem happens later when u touch the screen again if I touch it earlier than 3 seconds, the app doesn't hiccups but if I wait 4 seconds and more, the app starts to hiccup after every touch.
Every time if I touch again and again earlier than 3 seconds between touches, the app doesn't hiccups, but after 4 seconds between touches, the app hiccups.
Any idea to solve hiccups?
This is some code if needed
#property (nonatomic, strong) AVAudioPlayer *mySound;
- (void)viewDidLoad {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"bip" ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
self.mySound = newPlayer;
[mySound prepareToPlay];
[mySound setDelegate:self];
}
and after touch happens
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (location.x < viewWidth/2) {
[mySound play];
} else {
[mySound play];
}
}
Make a very short silent sound. Use your timer to play that (with another sound player) every second or so. — You see what the idea is here; we are try to keep the media server alive so that it is ready to go when we want our sound effect. Since you say there's no problem before 3 seconds, that makes me think that after 3 seconds the media server has gone back to sleep. Our goal is to keep "tickling" it so that can't happen.
In general, however, my impression is that AVAudioPlayer is not intended for this sort of thing. You should probably be using AVAudioEngine, which can be kept running and made to play sounds without latency.
I'm trying to create an application that plays a sound in one view controller (background music) and mutes it in the other view controllers. I have the part of the code figured out for playing background music but I can't get the application to mute it in another window? How do you I fix this? Thank you so much! :)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *music = [[NSBundle mainBundle]pathForResource:#"Waltz" ofType:#"wav"];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:music] error: NULL];
audioPlayer.delegate = self;
audioPlayer.numberOfLoops = -1;
[audioPlayer play];`
You can also use notifications to pause/restart the music. That way, a view controller that doesn't know anything about the music player could simply:
[[NSNotificationCenter defaultCenter] postNotificationName:#"STOP_MUSIC" object:nil];
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.
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 ?
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.