2

我知道很多人之前都问过这个问题,但我只是不知道为什么这在我的 x 代码和目标 c 中的应用程序中不起作用!

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

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。)

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

编辑:

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

4

3 回答 3

7

使用 AVPlayer 从 url 播放 mp3 文件

-(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

}
于 2013-04-06T11:50:56.073 回答
4

它非常简单使用 AVAudioPlayer,将对象作为 AVAudioPlayer *audioPlayer,

  NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
  NSData *soundData = [NSData dataWithContentsOfURL:url];
  audioPlayer = [[AVAudioPlayer alloc] initWithData:soundData  error:NULL];
  audioPlayer.delegate = self;
  [audioPlayer play];

在委托方法下面写:

 -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
    [audioPlayer stop];
     NSLog(@"Finished Playing");
  }

 - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
 {
  NSLog(@"Error occured");
 }
于 2013-04-06T11:53:42.663 回答
2

You may find a live test stream at here

To play a file use :

[player play];

And here is the code to play a live stream from URL:

NSURL *url = [NSURL URLWithString:@"<#Live stream URL#>];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
    /* [playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext]; (can use it) */
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:<#Live stream URL#>];
//(optional) [player addObserver:self forKeyPath:@"status" options:0 context:&PlayerStatusContext];
于 2013-04-06T12:11:51.717 回答