Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @interface ViewController ()
- @property (strong, nonatomic) AVPlayer *player;
- @property (assign, nonatomic) NSInteger index;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- - (IBAction)buttonPressed:(UIButton *)sender {
- [self.player.currentItem removeObserver:self forKeyPath:@"status"];
- NSLog(@"removed %@", self.player.currentItem);
- AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:self.urls[self.index]]];
- [item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
- NSLog(@"added %@", item);
- [self.player replaceCurrentItemWithPlayerItem:item];
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
- if ([keyPath isEqualToString:@"status"]) {
- if ([object isMemberOfClass:[AVPlayerItem class]]) {
- AVPlayerItem *playerItem = (AVPlayerItem *)object;
- switch (playerItem.status) {
- case AVPlayerItemStatusFailed:
- NSLog(@"failed");
- break;
- case AVPlayerItemStatusReadyToPlay:
- NSLog(@"ready");
- break;
- case AVPlayerItemStatusUnknown:
- NSLog(@"unknown");
- break;
- }
- }
- }
- }
- - (AVPlayer *)player {
- if (!_player) {
- _player = [[AVPlayer alloc] init];
- }
- return _player;
- }
- - (NSArray *)urls {
- return @[@"https://of92d29bn.qnssl.com/record1.m4a", @"https://of92d29bn.qnssl.com/record2.m4a"];
- }
- - (NSInteger)index {
- if (_index == 0) {
- _index = 1;
- } else {
- _index = 0;
- }
- return _index;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement