如何使用AVPlayer addperiodictimeobserverforinterval函数更新uilabel?

时间:2016-11-08 03:17:34

标签: ios swift uilabel avplayer

我正在尝试使用AVPlayer剩余时间更新UILabel。我可以在视频开始时立即将视频的当前时间设置为UILabel,但随着视频的进展,该数字不会改变。我搜索了Apple的文档,发现this block观察了视频的时间,但Stackoverflow上的这个资源和其他问题并不一定指出如何使用此函数更新UILabel。

这是我编写的代码,但我不确定如何继续前进:

SmtpClient smtpClient = new SmtpClient("mail.domain.com", 587);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(From, "password");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Port = 587;
MailMessage message = new MailMessage();

try
{
    MailAddress fromAddress = new MailAddress(From, "Me");

    smtpClient.Host = "mail.domain.com";

    message.From = fromAddress;
    message.To.Add(To);
    message.Subject = Subject;
    message.IsBodyHtml = true;
    message.Body = Body;

    smtpClient.Send(message);
    Label_Results.Text = "Email successfully sent.";
}
catch (Exception ex)
{
    ErrorLabel.Text = "<p>Send Email Failed:<p>" + ex.Message;
}

2 个答案:

答案 0 :(得分:1)

如果您有评论// update player transport UI,则需要重新计算timeRemainingInVideo并更新标签。

答案 1 :(得分:0)

更新UI应该在主线程中

videoViewController.player?.addPeriodicTimeObserver(forInterval: interval, queue: nil, using: { (time) in
        let currentTime = floor(CMTimeGetSeconds(time))
        print(currentTime)
        // update UI should be in main thread
        DispatchQueue.main.async {
            videoTimeRemainingLabel.text = String(currentTime)
        }
    })