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];
Related
My ProgressView doesn't update the progress.
I Did this by Interface Builder.
My ProgressView is in a tableViewCell.
//Declared in TableViewCell
#property(nonatomic,retain) IBOutlet UIProgressView *progressview;
and also Synthesized in Cell implementation.
then imported in my viewController
//Declared AVAudioPlayer
AVAudioPlayer *player;
in cellForRowAtIndexpath method :
obj.progressview.progress = 0.0;
in didSelectRowAtIndexPath method :
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(methodToUpdateProgress) userInfo:nil repeats:YES];
m_timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats:YES];
For every 1 second my methodToUpdateProgress is Called
-(void)methodToUpdateProgress{
dispatch_async(dispatch_get_main_queue(), ^{
[obj.progressview setProgress:(float)(player.currentTime/player.duration) animated:YES];
NSLog(#"method");
});
}
I have a button with 2 actions Play and Pause.
Those 2 actions works completely fine.
If I click on a Row in tableview, corresponding song plays, timer starts and for every 1 sec it called my methodToUpdateProgress,but my ProgressView doesn't show any progress.
Help me what should I do ?
I have added nstimer in my app.I have set it's repeat property to YES.I am trying to stop timer when i click on button but timer does not stopping in that case.
image_timer_one= [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(ImageTimer:)userInfo:nil repeats:YES];
code to stop timer
[image_timer_one invalidate];
image_timer_one=nil;
But timer does not stop.Please tell how can i stop the timer.
EDIT:
I trying to give cursor animation effect so i am setting a new image when timer repeats here is the code.
-(void)ImageTimer:(NSTimer*)timer{
if(mode_count==0)
{
if(img_time==0)
{
[self.pass_image_1 setImage:[UIImage imageNamed:#"blink_2.png"]];
img_time=1;
}
else if(img_time==1)
{
[self.pass_image_1 setImage:[UIImage imageNamed:#"blink_1.png"]];
img_time=0;
}
}
}
try this and before starting timer stop previous timer action with calling ImageTimer method.
if (image_timer_one != nil && [image_timer_one isValid]) {
[image_timer_one invalidate];
image_timer_one = nil;
}
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);
}
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