【问题标题】:iOS: What is the proper way to implement live audio streaming?iOS:实现实时音频流的正确方法是什么?
【发布时间】:2015-05-25 12:16:07
【问题描述】:
我正在为新闻网站开发 iOS 应用程序,该网站有 a live audio news channel,我尝试使用 AVAudioPlayer 并执行以下操作:
.h 文件:
@interface ViewController : UIViewController <AVAudioPlayerDelegate>
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
.m 文件(viewDidLoad):
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:@"http://198.178.123.23:8662/stream/1/;listen.mp3"];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
_audioPlayer.delegate = self;
[_audioPlayer prepareToPlay];
}
}
但应用程序给了我以下错误:
2015-05-25 15:03:12.353 AudioPlayer[2043:421434] audioPlayer 出错:操作无法完成。 (OSStatus 错误 2003334207。)
我不知道这段代码有什么问题。
谁能告诉我代码中的错误在哪里?
非常感谢。
【问题讨论】:
标签:
ios
objective-c
avaudioplayer
live-streaming
【解决方案1】:
我认为有一种更好的方法可以使用音频 url 在默认 MPMoviePlayer 中播放音频链接。其余的将由默认播放器管理。
编辑:
NSURL *audioPath = [NSURL URLWithString:@"Your Audio URL"];
MPMoviePlayerViewController *mpViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:audioPath];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:mpViewController
name:MPMoviePlayerPlaybackDidFinishNotification
object:mpViewController.moviePlayer];
// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mpViewController.moviePlayer];
// Set the modal transition style of your choice
mpViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
// Present the movie player view controller
[self presentViewController:mpViewController animated:YES completion:nil];
// Start playback
[mpViewController.moviePlayer prepareToPlay];
[mpViewController.moviePlayer play];
【解决方案2】:
您收到错误是因为AVAudioPlayer 不支持 HTTP(这意味着您无法播放来自 Internet 的媒体),所以您可以使用 AVPlayer 它非常相似,这是一个示例:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = @"http://198.178.123.23:8662/stream/1/;listen.mp3";
urlStream = [NSURL URLWithString:urlAddress];
self.audioPlayer = [AVPlayer playerWithURL:urlStream];
NSError *error;
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
[_audioPlayer prepareForInterfaceBuilder];
}
}
希望我的回答对你有帮助:)