I want to play any file for 6 seconds. Also suppose the audio is bigger then 6 sec the application will play only for 6 sec.and if it is less then 6 sec then play continuously. So is there any inbuilt option from any framework?
A Simple Way To Play MP3 Audio Files
Assuming you use an AVAudioPlayer, send a -stop message after 6 seconds:
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:6
target:thePlayer selector:#selector(stop)
userInfo:nil repeats:NO];
(Remember to invalidate the timer if the player stops early.)
If you are using AVAudioPlayer you can make use of an NSTimer as mentioned by #Kenny. Just to add to add to that answer.
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:6
target:thePlayer selector:#selector(stopPlayer)
userInfo:nil repeats:NO];
- (void)stopPlayer
{
[audioPlayer stop];
}
In case the duration of your audio file is less than 6 seconds or your desired limit you should continue to play right? so you must set the numberOfLoops for the audio player instance to continue playback. Here is the reference
Related
I'm using an NSTimer to play an audio file every x number of seconds. The audio files are in sync with a constantly running process, so the interval of the NSTimer is the shortest it can be (to my knowledge):
self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(updateTimerFired:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.updateTimer forMode:NSRunLoopCommonModes];
My problem occurs during the transition to the background state e.g. when the user clicks the home button. It seems that a latency occurs with the timer, and the "updateTimerFired:" method isn't called as reliably during the transition. This results in the audio files playing out of sync, although they eventually sync back up after the transition is complete. Note that my app incorporates the Audio background mode.
Is there a more reliable tool other than NSTimer to ensure that the latency does not occur when transitioning to the background?
Ended up replacing the NSTimer with dispatch_source_create:
self.timerQueue = dispatch_queue_create("updateTimerQueue", nil);
self.updateTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.timerQueue);
dispatch_source_set_timer(self.updateTimer, DISPATCH_TIME_NOW, (1 * NSEC_PER_SEC) / 50, (1 * NSEC_PER_SEC) / 20);
dispatch_source_set_event_handler(self.updateTimer, ^{
[self updateTimerFired];
});
dispatch_resume(self.updateTimer);
This provides a dedicated queue for the timer to perform on, and is not interrupted by a transition to the background.
I want to play a specified duration within a sound file on IOS. I found a method in AVAudioPlayer that seeks to the begining of the playing (playAtTime:) but i cannot find a direct way to specify an end time before the end of the sound file.
Is there is a way to achieve this?
If you don't need much precision and you want to stick with AVAudioPlayer, this is one option:
- (void)playAtTime:(NSTimeInterval)time withDuration:(NSTimeInterval)duration {
NSTimeInterval shortStartDelay = 0.01;
NSTimeInterval now = player.deviceCurrentTime;
[self.audioPlayer playAtTime:now + shortStartDelay];
self.stopTimer = [NSTimer scheduledTimerWithTimeInterval:shortStartDelay + duration
target:self
selector:#selector(stopPlaying:)
userInfo:nil
repeats:NO];
}
- (void)stopPlaying:(NSTimer *)theTimer {
[self.audioPlayer pause];
}
Bear in mind that stopTimer will fire on the thread's run loop, so there will be some variability in how long the audio plays, depending on what else the app is doing at the time. If you need a higher level of precision, consider using AVPlayer instead of AVAudioPlayer. AVPlayer plays AVPlayerItem objects, which let you specify a forwardPlaybackEndTime.
when iPhone has a incoming call , its vibration duration seems longer than invoke the method below:
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
how could i make the vibration duration as long as the iPhone incoming call .
I make a VoIP app, I want the single time vibration duration not too short...
There's no method to achieve a longer vibration. But you could repeat the vibration permanently or with a timer and a little space between the vibrations.
Example of a timer based solution:
//start the vibration
NSTimer * vibrationTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(vibrate) userInfo:nil repeats:YES];
//stop the vibration
[vibrationTimer invalidate];
-(void)vibrate{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
In Xcode, I have set my buttons to play a music clip which will last 40 seconds. My question is how do I link up a UIProgressView to the music playing? For example, if the song is half way through, the progress bar will display that.
If you have the following defined in your class:
AVAudioPlayer *audioPlayer;
UIProgressView *progressView;
NSTimer *audioTimer;
Running it off a timer seems to work:
- (void)audioProgressUpdate
{
if (audioPlayer != nil && audioPlayer.duration > 0.0)
[progressView setProgress:(audioPlayer.currentTime / audioPlayer.duration)];
}
When you start the clip start the timer (this runs it every tenth of a second):
[audioPlayer play];
audioTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(audioProgressUpdate) userInfo:nil repeats:YES];
And when you stop the clip, stop the timer:
[audioTimer invalidate];
[audioPlayer stop];
I am using the AVPlayer to play a live stream from the Internet, and need to show than the player is buffering :
I am using an NStimer :
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(buffering) userInfo:nil repeats:YES];
-(void)buffering {
if(radiosound.rate == 1.0)
[activityIndicator stopAnimating];
else
[activityIndicator startAnimating];
}
For sure rate property is not working properly to show !
Is it an other statement to know if the AVPlayer is buffering ?
You need to look at setting up key-value observers for the loaded times, and use that to figure out if you are waiting for data at the current play point.
Observers are setup using addObserver:self forKeyPath: options: context: method on AVPlayerItem and then in the observeValueForKeyPath: ofObject: change: context: callback you can figure out what times have been loaded compared with where in the item the player is playing.
You won't see the rate variable drop to zero when buffering, as this is the desired playback rate, not the actual rate being achieved.
Use the NSURLConnection class in conjunction with the NSURLConnectionDataDelegate protocol.