我有一個播放流內容的音頻應用程序。問題有時候,當信號很弱時,它會停止播放。網絡仍然可以訪問,但緩衝區似乎已經空了。AVPlayer observeValueForKeyPath
我試圖實現一個觀察者來監視玩家的狀態變化,但它不工作,該方法永遠不會被調用。
作爲一個特殊性,AVPlayer實例在AppDelegate中,因爲我有多個視圖,並且玩家必須繼續玩任何顯示的視圖。所以這裏是一段示例代碼:
- (void)viewDidLoad
{
[super viewDidLoad];
isPlaying = false;
playButton.enabled = NO;
//Add en observer for reachability change
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
//Adding the observer for player's status change
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.player addObserver:self forKeyPath:@"status" options:0 context:playerStatusContext];
[self initPlayer];
}
//Event raised whenever the current status of the player change
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UIAlertView *alert = [UIAlertView alloc];
NSString *chaineConcat = @"Entering observer method..."];
[alert initWithTitle:@"Test" message:chaineConcat delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
if (context == playerStatusContext) {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *statusPlayer = nil;
if (object == delegate.player && [keyPath isEqualToString:@"status"]) {
if (delegate.player.status == AVPlayerStatusReadyToPlay) {
statusPlayer = @"Everything's OK";
} else if (delegate.player.status == AVPlayerStatusFailed) {
statusPlayer = @"Houston, we have a problem";
}
}
[self syncUI];
UIAlertView *alert = [UIAlertView alloc];
NSString *chaineConcat = [NSString stringWithFormat:@"%@/%@/", @"Player status' is ", statusPlayer];
[alert initWithTitle:@"Test" message:chaineConcat delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
return;
}
- (void) initPlayer {
// Load the array with the sample file
NSString *urlAddress = @"http://MYSTREAMURL";
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//Create a URL object.
delegate.urlStream = [NSURL URLWithString:urlAddress];
//Reinit AVPlayer
delegate.player = [AVPlayer playerWithURL:delegate.urlStream];
}
有沒有人有關於爲什麼不提高事件的想法?我在方法中有2個警報,但沒有人被解僱,這意味着它沒有進入方法。所有這些的目標將是嘗試實現一種讓玩家在這種情況發生時重新啓動的方式。
謝謝!