ios 使用 AVAudioPlayer 播放来自互联网的音频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3635792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:39:19  来源:igfitidea点击:

play audio from internet using AVAudioPlayer

iosobjective-cstreamingavaudioplayer

提问by Bangdel

I am implementing AVAudioPlayer to play audio and it works perfectly well while playing files locally stored in the PC.

我正在实现 AVAudioPlayer 来播放音频,它在播放本地存储在 PC 中的文件时运行良好。

But when i give the url of some audio file over the internet, it fails sadly. Here's what the code looks like:

但是当我通过互联网提供一些音频文件的网址时,它很遗憾地失败了。代码如下所示:

NSString *url = [[NSString alloc] init];  
url = @"http://files.website.net/audio/files/audioFile.mp3";  
NSURL *fileURL = [[NSURL alloc] initWithString: url];  
AVAudioPlayer *newPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];

Could anybody please point out the problem and what could be done?
Thanks!

任何人都可以指出问题以及可以做些什么?
谢谢!

采纳答案by Borut Tomazin

I tried other method initWithData on AVAudioPlayer instead of initWithContentsOfURL. First try to get MP3 file into NSData and then play this data.

我在 AVAudioPlayer 上尝试了其他方法 initWithData 而不是 initWithContentsOfURL。首先尝试将 MP3 文件放入 NSData 中,然后播放此数据。

Look at my code here.

这里查看我的代码。

回答by Gautam Jain

Use AVPlayer to stream audio/video based on http url's. It will work fine. AVAudioPlayer is for local files. Here's the code

使用 AVPlayer 根据 http url 流式传输音频/视频。它会正常工作。AVAudioPlayer 用于本地文件。这是代码

NSURL *url = [NSURL URLWithString:url];    
self.avAsset = [AVURLAsset URLAssetWithURL:url options:nil];    
self.playerItem = [AVPlayerItem playerItemWithAsset:avAsset];    
self.audioPlayer = [AVPlayer playerWithPlayerItem:playerItem];    
[self.audioPlayer play];

回答by leviathan

This is what the Apple docs say:

这是苹果文档所说的:

The AVAudioPlayerclass does not provide support for streaming audio based on HTTP URL's. The URL used with initWithContentsOfURL:must be a File URL (file://). That is, a local path.

AVAudioPlayer类不基于HTTP URL的音频流提供支持。使用的 URLinitWithContentsOfURL:必须是文件 URL ( file://)。即本地路径。

回答by alijandro

Use AVPlayerand monitor its status to start playback.

使用AVPlayer并监控其状态以开始播放。

Here is a workable example, hope it would be helpful.

这是一个可行的例子,希望它会有所帮助。

@implementation AudioStream

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if (context == PlayerStatusContext) {
        AVPlayer *thePlayer = (AVPlayer *)object;
        switch ([thePlayer status]) {
            case AVPlayerStatusReadyToPlay:
                NSLog(@"player status ready to play");
                [thePlayer play];
                break;
            case AVPlayerStatusFailed:
                NSLog(@"player status failed");
                break;
            default:
                break;
        }
        return;
    } else if (context == ItemStatusContext) {
        AVPlayerItem *thePlayerItem = (AVPlayerItem *)object;
        switch ([thePlayerItem status]) {
            case AVPlayerItemStatusReadyToPlay:
                NSLog(@"player item ready to play");
                break;
            case AVPlayerItemStatusFailed:
                NSLog(@"player item failed");
                break;
            default:
                break;
        }
        return;
    }

    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

- (void)playAudioStream
{
    NSURL *audioUrl = [NSURL URLWithString:@"your_stream_url"];
    AVURLAsset *audioAsset = [AVURLAsset assetWithURL:audioUrl];
    AVPlayerItem *audioPlayerItem = [AVPlayerItem playerItemWithAsset:audioAsset];
    [audioPlayerItem addObserver:self forKeyPath:@"status" options:0 context:ItemStatusContext];
    self.player = [AVPlayer playerWithPlayerItem:audioPlayerItem];
    [self.player addObserver:self forKeyPath:@"status" options:0 context:PlayerStatusContext];
}

@end