Domanda

I tried to build a radio in my app using webView. But on loading the link my app crashes every time.

    - (IBAction)playMusic:(id)sender {
    NSString *stream = @"http://www.bbc.co.uk/radio/listen/live/r2.pls"; //just an example link
    NSURL *url = [NSURL URLWithString:stream];
    NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
    [Webview loadRequest:urlrequest];
}

...but with a page link (like www.google.com) everything works fine. Maybe there is also a better solution than using webView to play radio. Thanks to all of you in advance

È stato utile?

Soluzione

UIWebView is indeed not meant to play audio files. I would recommend you to use MPMoviePlayerController (part of the MediaPlayer framework) for what you're trying to achieve. I wrote some sample code to below to show how you how to get started.
If you need more information, I suggest you to take a look at the HTTP Live Streaming Resources or MPMoviePlayerController Class Reference

NSURL *URL = [NSURL URLWithString:@"http://www.bbc.co.uk/radio/listen/live/r2.pls"];

MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:URL];
[moviePlayerController prepareToPlay];

[[moviePlayerController view] setFrame:CGRectMake(0.f, 0.f, [[self view] frame].size.width, 24.f)];
[[self view] addSubview:[moviePlayerController view]];

[self setMoviePlayerController:moviePlayerController];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top