【发布时间】:2017-03-29 12:36:47
【问题描述】:
我有一个 URL:http://listen.streamonomy.com/cooljazzflorida(例如)。这是我在 PLS 文件(由 SHOUTcast 收音机提供)中解析的用于流式音频的 URL。当我从浏览器(Safari 或 Google Chrome)打开它时,此 URL 会重定向到 http://streaming.shoutcast.com/cooljazzflorida?lang=fr-fr,我可以播放该流。
但是,在我的 iOS 应用程序中,使用 UIWebView 或 WKWebView,重定向的 URL 是 http://streaming.shoutcast.com/cooljazzflorida_64?lang=fr-fr(然后是 _64),与浏览器不同,并且出现错误“找不到您需要的文件”。
有什么想法吗?谢谢。
编辑:说明问题的示例代码:
https://github.com/iDevelopper/RedirectURLSample.git
#import "ViewController.h"
@interface ViewController () <UIWebViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
webView.delegate = self;
[self.view addSubview:webView];
NSString *urlString = @"http://listen.streamonomy.com/cooljazzflorida";
/* If you enter this url using Safari browser, the redirected url is:
http://streaming.shoutcast.com/cooljazzflorida?lang=fr-fr
*/
NSURL *urlToPlay = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:urlToPlay];
[webView loadRequest:request];
}
#pragma mark - UIWebView Delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSURL *url = [webView.request mainDocumentURL];
NSLog(@"DID FINISH The Redirected URL is: %@", url);
/*
The redirected url is: (not the same returned by the browser and this url is not found)
http://streaming.shoutcast.com/cooljazzflorida_64?lang=en-us
*/
// Set AVPlayerItem with the redirected URL
// ...
}
@end
【问题讨论】:
-
使用
AVPlayerViewController代替UIWebview流式传输音频/视频 -
我不使用 UIWebView 播放音频,但使用 AVPlayer 并且效果很好。我只是使用 UIWebViewDelegate 方法来知道要播放的重定向 URL 并设置 AVPlayerItem url。
-
那么请用您的代码更新您的问题
-
用示例代码编辑,谢谢!