簡體   English   中英

如何在AVPlayer上顯示緩沖區進度?

[英]How to show buffer progress on AVPlayer?

我有一個音樂播放器應用程序,允許用戶從服務器流式傳輸歌曲。 我已經能夠使用AVPlayer從網址流式傳輸歌曲了。 我希望實現如下所示:

在此輸入圖像描述

我試圖從這個轉換頂部答案如何獲取文件大小和當前文件大小從NSURL為AVPlayer iOS4.0發布到swift,但我找不到“kLoadedTimeRanges”。

這是我的代碼:

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {

    if object as? NSObject == songPlayer && keyPath == "status" {

        if songPlayer?.status == AVPlayerStatus.Failed {

            println("AVPlayerStatusReadyToFailed")

        }else if songPlayer?.status == AVPlayerStatus.ReadyToPlay {

            println("AVPlayerStatusReadyToPlay")
            self.songPlayer!.play()

        }else if songPlayer?.status == AVPlayerStatus.Unknown {

            println("AVPlayerStatusReadyToUnknown")

        }

    }
}

@IBAction func playAction(sender: UIButton) {

    var songUrl = "http://a575.phobos.apple.com/us/r1000/119/Music/v4/67/9d/b0/679db0ba-a7f4-35ea-3bf1-e817e7bcf11f/mzaf_3227008615944938675.aac.m4a"
    var songPlayerItem = self.songPlayer?.currentItem
    self.songPlayer = AVPlayer(URL: NSURL(string: songUrl))
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.songPlayer?.currentItem)

    self.songPlayer?.addObserver(self, forKeyPath: "status", options: nil, context: nil)
    //NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateProgress:", userInfo: nil, repeats: true)

}

我會感激任何建議,無論使用客觀c還是swift

目標C:注意:我制作了一個自定義UISlider( 緩沖區滑塊 ),我在其中添加了一個UIProgressView來顯示緩沖區進度。 下面代碼中顯示的setBufferValue方法是一個自定義方法,它將設置UIProgressView值。

static void *PlayerViewControllerLoadedTimeRangesObservationContext = &PlayerViewControllerLoadedTimeRangesObservationContext;

[self.playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:PlayerViewControllerLoadedTimeRangesObservationContext];

- (void)observeValueForKeyPath:(NSString*) path
                      ofObject:(id)object
                        change:(NSDictionary*)change
                       context:(void*)context
{
    if (context == PlayerViewControllerLoadedTimeRangesObservationContext) {
        NSArray *timeRanges = (NSArray*)[change objectForKey:NSKeyValueChangeNewKey];

        if (timeRanges && [timeRanges count]) {
            CMTimeRange timerange=[[timeRanges objectAtIndex:0]CMTimeRangeValue];
            float bufferTimeValue=CMTimeGetSeconds(timerange.duration)/CMTimeGetSeconds(_player.currentItem.duration);
            [self.playerSlider setBufferValue:bufferTimeValue>.95?1:bufferTimeValue animated:YES];
        }
    }
}

只是用

[avPlayerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];

kLoadedTimeRanges可能只是定義為

static NSString *kLoadedTimeRanges = @"loadedTimeRanges";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM