• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift3根据秒数获取视频时长(转换成00:00:00时间格式)以及将时长转换成秒 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

直接代码了:

 

 /// 秒转换成00:00:00格式
    ///
    /// - Parameter secounds: <#secounds description#>
    /// - Returns: <#return value description#>
    class func getFormatPlayTime(secounds:TimeInterval)->String{
        if secounds.isNaN{
            return "00:00"
        }
        var Min = Int(secounds / 60)
        let Sec = Int(secounds.truncatingRemainder(dividingBy: 60))
        var Hour = 0
        if Min>=60 {
            Hour = Int(Min / 60)
            Min = Min - Hour*60
            return String(format: "%02d:%02d:%02d", Hour, Min, Sec)
        }
        return String(format: "00:%02d:%02d", Min, Sec)
    }
    
    
    /// 根据00:00:00时间格式,转换成秒
    ///
    /// - Parameter str: <#str description#>
    /// - Returns: <#return value description#>
    class func getSecondsFromTimeStr(timeStr:String) -> Int {
        if timeStr.isEmpty {
            return 0
        }
        
        let timeArry = timeStr.replacingOccurrences(of: "", with: ":").components(separatedBy: ":")
        var seconds:Int = 0
        
        if timeArry.count > 0 && isPurnInt(string: timeArry[0]){
            let hh = Int(timeArry[0])
            if hh! > 0 {
                seconds += hh!*60*60
            }
        }
        if timeArry.count > 1 && isPurnInt(string: timeArry[1]){
            let mm = Int(timeArry[1])
            if mm! > 0 {
                seconds += mm!*60
            }
        }
        
        if timeArry.count > 2 && isPurnInt(string: timeArry[2]){
            let ss = Int(timeArry[2])
            if ss! > 0 {
                seconds += ss!
            }
        }
        
        return seconds
        
    }

 

比如:

100s   -> 01:40

01:40  -> 100s 

 

enjoy~


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift-简单封装一个工具类模板发布时间:2022-07-13
下一篇:
【iOS】swift让程序挂起后,能在后台继续运行任务发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap