Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:18:35+00:00 2026-06-03T03:18:35+00:00

Disclaimer: I’m relatively new to iOS programming, and am trying to teach myself the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:18:36+00:00Added an answer on June 3, 2026 at 3:18 am

    Your for loop is 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 loop never ends until all the audio files are played. The audio files can be played in order because they’re initiated in that for loop and run on a separate thread. Makes sense? In general, you really don’t want to tie up the main thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer, new to programming, working my way through C++ Prime Plus 6th ed. I'm
Disclaimer: super new to rails. I'm using Rails 3.2 Anyways, I'm trying to create
Disclaimer: I'm new to unit testing and just getting my feet wet. That being
Disclaimer: I'm new to most of this I just downloaded virtualbox yesterday and installed
Disclaimer: I am new to python and django but have Drupal programming experience. I'm
Disclaimer: I'm not actually trying to make one I'm just curious as to how
Disclaimer: I'm fairly new with Gradle. I'm trying to build my project with Gradle
DISCLAIMER: I'm relatively new to Linux. I debated putting this on SuperUser or ServerFault
Disclaimer: I'm new to the REST school of thought, and I'm trying to wrap
Disclaimer: I'm a new convert to iOS, so please feel free to tell me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.