gpt4 book ai didi

ios - 让流与 AVAudioPlayer 一起工作

转载 作者:行者123 更新时间:2023-11-29 13:21:56 24 4
gpt4 key购买 nike

我似乎无法获得此链接:

https://api.soundcloud.com/tracks/54507667/stream

与 AVAudioPlayer 一起工作。我已经在 Souncloud API 启动的项目中对其进行了测试,它似乎工作得很好,但是,当我尝试自己实现它时,它似乎不起作用。

我得到的错误:

unrecognized selector sent to instance 0x9245d40 2013-01-04 17:56:04.699 CollectionViewTest[17023:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString absoluteURL]: unrecognized selector sent to instance 0x9245d40' * First throw call stack:.....

代码:

NSURL *streamURL = [NSString stringWithFormat:@"%@",
allDataDictionarySound[@"stream_url"], nil];
NSLog(streamURL);

NSURLRequest *request = [NSURLRequest requestWithURL:streamURL];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
connectionPlay = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"test:");
NSError *playerError;
player = [[AVAudioPlayer alloc] initWithData:streamData error:&playerError];
NSLog(@"test:2");

streamURL 按预期打印,然后程序崩溃。

测试未打印。当其他所有内容都被注释掉并保留 NSURLRequest 时,它仍然会崩溃。当我注释掉整个代码块时,一切都会编译并运行。

我现在尝试这样做:

        NSData *_objectData = [NSData dataWithContentsOfURL:streamURL];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog(@"%@", [error description]);
else
[audioPlayer play];

这也会返回长度错误,我不知道是什么导致了这个......

2013-01-05 13:46:16.536 CollectionViewTest[28224:c07] -[NSURL length]: unrecognized selector sent to instance 0x928a470

2013-01-05 13:46:16.546 CollectionViewTest[28224:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL length]: unrecognized selector sent to instance 0x928a470' * First throw call stack:

最佳答案

streamURL 是一个 NSString - 而不是 NSURL

尝试:

NSURL *streamURL = [NSURL URLWithString: [NSString stringWithFormat:@"%@",
allDataDictionarySound[@"stream_url"], nil]];

我也不清楚变量“streamData”的来源(或者您希望其中包含的内容)。

NSURLConnection 正在从请求中异步加载数据。看起来您假设数据是同步加载的,并且在您初始化“播放器”对象时可用。初始化播放器时,数据(很可能)不存在。

关于ios - 让流与 AVAudioPlayer 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14166970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com