1

我似乎无法获得此链接:

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

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

我得到的错误:

无法识别的选择器发送到实例 0x9245d40 2013-01-04 17:56:04.699 CollectionViewTest[17023:c07] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString absoluteURL]:无法识别的选择器发送到实例 0x9245d40”*首先抛出调用堆栈:......

编码:

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] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSURL 长度]:无法识别的选择器发送到实例 0x928a470”*第一次抛出调用堆栈:

4

1 回答 1

1

streamURL 是 NSString - 不是 NSURL

尝试:

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

我也不清楚变量“streamData”来自哪里(或者你期望它是什么)。

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

于 2013-01-05T00:04:22.487 回答