【问题标题】:iOS stream audio with no extension没有扩展的 iOS 流音频
【发布时间】:2013-05-17 20:10:42
【问题描述】:

我希望使用 AVAudioPlayer 或 AVPlayer 在我的应用程序中播放实时音频流,但是该链接没有扩展名,因此每当我尝试设置它时,它都会失败。有人有什么建议吗?

代码:

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

【问题讨论】:

    标签: ios audio streaming avaudioplayer avplayer


    【解决方案1】:

    NSData 不构成流,而是静态数据(例如,您创建的声音数据或从文件加载的数据)。你想创建一个 NSURL 指向流的 AVPlayer 对象。例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 2018-12-18
      • 1970-01-01
      • 2022-01-05
      • 2014-11-25
      • 1970-01-01
      • 2014-10-19
      相关资源
      最近更新 更多