简体   繁体   中英

play audio from internet using AVAudioPlayer

I am implementing AVAudioPlayer to play audio and it works perfectly well while playing files locally stored in the 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!

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

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];

This is what the Apple docs say:

The AVAudioPlayer class 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.

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

Look at my code here .

Use AVPlayer and monitor its status to start playback.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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