2015-03-10 22 views
6

Trong ứng dụng của tôi, tôi sử dụng AVPlayer để đọc một số luồng (m3u8 tệp), với giao thức HLS. Tôi cần biết số lần, trong phiên truyền trực tuyến, máy khách chuyển bitrate.Phát hiện khi AvPlayer chuyển đổi tốc độ bit

Giả sử băng thông của khách hàng đang tăng lên. Vì vậy, khách hàng sẽ chuyển sang phân đoạn tốc độ bit cao hơn. Trình phát AVPlayer có thể phát hiện chuyển đổi này không?

Cảm ơn.

Trả lời

9

Gần đây tôi đã gặp sự cố tương tự. Các giải pháp cảm thấy một chút hacky nhưng nó làm việc như xa như tôi thấy. Trước tiên, tôi thiết lập một người quan sát cho thông báo Nhật ký Truy cập mới:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(handleAVPlayerAccess:) 
              name:AVPlayerItemNewAccessLogEntryNotification 
              object:nil]; 

Cuộc gọi này hoạt động. Nó có lẽ có thể được tối ưu hóa nhưng đây là ý tưởng cơ bản:

- (void)handleAVPlayerAccess:(NSNotification *)notif { 
    AVPlayerItemAccessLog *accessLog = [((AVPlayerItem *)notif.object) accessLog]; 
    AVPlayerItemAccessLogEvent *lastEvent = accessLog.events.lastObject; 
    float lastEventNumber = lastEvent.indicatedBitrate; 
    if (lastEventNumber != self.lastBitRate) { 
     //Here is where you can increment a variable to keep track of the number of times you switch your bit rate. 
     NSLog(@"Switch indicatedBitrate from: %f to: %f", self.lastBitRate, lastEventNumber); 
     self.lastBitRate = lastEventNumber; 
    } 
} 

Mỗi lần có một mục mới vào nhật ký truy cập, nó sẽ kiểm tra tốc độ bit chỉ định cuối cùng từ sự xâm nhập gần đây nhất (các lastObject trong nhật ký truy cập cho mục trình phát). Nó so sánh tốc độ bit được chỉ định này với thuộc tính đã lưu tốc độ bit từ thay đổi cuối cùng đó.

+0

Bí quyết! Cảm ơn bạn: D – tcacciatore

4

Giải pháp của BoardProgrammer hoạt động tuyệt vời! Trong trường hợp của tôi, tôi cần bitrate được chỉ định để phát hiện khi chất lượng nội dung chuyển từ SD sang HD. Đây là phiên bản Swift 3.

// Add observer. 
NotificationCenter.default.addObserver(self, 
              selector: #selector(handleAVPlayerAccess), 

              name: NSNotification.Name.AVPlayerItemNewAccessLogEntry, 
              object: nil) 

// Handle notification. 
func handleAVPlayerAccess(notification: Notification) { 

    guard let playerItem = notification.object as? AVPlayerItem, 
     let lastEvent = playerItem.accessLog()?.events.last else { 
     return 
    } 

    let indicatedBitrate = lastEvent.indicatedBitrate 

    // Use bitrate to determine bandwidth decrease or increase. 
} 
Các vấn đề liên quan