Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're setting <code>songIsCurrentlyPaused</code> to <code>NO</code> at the beginning of <code>play:</code></p> <p>Try commenting it out:</p> <pre><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]; } </code></pre> <p>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:</p> <pre><code>[self.background prepareToPlay]; </code></pre> <p>which will preload the song. Also move <code>songIsCurrentlyPaused = NO;</code> to some earlier place in code.</p> <p>EDIT:</p> <p>To get rid of initial delay you should move your initializing code to somewhere like <code>loadView</code> or <code>viweDidLoad</code>.</p> <pre><code> //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]; </code></pre> <p>Now this may cause a lag in UI display so you might wan't to consider preloading data in background thread.</p> <p>Your <code>IBAction</code> methods should then be modified:</p> <pre><code>- (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]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload