gpt4 book ai didi

iphone - 当项目开始播放时振动 AVQueuePlayer

转载 作者:行者123 更新时间:2023-11-29 13:12:13 27 4
gpt4 key购买 nike

我有一个 AVQueuePlayer,我在运行时向其中添加了一些项目。

NSArray *queue = [self.queuePlayer items];
if ([self.queuePlayer canInsertItem:sound afterItem:[queue lastObject]]) [self.queuePlayer insertItem:sound afterItem:[queue lastObject]];

初始化 AVQueuePlayer 时没有添加任何项目。我所做的只是一个分配初始化。我还会在他们查看加载时立即发送播放消息,这样一来添加新项目时它会立即开始播放。

我正在尝试让手机在新项目开始播放时立即振动。

我正在查看在线文档,我发现的唯一有趣的事情是当项目播放结束时发送的通知。

AVPlayerItemDidPlayToEndTimeNotification

有什么解决方法吗?

最佳答案

来自AVPlayer Class Reference :

You can observe the status of a player using key-value observing.

一些额外的细节:

So that you can add and remove observers safely, AVPlayer serializes notifications of changes that occur dynamically during playback on a dispatch queue. By default, this queue is the main queue (see dispatch_get_main_queue). To ensure safe access to a player’s nonatomic properties while dynamic changes in playback state may be reported, you must serialize access with the receiver’s notification queue. In the common case, such serialization is naturally achieved by invoking AVPlayer’s various methods on the main thread or queue.

这是一个简单的例子。创建播放器时,注册以接收状态更改通知:

[self.queuePlayer addObserver:self forKeyPath:@"status" options:kNilOptions context:nil];   

然后,添加这个方法,当 queuePlayerstatus 改变时调用该方法:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if (object == self.queuePlayer && [keyPath isEqualToString:@"status"]) {
if (self.queuePlayer.status == AVPlayerStatusReadyToPlay) {
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}
}
}

(我不确定您想要的确切振动行为,但希望这能为您指明正确的方向。)

关于iphone - 当项目开始播放时振动 AVQueuePlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16969275/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com