I have code which plays a sound after I clicked a button. When I click this button a second time first comes a thing like a reset or so.
What I want: Every time I click the button I want to play the sound immediately without reset button.
My code:
-(IBAction)playfrosch {
if (gestartet == 0) {
gestartet = 1;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/frosch.mp3", [[NSBundle mainBundle]resourcePath]]];
NSError *error;
audioPlayerfrosch = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayerfrosch.numberOfLoops = 0;
[audioPlayerfrosch play];
[startfrosch setTitle:#"Frosch" forState:UIControlStateNormal];
} else {
gestartet = 0;
[startfrosch setTitle:#"Nochmal?" forState:UIControlStateNormal];
}
}
I think my problems are the part after "else".
This is a logic problem. Your code only executes the sound play code if gestartet == 0.
So move the sound play code outside of the if statement. Problem solved.
Related
I'm using this code to load and begin playing a sound:
- (void) buzz {
if (buzzSound == nil) {
[[AVAudioSession sharedInstance] setActive: YES error: nil];
NSError *error;
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *soundFile = [[NSURL alloc] initFileURLWithPath: [mainBundle pathForResource: #"buzzer_8"
ofType: #"wav"]];
buzzSound = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFile error: &error];
buzzSound.delegate = self;
if (DEBUG_BLE && error != nil)
printf("Error loading sound: %s\n", [[error localizedDescription] cStringUsingEncoding: NSUTF8StringEncoding]);
}
// Play the sound.
buzzSound.numberOfLoops = -1;
[buzzSound prepareToPlay];
[buzzSound setVolume: 1.0];
if (![buzzSound play]) {
self.buzzSound = nil;
[self buzzSound];
}
}
This code is triggered by a touch down event on a button. When the button gets a touch up event (on or off of the button), it stops the sound with:
[buzzSound stop];
The buzz sound itself is a short .wav file.
The first time buzz is called, the sound plays once. It does not repeat. On subsequent calls it does repeat. Why?
Intermittently, if I play the sound for a short period of time that is about the same length of time as one cycle through the sound file, the sound fails on subsequent calls to buzz, and play returns NO. Thereafter, play will always return NO until the sound is reloaded, after which is plays the sound once as noted above and then starts working correctly until the sound is played for a short time, again.
I do have audioPlayerDecodeErrorDidOccur:error: set up; no error is reported.
Hi in my application, audio is playing when user press the button but now I have two audio buttons like audio1 and audio2, now I want to play second audio after a time interval once the audio1 is completed by clicking the same button.
- (IBAction)btn1:(id)sender {
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"audio" ofType:#"mp3" ];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
}
The above code I have used for play one audio now I want to play second audio once the first audio completed please tell me how to make it.
Thanks.
Try this:
You have to add a bool variable with the name 'firstAudioPlayed' on start the variable should be NO or FALSE.
- (IBAction)btn1:(id)sender {
if (![self.audioPlayer isPlaying]) {
NSString *audioPath;
if (self.firstAudioPlayed) {
audioPath = [[NSBundle mainBundle] pathForResource:#"audio2" ofType:#"mp3" ];
} else {
audioPath = [[NSBundle mainBundle] pathForResource:#"audio1" ofType:#"mp3" ];
}
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
self.firstAudioPlayed = !self.firstAudioPlayed;
}
}
I'm not sure if it's working like this, else just ask with a comment...
Explication of the code:
First you check if the player is already playing a song, if he isn't, you check if the first audio already has played (self.firstAudioPlayed) with this you can give the correct path to load the audio file. Now you play this audio file.
(Sorry for my grammar and my english, corrections are welcome)
You probably should use player's delegate. Set audioPlayer.delegate=self; and implement audioPlayerDidFinishPlaying:successfully: method to see when a player finished playing.
Then you can do whatever you want in there, for example start the second audio.
I have several views, one of them is an "options" view, in this view there will be a button that will allow the user to click a button to "stop" the background music, or click it to turn it back on. Im pretty sure I have the general idea of how to do this, the problem is, my code isnt working and I am not sure why.
this is the code from the implementation file for the "options" class:
- (IBAction)musicIO:(id)sender {
MenuViewController *myMusicPlayer = [[MenuViewController alloc] init];
[myMusicPlayer backgroundMusicStart];
}
it calls the method in my menu code(a different class)
This is my menu code:
-(IBAction) backgroundMusicStart{
if( i != 1){
NSURL * URL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/backgroundMusic.wav", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
backgroundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:URL error:&error];
backgroundPlayer.numberOfLoops = -1;
[backgroundPlayer play];
i = 1;
NSLog(#"In start music");
}
else if ( i == 1 ) {
[backgroundPlayer stop];
i = 0;
NSLog(#"In stop music");
}
}
"i" is a globally declared variable.
The problem with the code:
it runs, NSLog outputs that it gets into both if statements fine, the problem is when it is in the [backgroundPlayer stop] bracket, it doesnt stop the music, and I cant figure out why.
You're initialising backgroundPlayer every time this method is invoked and in only one of the cases. What you should be doing is make backgroundPlayer an instance variable and act on the shared instance of it... Example follows:
// static AVPlayer *backgroundPlayer;
- (IBAction) backgroundMusicStart {
if (backgroundPlayer == nil) {
backgroundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:URL error:&error];
backgroundPlayer.numberOfLoops = -1;
}
if (i == 1) { [backgroundPlayer stop]; }
else if (i == 0) { backgroundPlayer play]; }
}
*This is my first stackoverflow question so apologies if I am doing something wrong.
I am trying to create a simple sound board app. I'm having trouble getting my AVAudioPlayer on xCode 4.1 (iOS 6.1.2) to update where the sound file to play is located. I wanted to avoid having multiple audio players (audioPlayer2, audioPlayer3, etc) so I am trying to use the same audio player but instead, update where the sound file is located. I only want one sound playing at a time so multiple sounds is obviously not an issue. Here is my code:
- (IBAction)play {
if (audioPlayer.playing == NO) {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/k.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
//I added the next two lines to allow me to play AVAudio with MPiPodPlayer (which I found on this site, too) simultaneously.
AVAudioSession *audiosession = [AVAudioSession sharedInstance];
[audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audioPlayer play];
[start setTitle:#"Stop" forState:UIControlStateNormal];
}
else
if (audioPlayer.playing == YES) {
[audioPlayer stop];
[start setTitle:#"Start" forState:UIControlStateNormal];
}
}
- (IBAction)play2 {
if (audioPlayer.playing == NO) {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/chicken.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
AVAudioSession *audiosession = [AVAudioSession sharedInstance];
[audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audioPlayer play];
[start2 setTitle:#"Stop" forState:UIControlStateNormal];
clicked = 1;
clicked2 = 0;
} else
if (audioPlayer.playing == YES) {
[audioPlayer stop];
[start2 setTitle:#"Start" forState:UIControlStateNormal];
clicked = 0;
}
}
My IBActions are linked to UIButtons and the 'start's are UIButton references. Whenever I click on 'start' it plays 'k.mp3' fine, however if I then click on 'start2' AFTER it starts to play the 'k.mp3' file, and not the chicken file. Whichever button I click on first is what the url is set to. This is my first iPhone OS application project so I realize there are probably some embarrassing coding mistakes in there (feel free to correct me). I'd like an answer that would be applicable to multiple buttons, even for some 30 buttons so I do not have to copy and paste stop audio player 1, 2, and 3 for each button.
To summarize: I have multiple buttons that play one sound each, I would like no more than one sound playing at a time; when I click on a button it plays 1 sound and all other audio players stop. I would prefer having only one AVAudioPlayer instance for simplicity. Thanks in advance!
Check if application control goes into - (IBAction)play2
And if control goes in it and it is still not working then you might try [audioPlayer release] before
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
that might do the trick for you
Tried inserting my code but this site rejected it. The code was legit for using a UIButton but I can't figure out how to exclude the UIButton from the code and still get it to work right. Any suggestions?
By the way I am using AVFoundation
I try copying the code straight over from xcode and it tells me it is not indented by four spaces so really really irritating because I have code.
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)motionBegan:(UIEventSubtype)motion WithEvent:(UIEvent *)event;
{
NSLog(#"Device started shaking!");
clicked = 1;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/FightSong.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
}
The clicked refers to the integer of a UIButton that the motion is setting off to start the audio file but I want to get rid of the UIButton and still have the shake gesture initiate the audio file.