Disclaimer: I’m relatively new to iOS programming, and am trying to teach myself the basics as I go along just for fun… so be gentle 🙂
I’m building an app that plays a series of .mp3 files strung together using AVAudioplayer. So far, that part is working good. IBActions, and IBOutlets hooked up. Click the button, and the audio plays.
My problem is this:
My play button has been added directly to the main.storyboard, set and custom with it’s initial state defined in Interface Builder. When I click the play, I want to change the image of the button to indicate that it’s playing. However, as soon as the audio starts, it almost seems like the view is… paused, or something. The button image stays the same, until the audio done playing. Only then, will the button image update itself — hardly useful.
I’ve also just tried to set the image of of another random UIImageView that’s not attached to this particular button. Same experience. As soon as I comment out my playAudioArray method, the button swaps work immediately.
-(IBAction)start_playing:(UIButton*)pressed_button {
[_btn_start setImage:[UIImage imageNamed:@"btn_on-active"] forState:UIControlStateNormal];
[self playAudioArray];
}
I hope I’ve left enough to go on… Any thoughts / comments / suggestions??
Thanks a million
– Drew
#
Update:
I’ve found that updating the view breaks down when I have my audio player in a lopp. This code works fine…
if (audioPlayer.playing == YES) {[audioPlayer pause]; [sender setImage:[UIImage imageNamed:@"btn_on.png"] forState:UIControlStateNormal]; [_btn_stop setImage:[UIImage imageNamed:@"btn_off.png"] forState:UIControlStateNormal]; } else { NSString *path = [[NSBundle mainBundle] pathForResource:@"audio_file1" ofType:@"mp3"]; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; audioPlayer.volume = 1.0; audioPlayer.numberOfLoops = 0; [audioPlayer setMeteringEnabled: YES]; [audioPlayer play]; //changes the image when the button is pressed [sender setImage:[UIImage imageNamed:@"btn_on.png"] forState:UIControlStateNormal]; [_btn_stop setImage:[UIImage imageNamed:@"btn_off.png"] forState:UIControlStateNormal]; }But this code doesn't below doesn't.
} else {//changes the image when the button is pressed [sender setImage:[UIImage imageNamed:@"btn_on.png"] forState:UIControlStateNormal]; [_btn_stop setImage:[UIImage imageNamed:@"btn_off.png"] forState:UIControlStateNormal]; NSArray *theSoundArray = [NSArray arrayWithObjects:@"audio1",@"audio2",@"audio3",nil]; for (int i = 0; i < totalSoundsInQueue;) { NSString *sound = [theSoundArray objectAtIndex:i]; // Wait until the audio player is not playing anything while(![audioPlayer isPlaying]){ NSString *path = [[NSBundle mainBundle] pathForResource:@"mom" ofType:@"mp3"]; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; audioPlayer.volume = 1.0; audioPlayer.numberOfLoops = 0; [audioPlayer setMeteringEnabled: YES]; [audioPlayer play]; i++; } } }The audio plays fine, loops through all my sounds and no errors occur. Only, the 'on-state' of my button only gets set AFTER the loop is done playing. I've tried this with a number of different types of looping methods. For, for each, while... every time I start to loop through sounds audio playback works great, but the display looses all focus.
Which reminds me. Once I'm in the loop, my "Stop" button looses it's a clickable (tappable) nature. Until the loop has finished running. If I'm not in a loop, the stop button works fine, no problems.
Sorry if I'm making a total newb mistake here... thoughts / comments anyone?
Your
for loopis tying up the main thread, making it your UI unresponsive. Instead of running the for loop continuously, set yourself as the delegate of AVAudioPlayer. Once the audio player is done playing it will send the delegate the message- (void) audioPlayerDidReachEnd;(which you’ll have to implement). Then you can play the next audio file in the queue in that implementation. You really want to set yourself up as the delegate anyway. AVAudioPlayer will send your delegate messages for interruptions (like a call coming in) and decode errors. You want to handle these events gracefully.I think the reason the audio is playing fine is that is has a higher priority and runs on a separate thread. The reason the UI becomes un-responsive is even though animations/UI changes occur on a separate thread, they’re initiated from the main thread. But all UI changes are queued to the end of a run-loop (so optimizations can be made). However, that run loop never ends because your
for loopnever ends until all the audio files are played. The audio files can be played in order because they’re initiated in thatfor loopand run on a separate thread. Makes sense? In general, you really don’t want to tie up the main thread.