我尝试使用来自 swiftUI的 YouTube-ios-player-helper从 YouTube 获取当前时间。有currentTime()
调用当前时间的方法。但是,每当我调用此函数时,它都会返回void
值,当我检查该currentTime()
函数时,它的返回类型实际上是void
. 我认为返回值应该具有当前时间值,而不应该是无效值。我想知道这是错误设计的功能还是其他原因。当我尝试从 YouTube 播放器获取当前时间时,您能告诉我这里有什么问题吗?
下面是我的快速代码
struct YTWrapper : UIViewRepresentable {
@Binding var videoID : String
@Binding var seconds : Float
@State var playerView = YTPlayerView()
func makeUIView(context: Context) -> YTPlayerView {
playerView.load(withVideoId: videoID,
playerVars: ["playsinline": 1])
return playerView
}
func updateUIView(_ uiView: YTPlayerView, context: Context) {
playerView.seek(toSeconds: seconds, allowSeekAhead: true)
@State var temp : Void
NSLog("current Time:")
temp = playerView.currentTime()
print(temp)
NSLog("end")
}
}
下面是结果
2021-06-15 20:05:12.086348+0800 Meerkat[3141:795444] current Time:
()
2021-06-15 20:05:12.086446+0800 Meerkat[3141:795444] end
以下是 YouTube-ios-player-helper 的 currentTime 函数
/**
* Returns the elapsed time in seconds since the video started playing. This
* method corresponds to the JavaScript API defined here:
* https://developers.google.com/youtube/iframe_api_reference#getCurrentTime
*
* @param completionHandler async callback block that contains float number with the result or an error.
*/
- (void)currentTime:(_Nullable YTFloatCompletionHandler)completionHandler;
- (void)currentTime:(_Nullable YTFloatCompletionHandler)completionHandler {
[self evaluateJavaScript:@"player.getCurrentTime();"
completionHandler:^(id _Nullable result, NSError * _Nullable error) {
if (!completionHandler) {
return;
}
if (error) {
completionHandler(-1, error);
return;
}
if (!result || ![result isKindOfClass:[NSNumber class]]) {
completionHandler(0, nil);
return;
}
completionHandler([result floatValue], nil);
}];
}