How to perform operations when playing sound on iPhone?

I play MP3 in my iPhone application using AVAudioPlayer; I need to perform some operations at a specific time (for example, 30 seconds, 1 minute); is there any way to call callback functions based on mp3 play time?

+5
source share
6 answers

I believe that the best solution is to start NSTimerwhen the game starts AVAudioPlayer. You can set the timer to fire every half second or so. Then each time your timer fires, look at the property currentTimeon your audio player.

To do something at regular intervals, I would suggest that you save the instance variable for the playback time from the last call to the timer call. Then, if you have passed a critical point between the last callback and this, do your action.

So, in the pseudo code, the timer callback:

  • Get currentTimeyour AVAudioPlayer
  • Check if currentTime criticalPoint
  • If yes, check if less lastCurrentTime criticalPoint
  • If so, do your thing.
  • Set lastCurrentTimetocurrentTime
+8
source

If you can use AVPlayer instead of AVAudioPlayer, you can set boundary or periodic time observers:

// File URL or URL of a media library item
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];        

CMTime time = CMTimeMakeWithSeconds(30.0, 600);
NSArray *times = [NSArray arrayWithObject:[NSValue valueWithCMTime:time]];

id playerObserver = [player addBoundaryTimeObserverForTimes:times queue:NULL usingBlock:^{
    NSLog(@"Playback time is 30 seconds");            
}];

[player play];

// remove the observer when you're done with the player:
[player removeTimeObserver:playerObserver];

AVPlayer Documentation: http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVPlayer_Class/Reference/Reference.html

+5

, , , , , .

, currentTime - , . , currentTime - , , .

, .

, :

if (avAudioPlayerObject.currentTime == 30.0) //You may need a more broad check. Double may not be able to exactly represent 30.0s
{
    //Do Something
}
+1

, ... , , , ..

1- you play your mp3 file.

2- [self performSelector:@selector(Operation:) withObject:Object afterDelay:30];

-(void)Operation:(id)sender;

; 30 mp3 .. , .

3-

[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(CheckTime:) userInfo:nil repeats:YES];

Check Time

-(void)CheckTime:(id)sender{
   if (avAudioPlayerObject.currentTime == 30.0)
   {
       //Do Something
       //Fire and function call such
       [self performSelector:@selector(Operation:) withObject:Object]
   }
}

, , - , 5 . , ..

+1

, :

1 :  in your main thread create a variable for storing time passed 
2 :  create new thread like "checkthread" that check each 30-20 sec(as you need)
3 :  if the time passed is what you want do the callback  
+1

, 30 , :

1) , ,

2), :

-(IBAction)play_sound
{

    BackgroundPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[Arr_tone_selected objectAtIndex:j]ofType:@"mp3"]]error:NULL];
    BackgroundPlayer.delegate=self;

    [BackgroundPlayer play];


}   


- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [BackgroundPlayer stop];
    j++;
    [self performSelector:@selector(play_sound) withObject:Object afterDelay:30];   
}
+1

All Articles