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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With