如何从AVPlayer获取持续时间(不是AVAudioPlayer)?

slo*_*n21 38 iphone streaming xcode objective-c

我想为我的AVPlayer制作一个UISlider(擦洗器).但由于这不是AVAudioPlayer,因此它没有内置持续时间.有关如何创建滑块以快进,快退和播放进度的任何建议?

我在AVPlayer上阅读了doc,它有一个内置的seekToTime或seekToTime:toleranceBefore:toleranceAfter:.我真的不明白.这是我滑块的答案吗?AVPlayer还有addPeriodicTimeObserverForInterval:queue:usingBlock:,这是用于获取我的音轨的持续时间吗?有人能举例说明如何实现这段代码吗?我不是Apple的文档的粉丝.这似乎很难理解.

slo*_*n21 128

self.player.currentItem.asset.duration
Run Code Online (Sandbox Code Playgroud)

得到它了!

  • CMTimeGetSeconds(aboveCode)为浮点值:) (17认同)
  • 值是 0.00,直到加载视频资产 (3认同)

neo*_*eye 36

#import <AVFoundation/AVPlayer.h>
#import <AVFoundation/AVPlayerItem.h>
#import <AVFoundation/AVAsset.h>
Run Code Online (Sandbox Code Playgroud)

CMTime duration = self.player.currentItem.asset.duration;
float seconds = CMTimeGetSeconds(duration);
NSLog(@"duration: %.2f", seconds);
Run Code Online (Sandbox Code Playgroud)

构架

AVFoundation
CoreMedia
Run Code Online (Sandbox Code Playgroud)

  • 有时返回NaN (5认同)

Har*_*kar 14

让Swift在几秒钟内获得持续时间

if let duration = player.currentItem?.asset.duration {
    let seconds = CMTimeGetSeconds(duration)
    print("Seconds :: \(seconds)")
}
Run Code Online (Sandbox Code Playgroud)


Dou*_*las 11

从iOS 4.3开始,您可以使用稍短的:

self.player.currentItem.duration;
Run Code Online (Sandbox Code Playgroud)


onm*_*133 6

来自StitchedStreamPlayer

你应该使用 player.currentItem.duration

- (CMTime)playerItemDuration
{
    AVPlayerItem *thePlayerItem = [player currentItem];
    if (thePlayerItem.status == AVPlayerItemStatusReadyToPlay)
    {        
        /* 
         NOTE:
         Because of the dynamic nature of HTTP Live Streaming Media, the best practice 
         for obtaining the duration of an AVPlayerItem object has changed in iOS 4.3. 
         Prior to iOS 4.3, you would obtain the duration of a player item by fetching 
         the value of the duration property of its associated AVAsset object. However, 
         note that for HTTP Live Streaming Media the duration of a player item during 
         any particular playback session may differ from the duration of its asset. For 
         this reason a new key-value observable duration property has been defined on 
         AVPlayerItem.

         See the AV Foundation Release Notes for iOS 4.3 for more information.
         */     

        return([playerItem duration]);
    }

    return(kCMTimeInvalid);
}
Run Code Online (Sandbox Code Playgroud)