开发者

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];
    [radios开发者_Python百科ound 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜