2013-04-18 2 views
0

나는 특정 링크에서 오디오를 스트리밍해야하는 작은 응용 프로그램을 개발하려고합니다. 그 AVPlayer를 사용하고 있지만 그 이유는 모르겠다. 구글 시도했지만 아무것도 거기에 관련이있는 것 같습니다. 친절하게 당신이 AVAudioPlayer 이것을 시도 할 수 있습니다 .. 도움말 당신에게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

+0

나는 내가이 분야에 대해 아주 새로운 것을 알았지 만 제대로 이해할 수 없다는 것을 확인했다. 친절하게 몇 가지 간단한 예를 제안합니다. – Newbee

+0

중복 된 질문 일 수 있습니다. 이것을 찾으 셨습니다 : http://stackoverflow.com/questions/13131177/streaming-mp3-audio-with-avplayer –

답변

-1

감사합니다 : 당신이 먼저 playeritem로하고이 NSURL로 init을 한 후 플레이어를 초기화하기 이유

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은 File URL (file : //)이어야합니다. 즉, 로컬 경로입니다. https://developer.apple.com/library/ios/qa/qa1634/_index.html – stetro

0

돈`t은 알고있다. 어쨌든, 여기 방법입니다 :

-(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를 물건

관련 문제