【问题标题】:getting a stream to work with AVAudioPlayer让流与 AVAudioPlayer 一起工作
【发布时间】:2013-01-06 03:48:39
【问题描述】:

我似乎无法获得此链接:

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” * 首先抛出调用栈:

【问题讨论】:

    标签: ios avaudioplayer


    【解决方案1】:

    streamURL 是 NSString - 不是 NSURL

    尝试:

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

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

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

    【讨论】:

    • 我把它改成了 NSString *streamString = [NSString stringWithFormat:@"%@", allDataDictionarySound[@"stream_url"], nil]; NSURL *streamURL = [NSURL URLWithString:streamString]; NSURLRequest 没有被注释掉的时候还是会报错
    • 错误:2013-01-04 18:21:59.014 CollectionViewTest[17936:c07] -[NSURL 长度]:无法识别的选择器发送到实例 0x929c4a0 2013-01-04 18:21:59.015 CollectionViewTest[ 17936:c07] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSURL 长度]:无法识别的选择器发送到实例 0x929c4a0”*** 第一次抛出调用堆栈:
    • 您将“length”方法发送到 NSURL(它在 NSString 上实现)。您需要考虑存储 URL 的 NSString 和 NSURL 之间的区别。它们不可互换。
    • 我应该在哪里将长度方法发送到 NSURL?我也尝试了你的方法,并给了我同样的错误。
    • 你没有得到同样的错误。最初您将“absoluteURL”发送到 NSString。现在您正在向 NSURL 发送“长度”。
    猜你喜欢
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2016-12-13
    • 2016-10-06
    • 2011-11-30
    • 2014-02-19
    • 2011-09-03
    • 2013-05-01
    相关资源
    最近更新 更多