I want to set the timeout period for AVPlayer.

Asked 2 years ago, Updated 2 years ago, 64 views

I use Objective-C to create iOS apps.
I want to read MP3 files on the network and play audio. Using AVPlayer and AVPlayerItem classes in AVFoundation,

like AVPlayerItemWithURL:url You are playing audio with a URL on the Internet.

AVFoundation Programming Guide (https://developer.apple.com/jp/documentation/AVFoundationPG.pdf)
Monitor the AVPlayer status key value according to
When AVPlayerStatusReadyToPlay is ready, start playing and
AVPlayerStatusFailed indicates an error.

If the URL is not accessible, the status immediately becomes AVPlayerStatusFailed, but
At timeout, the status value changes after approximately 60-90 seconds and
AVPlayer error is set to "NSURLErrorDomain" with timeout error information.

I would like to set this timeout period to any value.
I am having trouble finding properties to configure such information in AVPlayer-related classes.
Is there any way to set it up?

ios objective-c

2022-09-30 16:35

1 Answers

I looked at the reference again, but I certainly didn't see any properties that specify a timeout.In such a case, I think it's inevitable that you don't implement the timeout process yourself.

If you are writing code according to the AVFoundation programming guide, you may be writing code to monitor status changes in key value monitoring, but add a timeout monitoring action to it.

#define TIMEOUT_SEC(10)
    _player= [AVPlayer playerWithURL:url];
    [_player addObserver:self forKeyPath:@"status" options:0
            context:&PlayerStatusContext];
    _timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:TIMEOUT_SEC target:self]
       selector:@selector(playerTimeout:) userInfo:nil repeat:NO];

The next step is to describe the actions required for the specified timeout operation.

-(void)playerTimeout:(NSTimer*)timer{
    if(_player.status!=AVPlayerStatusReadyToPlay){
        [_player removeObserver:self forKeyPath:@"status" context:&PlayerStatusContext];
        // Cancellation processing
    }
    timer invalidate;
}

If you can play it normally, you also need to validate the timer.

-(void)observeValueForKeyPath:(NSString*)keyPath
 ofObject: (id) object
 change:(NSDictionary*) change context:(void*) context
{
    if(context==&PlayerStatusContext){
        [_timeoutTimer invalidate];
        // Playback Start Processing
    //} else if (...) {
    } else{
        superobserveValueForKeyPath:keyPath ofObject:object change:change context:context;
    }
}

I didn't have time to write the code until I could actually verify it, so I might need some modifications, but I should be able to implement the timeout process roughly this way.


2022-09-30 16:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.