button s sound makes button press "delay" with [buttonsound prepareToPlay]; - iphone

After the app starts, and I press the start button, it s "lagg" but I used this code into viewdidload and viewdidappear too:
gombhang = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"button4" ofType:#"mp3"]];
gombha = [[AVAudioPlayer alloc] initWithContentsOfURL:gombhang error:nil];
[gombha prepareToPlay];
gombha.delegate = self;
How can I fix this lagg? If I can t fix it apple will reject it?

As I know Audioplayer takes few seconds to load the audio file .

NSString *soundFile = [[NSBundle mainBundle] pathForResource:#"button4" ofType:#"wav"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFile]; AudioServicesCreateSystemSoundID((CFURLRef) CFBridgingRetain(soundFileURL), &_MySound); AudioServicesPlaySystemSound(_MySound);
And another way: I made an empty/ muted 0.1 sec sound file, and played in viewdidload, and this method loaded the avaudioplayer before I clicked on the start button, so it doesn t lagg anymore.

Related

How do I get sound to play in an animation I created in xcode?

I managed to create this application that includes an animation in it. But I want to make it stand out and more fun by adding a .wav file that I downloaded from the web. I want the sound to play for the duration of the animation. Only started coding a month ago so any help would be deeply appreciated
You could use the AVAudioPlayer
just create a method and connect it to your button or animation
-(void) playLoopingMusicInApplication {
AVAudioPlayer *audioPlayer;
// set the pathForResource: to the name of the sound file, and ofType: to AAC preferrably.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"WAV"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.delegate = self;
//infinite
player.numberOfLoops = -1;
[player play];
//[player release];
}
PS - You will need to get the AVFoundation framework, and the AudioToolbox framework in your project
hope this helps!

alert sound when start recording

First time i am capturing video in my App, but when i start capturing i want that button click sound as native application. I have searched a lot and came to know that it is a system sound. Can anyone please tell me how can i add that sound in my application when i start capturing video and when i stop capturing.
Currently i am using AVAudioPlayer to play sound as shown below
NSURL *url1 = [[NSBundle mainBundle] URLForResource:str withExtension:#"wav"];
_startAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil];
_startAudioPlayer.delegate= self;
[_startAudioPlayer prepareToPlay];
[_startAudioPlayer play];
Thanks in advance.....
Just import AudioToolbox.framework.
Create the URL for the source audio file
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: #"tap"
withExtension: #"aif"];
// Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (
soundFileURLRef,
&soundFileObject
);
Play sound
AudioServicesPlayAlertSound (soundFileObject);
Here is a nice tutorial link you can follow this
When you want to start capturing video at that time call this bellow method like this...
[self playSound];
use my this bellow method for play sound...
-(void)playSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"adriantnt_release_click" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
first store any mp3 or any audio file in your project folder For Ex: i add adriantnt_release_click.mp3 file.
Also add AVAudioPlayerDelegate in your .h file and add AudioToolbox.framework in your project..

Issue with playing video file

I am using following code to show video file in initial state.But when i launch app it start playing without giving me option to play.how i can achieve that
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"En_annan_resa_Master_ENG_PC" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
movie_obj = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[movie_obj.view setFrame:CGRectMake(1024*i, 0, 1024, 748)];
movie_obj.scalingMode=MPMovieScalingModeAspectFit;
[self.scrollView addSubview:movie_obj.view];
[movie_obj prepareToPlay];
I am expecting when i launch app it show mw video view and show control to play do not play automatically.
thanks for your help
check below answer
https://stackoverflow.com/a/15701076/1713478
just replace NO to En_annan_resa_Master_ENG_PC
try this your problem will solve

How do I loop a sound for an iPhone app?

I am trying to loop my sound file and I cannot find the parameter to loop the sound file! I'm about to pull out the last standing hair on my head. It loads fine....it plays....I just can't figure out the code on how to make it loop.
Here is my code:
NSString *eyeBGPath = [[NSBundle mainBundle] pathForResource:#"theeye" ofType:#"caf"];
CFURLRef eyeBGURL = (CFURLRef ) [NSURL fileURLWithPath:eyeBGPath];
AudioServicesCreateSystemSoundID(eyeBGURL, &eyeBackGroundID);
AudioServicesPlaySystemSound(eyeBackGroundID);
What am I doing wrong here?
I found this:
loops
Indicates whether the receiver restarts playback when it reaches the end of its content. Default: NO.
- (BOOL)loops
Return Value
YES when the receiver restarts playback when it finishes, NO otherwise.
Availability
Available in Mac OS X v10.5 and later.
See Also
– setLoops:
Declared In
NSSound.h
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSSound/loops
I'm confused on how to implement this.
This is the code that worked for me. If anyone needs help with this, let me know. Someone on the Apple Dev forum pointed me to the AVFoundation docs.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/theeye3.caf", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
Here is code to pick out a sound file based on a random number from an image array.
NSString *audioWiz = [[NSString alloc]initWithFormat:#"%d.caf", randNum];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:[#"%#/" stringByAppendingString:audioWiz], [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 0.1;
[audioPlayer play];
I think the answer you are looking for is here.
Short answer, use a MPMusicPlayerController.

How do I keep music playing after the user presses the hold button on the iPhone?

Hey guys how can I make an app keep playing an mp3 after pressed the hold/power button.
Here is the code I use for preparing the AVAudioPlayer:
- (void)PrepareAudio:(int)index
{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#",[_Audio objectAtIndex:Game]] ofType:#"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
MusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: newURL error: nil];
[newURL release];
[MusicPlayer setVolume: 1.5];
}
And this is the code when I press the button play:
- (IBAction)PushPlay: (id)sender
{
if(!MusicPlayer.playing)
[MusicPlayer play];
}
Best Regards
Carlos Vargas
If you are holding long enough to turn the device off, then there is no way. If you are pressing the button to lock the screen, the music can be played. Why don't you put some code up and let us see what you have tried. Hint: The answer lies within the Audio playback categories.