AVPlayer 在 NSDate 寻找播放器
AVPlayer seek player at NSDate
我有 hh:mm:ss 格式的文本为“00:01:30”的字符串。
我想把这个时间作为我的视频播放器的搜索时间。意味着在排序中我想从 hh:mm:ss 的字符串值设置 'seekToTime'。我在 NSDate formate 中转换它,但无法使用它来查看时间。请建议。
谢谢。
self.audioPlayer.timeFormat(self.audioPlayer.getCurrentAudioTime()))
func getCurrentAudioTime(){
let dur = CMTimeGetSeconds(audioPlayer.currentTime())
let cmValue = NSTimeInterval(dur)
let newTime = CMTimeMakeWithSeconds(value, 1)
self.audioPlayer.seekToTime(newTime)
}
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;
}
如果将字符串转换为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)
希望对您有所帮助
您可以使用下面的代码将字符串转换为 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;
}
我有 hh:mm:ss 格式的文本为“00:01:30”的字符串。 我想把这个时间作为我的视频播放器的搜索时间。意味着在排序中我想从 hh:mm:ss 的字符串值设置 'seekToTime'。我在 NSDate formate 中转换它,但无法使用它来查看时间。请建议。 谢谢。
self.audioPlayer.timeFormat(self.audioPlayer.getCurrentAudioTime()))
func getCurrentAudioTime(){
let dur = CMTimeGetSeconds(audioPlayer.currentTime())
let cmValue = NSTimeInterval(dur)
let newTime = CMTimeMakeWithSeconds(value, 1)
self.audioPlayer.seekToTime(newTime)
}
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;
}
如果将字符串转换为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)
希望对您有所帮助
您可以使用下面的代码将字符串转换为 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;
}