Hi!
my app has this feature: the user can write (in an UITextfield) the numbers from 0 to 9 and, based on what he writes, the iPhone play a different mp3.
i've used AVFoundation framework and this is the code:
So, it works! If the user write "0123" the iphone play 0.mp3, 1.mp3, 2.mp3 and 3.mp3!
the problem is that when the user push the UIButton, this UIButton remains pressed (in blue, do you understand?) until the last mp3 is played!
The problem is that when it is blue, it doesn't become disable (with self.playbutton.enabled = NO), and if the user push again, it starts the IBAction again!
I've tried to write "self.playbutton.hidden = YES" (instead of enable = NO) and the UIButton become hidden only when the last mp3 is played (or, better, only when the switch finish)!
The problem is that the command line is before the switch!
The cause of the problem is that when you call usleep, UIKit does not have a chance to change the state of your button. Your function needs to return before anything in the UI can change.
The best way to do this would be to store the sounds to be played into an NSMutableArray, start the first one, and use the AVAudioPlayerDelegate methods to know when each song has finished. When each one finishes, you would remove it from the array and start the next one. When they are all done, you could re-enable the button.
I don't have any code handy to help with this, but the documentation should be enough to get you started if you want to try this.
The cause of the problem is that when you call usleep, UIKit does not have a chance to change the state of your button. Your function needs to return before anything in the UI can change.
oh! thanks!
it's strange because i've tried one thing like this:
Code:
-(IBAction)play{
playbutton.enable = NO;
[self play2];
}
-(void)play2{
//here the code, then
playbutton.enable = YES;
return;
}
but nothing change!
Quote:
Originally Posted by JasonR
The best way to do this would be to store the sounds to be played into an NSMutableArray, start the first one, and use the AVAudioPlayerDelegate methods to know when each song has finished. When each one finishes, you would remove it from the array and start the next one. When they are all done, you could re-enable the button.
I don't have any code handy to help with this, but the documentation should be enough to get you started if you want to try this.
oh! thanks!
it's strange because i've tried one thing like this:
Code:
-(IBAction)play{
playbutton.enable = NO;
[self play2];
}
-(void)play2{
//here the code, then
playbutton.enable = YES;
return;
}
This code has the same problem. By the time the UI has a chance to update you've already set enable back to YES already. So the UI never sees that you set it to NO.
Using the delegate methods should work much better.
This code has the same problem. By the time the UI has a chance to update you've already set enable back to YES already. So the UI never sees that you set it to NO.
Using the delegate methods should work much better.
Well, you won't be able to do it from inside a for loop. But you could just add the index (I would call it something more descriptive than just "i") to your .h and keep track of it each time an audio sound ends.
now i have only one more question (sorry!!): why the first time that i press the UIButton, it takes a little bit of time to play? and just the first time!
I mean... the uibutton remains pressed for 1/2 seconds, the it starts to play... after that, it works normally.
Do you know why?
Glad to hear you got it working. One thing I've seen is that the simulator seems to add a long delay that does not happen on the device. If it's happening on the device, the thread you linked to looks like it has everything I would try next.