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 7657075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:55:53+00:00 2026-05-31T12:55:53+00:00

In my project, I have an AVAudioPlayer which plays a song. After I pause

  • 0

In my project, I have an AVAudioPlayer which plays a song. After I pause it, and hit play again, it starts over instead of playing where I paused. Why is that happening?
Also, when I press play for the first time, it loads the song about 3-4 seconds. I thought I solved it by loading it on the other thread, but it still loads too slow (at least the view doesn’t freeze anymore). How can I fix that? Here’s the code:

- (IBAction)play:(id)sender {
  songIsCurrentlyPaused = NO;
  if(songIsCurrentlyPaused==YES){
    [self.background play];
  } else {
    playQueue = dispatch_queue_create("volume_change", NULL);
    dispatch_async(playQueue, ^{ NSString *filePath = 
      [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"];
      NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
      self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
      self.background.delegate = self;
      [self.background setNumberOfLoops:1];
      [self.background setVolume:0.5];
      [self.background play]; });

      [trackNameLabel setText:@"Currently playing :\n some_song"];
      self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
    }
}

- (IBAction)pause:(id)sender {
  songIsCurrentlyPaused = YES;
  [self.background pause];
  [trackNameLabel setText:@"Currently playing : some_song (paused)"];
  [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES];
}

Thanks!

  • 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-05-31T12:55:55+00:00Added an answer on May 31, 2026 at 12:55 pm

    You’re setting songIsCurrentlyPaused to NO at the beginning of play:

    Try commenting it out:

    - (IBAction)play:(id)sender {
      //songIsCurrentlyPaused = NO;
      if(songIsCurrentlyPaused==YES){
        [self.background play];
      } else {
        playQueue = dispatch_queue_create("volume_change", NULL);
        dispatch_async(playQueue, ^{ NSString *filePath = 
          [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"];
          NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
          self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
          self.background.delegate = self;
          [self.background setNumberOfLoops:1];
          [self.background setVolume:0.5];
          [self.background play]; });
    
          [trackNameLabel setText:@"Currently playing :\n some_song"];
          self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
        }
    }
    
    - (IBAction)pause:(id)sender {
      songIsCurrentlyPaused = YES;
      [self.background pause];
      [trackNameLabel setText:@"Currently playing : some_song (paused)"];
      [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES];
    }
    

    If you want to get rid of that initial pause you’re gonna have to completely reorganize your player setup. Initialize it before user can hit play button and also call:

    [self.background prepareToPlay];
    

    which will preload the song. Also move songIsCurrentlyPaused = NO; to some earlier place in code.

    EDIT:

    To get rid of initial delay you should move your initializing code to somewhere like loadView or viweDidLoad.

       //initialization code
       NSString *filePath = [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"];
       NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
       self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
       self.background.delegate = self;
       [self.background setNumberOfLoops:1];
       [self.background setVolume:0.5];
       [self.background prepareToPlay];
    

    Now this may cause a lag in UI display so you might wan’t to consider preloading data
    in background thread.

    Your IBAction methods should then be modified:

    - (IBAction)play:(id)sender
    {
    
       if (songIsCurrentlyPaused==YES) 
       { 
          [self.background play];
       }
       else
       {
          playQueue = dispatch_queue_create("volume_change", NULL);
          dispatch_async(playQueue, ^{
             [self.background setCurrentTime: 0.0];
             [self.background play];
          });
    
          [self.progressBar setProgress:0.0 animated:YES];
          [trackNameLabel setText:@"Currently playing :\n some_song"];
          self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
    
      }
      songIsCurrentlyPaused = NO;
    }
    
    - (IBAction)pause:(id)sender
    {
      songIsCurrentlyPaused = YES;
      [self.background pause];
      [trackNameLabel setText:@"Currently playing : some_song (paused)"];
      [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project structure which is like Parent |-- child Project1 (Making war
My Project have two Screen in which Activity A and Activity B. Activity A
in my project I have multiple recordings taken from the iPhones microphone which I
I want my AVAudioPlayer to play some mp3 files. It plays some of them
I have an xcode project which has 2 classes - Stem & Player, I'm
In my project i have a function that play music -(void)playPlin { AudioSessionSetActive(true); //
I'm writing a tasklist and have Project object, which holds all the tasks (and
My project have no visible error but when i try to run it gives
I want to apply NTFS-Search to our project. Our project have to find the
I have project asp.net with namespace test and I'm using resources (files Resource.resx and

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.