【发布时间】:2021-06-15 14:15:13
【问题描述】:
我尝试使用来自 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);
}];
}
【问题讨论】:
-
currentTime 函数返回 void 但有一个完成块将给出值,正如您从您共享的 Objective-c 代码(来自 Youtube 助手)中看到的那样。
-
@ChanpreetSingh 你能详细说明一下吗?你的意思是 '- (void)currentTime:(_Nullable YTFloatCompletionHandler)completionHandler {...}' 这部分?由于我不熟悉objective-c 代码,所以我不太了解YouTube 助手中的代码。您能否解释一下如何使用您提到的完成块来获取当前时间?为了获取当前时间,我应该从“playerView.currentTime()”传递一些参数,比如“playerView.currentTime(something)”吗?
标签: swift current-time