2010-09-03 45 views
6

Tôi đang triển khai AVAudioPlayer để phát âm thanh và nó hoạt động hoàn hảo trong khi phát các tệp được lưu trữ cục bộ trong PC.phát âm thanh từ internet bằng AVAudioPlayer

Nhưng khi tôi cung cấp url của một số tệp âm thanh qua internet, nó không thành thật đáng buồn. Đây là những gì mã trông giống như:

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

thể ai xin chỉ ra những vấn đề và những gì có thể được thực hiện?
Cảm ơn!

+0

Bạn đã cố gắng cung cấp một đối tượng lỗi để xem liệu nó có chứa mô tả lỗi không? – Toastor

+0

Không, nhưng ứng dụng không bị lỗi ... chỉ hiển thị chế độ xem và không có gì xảy ra. – Bangdel

Trả lời

2

Tôi đã thử phương thức initWithData khác trên AVAudioPlayer thay vì initWithContentsOfURL. Trước tiên, hãy thử tải tệp MP3 vào NSData và sau đó phát dữ liệu này.

Nhìn mã của tôi here.

17

Đây là những gì các tài liệu của Apple nói:

Các AVAudioPlayer lớp không cung cấp hỗ trợ cho streaming audio dựa trên HTTP URL. URL được sử dụng với initWithContentsOfURL: phải là URL tệp (file://). Đó là, một con đường địa phương.

27

Sử dụng AVPlayer để phát âm thanh/video dựa trên url của http. Nó sẽ hoạt động tốt. AVAudioPlayer dành cho các tệp cục bộ. Đây là mã số

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

Sử dụng AVPlayer và theo dõi trạng thái của nó để bắt đầu phát lại.

Đây là một ví dụ khả thi, hy vọng nó sẽ hữu ích.

@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 
Các vấn đề liên quan