问题
AVPlayer on iOS is perfectly playing radio when doing this
AVPlayer *player;
player =[[AVPlayer alloc] initWithURL:[NSURL URLWithString:@"http://kantipur-stream.softnep.com:7248"]];
[player play]
But it doesn't play any radio when I use NSString variable called link to hold url string of different radios like
AVPlayer *player;
player =[[AVPlayer alloc] initWithURL:[NSURL URLWithString:link]];
[player play]
link is fetched and parsed from an XML document. I can't understand whats the problem.
回答1:
try this code....its working good for me...
NSString *string=@"http://kantipur-stream.softnep.com:7248";
moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:string]];
[moviePlayer.moviePlayer prepareToPlay];
moviePlayer.view.hidden = YES;
[moviePlayer.moviePlayer play];
[self.view addSubview:moviePlayer.view];
and don't forget to add a media playerFramework
or url encoding
-(NSString *)urlencode:(NSString *)str
{
NSString *encodeString=(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)str, NULL, (CFStringRef)@"", kCFStringEncodingUTF8));
return encodeString;
}
and your code
NSString *string=@"http://kantipur-stream.softnep.com:7248";
player =[[AVPlayer alloc] initWithURL:[NSURL URLWithString:[self urlencode:string]]];
[player play];
来源:https://stackoverflow.com/questions/23908870/avplayer-not-playing-streaming-radio-with-dynamic-url