iOS:实现实时音频流的正确方法是什么?

标签 ios objective-c avaudioplayer live-streaming

我正在为新闻网站开发 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] Error in audioPlayer: The operation couldn’t be completed. (OSStatus error 2003334207.)

我不知道这段代码有什么问题。

谁能告诉我代码中的错误在哪里?

非常感谢。

最佳答案

我认为有一种更好的方法可以在带有音频 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];

关于iOS:实现实时音频流的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30438183/

相关文章:

javascript - FlatList contentContainerStyle -> justifyContent : 'center' causes issues with scrolling

ios - 额外的 settings.plist 不显示在 iOS 的设置包中

iphone - 如何确定为 Web 服务 API 实现的 Http 方法类型

ios - AVAudioPlayer初始化和内存管理

iphone-sdk-3.0 - 使用iPhone SDK以编程方式播放音频流的推荐方法是什么?

ios - 自 iOS 9 以来如何为状态栏样式更改设置动画

iphone - 呈现用户可以随机来回跳转的序列 View Controller 的最佳方式是什么

ios - 如何将标签栏控件设置为针对不同的 Storyboard

objective-c - 此语法错误是什么意思?

ios - 第一次音频启动延迟 - ios Swift