如何从iPhone上的在线网址播放音频 [英] How to get audio to play from an online url on iPhone

查看:148
本文介绍了如何从iPhone上的在线网址播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很多人之前已经问过这个问题,但我不知道为什么这个问题在x-code和objective c中不能用于我的应用程序!

I know it's been asked before by plenty of people, but I just have no idea why this isn't working in my application in x-code and objective c!

基本上,我想在网址的应用程序中播放音频。只需使用Apple网站上的测试音频,我的代码就是:

Basically, I want to play audio in an application from a url. Just using the testing audio from the Apple site, my code for it is:

NSString *playString = @"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";

//Converts songURL into a playable NSURL
NSURL *playURL = [NSURL URLWithString:playString];

AVAudioPlayer *backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:playURL error:&error];

if (backgroundMusicPlayer == nil) {
    NSLog(@"%@", [error description]);
}
else {
    [backgroundMusicPlayer prepareToPlay];
    [backgroundMusicPlayer play];
}

它出现以下错误:

错误域= NSOSStatusErrorDomain代码= -43操作无法完成。(OSStatus错误-43。)

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

我可以'当我谷歌时,找不到任何东西。
任何想法?我应该尝试另一个音频流吗?或者有没有人有一个有效的例子?是因为我使用的是iPhone模拟器而不是真正的iPhone吗?

And I can't find anything when I google it. Any ideas? Should I try another audio stream? Or does anyone have an example that works? Is it because I am using the iPhone simulator not the actual iPhone?

编辑:

请注意 - 当我说URL时,我指的是来自实际网站,例如 http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8
我不想首先下载这首歌,我希望能够在下载时播放它!谢谢

Please note - When I say URL I mean from an actual website, like "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8" I don't want to have to download the song first, I want to be able to play it as it is downloading! Thanks

推荐答案

使用AVPlayer从url播放mp3文件

USe AVPlayer to play mp3 file from url

-(void)playselectedsong{

    AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];
    self.songPlayer = player;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[songPlayer currentItem]];
    [self.songPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];

    [self.songPlayer play];

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

    if (object == songPlayer && [keyPath isEqualToString:@"status"]) {
        if (songPlayer.status == AVPlayerStatusFailed) {
            NSLog(@"AVPlayer Failed");

        } else if (songPlayer.status == AVPlayerStatusReadyToPlay) {
            NSLog(@"AVPlayerStatusReadyToPlay");


        } else if (songPlayer.status == AVPlayerItemStatusUnknown) {
            NSLog(@"AVPlayer Unknown");

        }
    }
}

- (void)playerItemDidReachEnd:(NSNotification *)notification {

 //  code here to play next sound file

}

这篇关于如何从iPhone上的在线网址播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆