Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS stream audio from authenticated URL

I want to stream audio file from a source that needs OAuth2 authentication, this is my code, but it is not working.

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

        [req setValue:[NSString stringWithFormat:@"Bearer %@", accessToken] forHTTPHeaderField:@"Authorization"];

        player = [AVPlayer playerWithURL:req.URL];
        [player play];

Can you please help?

like image 344
vburojevic Avatar asked Nov 01 '22 15:11

vburojevic


1 Answers

The issue is that you're adding your authentication token as an HTTP header, but then making a request using only the URL (and headers are not included in the URL).

I need to do a similar thing in my app, but it doesn't look like it's possible to interfere with AVPlayer's network request (at least, not with a public API). So now I'm looking at either requesting the file myself using NSURLSession (which means no streaming - gotta wait for the whole file, or else do some complicated processing to hand it off to an audio player in chunks), or else fudge the web service to accept my parameter as a query parameter instead of as an HTTP header.

This question also has some potential work-arounds: Send headers with AVPlayer request in iOS

like image 142
Brian Avatar answered Nov 08 '22 04:11

Brian