简体   繁体   中英

iOS AVPlayer Play/Pause button issue

I am using AVPlayer to play a live stream from the Internet using the following code :

    NSString *u = @"http://192.192.192.192:8036";
NSURL *url = [NSURL URLWithString:u];
radiosound = [[AVPlayer alloc] initWithURL:url];
    [radiosound play]; 

And I have one button play :

[radiosound play]; 

and Pause :

[radiosound pause]; 

My issue is that I want to use only one button Play/Pause, but when I am using this code

if (radiosound.isPlaying) {         
    [radiosound pause]; 
} else {                
     [radiosound play]; 
}

My app crashes, because AVPlayer doesn´t recognize "isPlaying".

Any tips?

AVPlayer doesn't have an isPlaying property. Use the rate property (0.0 means stopped, 1.0 playing).

if (radiosound.rate == 1.0) {         
    [radiosound pause]; 
} else {                
     [radiosound play]; 
}

You can look at the AVPlayer class reference here .

After some research I found out that when there is no network connection, AVPlayer still sets the rate to 1.0 after receiving a -play message.

Thus, I also check for the currentItem and modified my method like that:

-(BOOL)isPlaying
{
    if (self.player.currentItem && self.player.rate != 0)
    {
        return YES;
    }
    return NO;
}

Please share your opinion if you think something is wrong with this approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM