2013-04-18 159 views
0

我正在嘗試開發一個小型應用程序,該應用程序應該從特定鏈接流式傳輸音頻。我正在使用AVPlayer,但我不知道爲什麼它不工作。嘗試谷歌,但似乎沒有任何相關的。請幫助..謝謝使用AVPlayer在ios中進行音頻流式傳輸

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

NSURL *url = [NSURL URLWithString:@"http://108.166.161.206:8826/stream.mp3"]; 

self->playerItem = [AVPlayerItem playerItemWithURL:url]; 
self->player = [AVPlayer playerWithPlayerItem:playerItem]; 
self->player = [AVPlayer playerWithURL:url]; 
[player play]; 

} 
+0

檢查這個https://github.com/mattgallagher/AudioStreamer – BhushanVU 2013-04-18 07:05:57

+0

我檢查,但無法正確理解,因爲我對這個領域很陌生。請提出一些簡單的例子。 – Newbee 2013-04-18 07:09:22

+0

這可能是一個重複的問題。爲你找到這個: http://stackoverflow.com/questions/13131177/streaming-mp3-audio-with-avplayer – 2013-04-18 07:59:40

回答

-1

你可以用AVAudioPlayer試試這個:

NSData *fileData = [NSData dataWithContentsOfURL: 
        [NSURL URLWithString:@"http://108.166.161.206:8826/stream.mp3"]]; 
NSError *error = nil; 
self.player = [[AVAudioPlayer alloc] initWithData:fileData error:&error]; 
[self.player play]; 
+0

AVAudioPlayer類不支持基於HTTP URL的流式音頻。用於initWithContentsOfURL:的URL必須是文件URL(file://)。那就是一個本地路徑。 https://developer.apple.com/library/ios/qa/qa1634/_index.html – stetro 2015-04-29 19:34:40

0

不`噸知道爲什麼你第一次初始化您的播放器,具有playeritem並與NSURL初始化之後。 不管怎麼說,這裏是要走的路:

-(void) viewDidLoad{ 

    player = [[AVPlayer alloc] initWithURL: 
      [NSURL URLWithString:@"http://xxx/yourfile.mp3"]]; 

    [player addObserver:self [email protected]"status" options:0 context:nil]; 
} 

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

    if (object == player && [keyPath isEqualToString:@"status"]) { 
     if (player.status == AVPlayerStatusReadyToPlay) { 
      [player play]; 
     } 
     if (player.status == AVPlayerStatusFailed) { 
      NSLog(@"Something went wrong: %@", player.error); 
     } 
    } 
} 

AV Foundation Programming Guide應該是一個很好的起點,以找出AVPlayer的東西是如何工作的