AVPlayer在NSDate寻找玩家

时间:2016-05-04 09:57:49

标签: ios objective-c avfoundation avplayer xcode7.3

我的字符串是" 00:01:30"在......的形式中:mm:ss。 我希望这次是我视频播放器的寻找时间。排序方式我想设置' seekToTime'来自字符串值hh:mm:ss。我在NSDate formate中转换它但不能使用它来查看ToTime。请建议。 谢谢。

3 个答案:

答案 0 :(得分:2)

  func timeFormat(value:Double) -> NSString
   {
    let someFloat = Float(value)
    let floatvalue = lroundf(someFloat)
    let floorvalue = Double(floatvalue)

    let minutes = floor(floorvalue / 60)
    let seconds = floorvalue - (minutes * 60)

    let roundsec = Float(seconds)
    let roundmin = Float(minutes)

    let roundedSeconds = lroundf(roundsec)
    let roundedMinutes = lroundf(roundmin)

    var time : NSString!
    time = NSString.init(format:"%d:%02d", roundedMinutes,roundedSeconds)
    return time;
   }

}

(function () {
    'use strict';

    angular
        .module('app.services')
        .factory('db',db);

    db.$inject = ['$db'];

    function db($db) {

        var data = {};              // set up a data object to receive document(s)

        return {
            getDoc: getDoc,
            getList: getList,
            save: save,
            saveBatch: saveBatch
            };


        // get a single document using the id
        function getDoc(id) {
            $db.getDoc(id)
                .then(
                    function onSuccess(doc) {
                        // success so update the view model
                        angular.extend(data,doc);                         // use angular.extend to shallow copy object so that it can be returned in full
                    },
                    function onError() {
                        // failure to get document                             
                    }
                );
            return data;
        }

        // retrieve a group of documents where key is the prefix of the data you want
        function getList(key) {
            $db.getList(key).then(
                function onSuccess(docs) {
                    // success so update the view model details
                    angular.forEach(docs.rows, function (value) {
                        this.push(value.doc);
                    }, data);
                    // now you can sort data or anything else you want to do with it

                },
                function onError() {
                    // no data found
                }
            );
            return data;
        }


        // save a single viewItem
        function save(viewItem) {
            $db.update(viewItem).then(
                function onSuccess() {                   
                    // success so update view model if required
                },
                function onError(e) {
                    console.log(e);         // unable to save
                }    
            );    
        }

        // save an array of viewItems
        function saveBatch(viewItems) {
            $db.updateBatch(viewItems).then(
                function onSuccess() {                   
                    // success so update the view model if required
                },
                function onError(e) {
                    console.log(e);         // unable to save
                }    
            );    
        }

    }
})();

答案 1 :(得分:1)

如果将字符串转换为CMTime格式,则分配搜索时间,代码如下所示,

let fileURL: NSURL = NSURL(string: timeString)!
let asset = AVURLAsset(URL: fileURL, options: nil)
let audioDuration = asset.duration
let newTime:CMTime = CMTimeGetSeconds(audioDuration)
playerVal.seekToTime(newTime)

希望它有用

答案 2 :(得分:0)

您可以使用下面的代码将字符串转换为CMTime,

func seekTime(timeString:String) -> CMTime? {
    let dateFormatter = NSDateFormatter.init()
    dateFormatter.dateFormat = "HH:mm:ss"
    if let startDate = dateFormatter.dateFromString("00:00:00") {
        if let neededDate = dateFormatter.dateFromString(timeString) {
            let interval = neededDate.timeIntervalSinceDate(startDate)
            return CMTime.init(value: CMTimeValue(interval), timescale: 1)
        }
    }
    return nil;
}