简体   繁体   English

需要帮助使用AVAudioPlayer iOS从URL流音频

[英]Need help streaming Audio from URL using AVAudioPlayer iOS

I am using twilio to create a phone app. 我正在使用twilio创建电话应用程序。 I have a link to a voicemail and I am trying to play the voicemail from a control in a tableview. 我有一个语音邮件的链接,我正在尝试从表视图的控件中播放语音邮件。 Since I have never streamed audio before I decided to simplify everything and just try to stream audio from any URL. 由于我从没有流过音频,因此我决定简化所有工作,并尝试从任何URL流式传输音频。 The URL I am trying to stream audio from is " http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8 ". 我尝试从中流式传输音频的URL是“ http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8 ”。

I have tried using examples from Apples Developer page but I guess I'm just not understanding the documentation. 我尝试使用Apples开发人员页面上的示例,但我想我只是不了解文档。

This is what I am currently trying and I get an error. 这是我目前正在尝试的操作,但出现错误。

NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
NSError *error;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

And I get This Error... 我得到这个错误...

Error Domain=NSOSStatusErrorDomain Code=2003334207 "The operation couldn’t be completed. (OSStatus error 2003334207.)"

Am I going in the right direction or should I be doing something differently. 我是朝着正确的方向前进还是应该做一些不同的事情。 I've found a number of different threads but none of what I see gets me to play audio. 我发现了许多不同的线程,但看不到让我播放音频的任何东西。 A Push in the right direction would be greatly appreciated. 向正确方向的推动将不胜感激。

I have also tried using AVPlayer... 我也尝试过使用AVPlayer ...

NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
self.player = [[AVPlayer alloc] initWithURL:url];
[self.player play];

and nothing happens 并没有任何反应

Try this 尝试这个

@property (nonatomic,strong) AVPlayer *audioStremarPlayer;

NSURL *url = [NSURL URLWithString:@"your url"];

// stremar player
AVPlayer *player = [[AVPlayer alloc]initWithURL:url];
self.audioStremarPlayer= player;
[self.audioStremarPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{

    if (object == self.audioStremarPlayer && [keyPath isEqualToString:@"status"]) {
        if (self.audioStremarPlayer.status == AVPlayerStatusFailed)
        {
      //  //NSLog(@"AVPlayer Failed");
        } 
        else if (self.audioStremarPlayer.status == AVPlayerStatusReadyToPlay) 
        {
            [self.audioStremarPlayer play];
        } 
        else if (self.audioStremarPlayer.status == AVPlayerItemStatusUnknown) 
        {
      //  //NSLog(@"AVPlayer Unknown");

        }
    }
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM