2013-05-17 104 views
1

我正在使用AVAudioPlayer或AVPlayer在我的應用程序中播放實時音頻流,但鏈接沒有擴展名,所以無論何時嘗試設置它,它都會失敗。有沒有人有什麼建議?沒有擴展名的iOS流音頻

代碼:

NSString *path = @"http://audio7.radioreference.com/24705802"; 
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]]; 
NSError *error; 

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error:&error]; 
player.volume = 1.0f; 
player.numberOfLoops = -1.0f; 
[player prepareToPlay]; 

if (player == nil) { 
    NSLog(@"Error: %@ Description: %@", error.localizedDescription, error.description); 
} 
else { 
    [player play]; 
} 

回答

1

的NSData不構成一個流,但靜態數據(例如聲音數據所創建或從文件加載的數據)。你想創建一個帶有指向流的NSURL的AVPlayer對象。例如:

AVPlayer *player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:path]]; 

[player play];