开发者

iphone : Play and pause a song for particular time?

In my app,

song play through AVAudioPlayer,

now i want to play a song for particular time than pause for pa开发者_StackOverflow中文版rticular time & play again.

How can i do that?


If you use AVPlayer instead of AVAudioPlayer then you can use the method

- (id)addPeriodicTimeObserverForInterval:(CMTime)interval queue:(dispatch_queue_t)queue usingBlock:(void (^)(CMTime time))block

Which will call the block at the set interval for as long as the item is playing. The parameter Time passed to the block will tell you how long the current item has been playing for.

Therefore all you'd need to do would be to test the Time value and if it's a certain length then call pause to stop the player. You could then setup a one-time timer to fire after a certain amount of time to recommence playback.


You need to use an NSTimer and invoke a method after a given period of time. So, make two methods, pause: and play:, for example

- (void)pause:(id)sender {
    [player pause];
}

and then invoke this method using a scheduled time after a given time period like this:

[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(pause:) userInfo:nil repeats:no

so in this case, after 3 seconds, pause will be invoked in 'self'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜