简体   繁体   English

iPhone:在特定时间播放和暂停歌曲?

[英]iphone : Play and pause a song for particular time?

In my app, 在我的应用中

song play through AVAudioPlayer, 通过AVAudioPlayer播放歌曲,

now i want to play a song for particular time than pause for particular time & play again. 现在我想在特定时间播放一首歌,而不是在特定时间暂停并再次播放。

How can i do that? 我怎样才能做到这一点?

If you use AVPlayer instead of AVAudioPlayer then you can use the method 如果使用AVPlayer而不是AVAudioPlayer则可以使用该方法

- (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. 传递给块的Time参数将告诉您当前项目已播放了多长时间。

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. 因此,您所需要做的就是测试“ Time值,如果它是某个长度,请致电“ pause以停止播放器。 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. 您需要使用NSTimer并在给定的时间段后调用一个方法。 So, make two methods, pause: and play:, for example 因此,例如,制作两个方法,pause:和play:

- (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'. 因此,在这种情况下,三秒钟后,暂停将在“自我”中调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM