1

I cannot seem to get this link:

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

to work with the AVAudioPlayer. I have tested it in a Souncloud API started project and it seems to work just fine, however, when I try to implement it on my own it doesn't seem to work.

The error I get:

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:.....

The code:

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");

The streamURL prints as expected, and then the program crashes.

the tests are not printed. When everything else is commented out, and the NSURLRequest is left, it still crashes. When I comment the whole block of code out, everything compiles and runs.

I now have attempted this:

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

This also returns the length error, I am at a loss on what could be causing this...

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:

Paulius Dragunas
  • 1,702
  • 3
  • 19
  • 29

1 Answers1

1

streamURL is an NSString - not a NSURL

try:

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

I'm also not clear where the variable "streamData" is coming from (or what you expect to be in it).

The NSURLConnection is loading the data from the request asynchronously. It looks like you are assuming that the data is loaded synchronously and is available when you're initializing the "player" object. The data will (mostly likely) not be there when player is initialized.

xyzzycoder
  • 1,831
  • 13
  • 19
  • I changed it to NSString *streamString = [NSString stringWithFormat:@"%@", allDataDictionarySound[@"stream_url"], nil]; NSURL *streamURL = [NSURL URLWithString:streamString]; It still throws an error when the NSURLRequest is not commented out – Paulius Dragunas Jan 05 '13 at 00:17
  • error: 2013-01-04 18:21:59.014 CollectionViewTest[17936:c07] -[NSURL length]: unrecognized selector sent to instance 0x929c4a0 2013-01-04 18:21:59.015 CollectionViewTest[17936:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL length]: unrecognized selector sent to instance 0x929c4a0' *** First throw call stack: – Paulius Dragunas Jan 05 '13 at 00:22
  • You're sending the "length" method to an NSURL (it's implemented on NSString). You need to think about the distinction between an NSString that stores a URL and an NSURL. They're not interchangeable. – xyzzycoder Jan 05 '13 at 00:27
  • Where would I be sending the length method to the NSURL? I tried your method as well, and gives me the same error. – Paulius Dragunas Jan 05 '13 at 00:31
  • You're not getting the same error. Originally you were sending "absoluteURL" to an NSString. Now you're sending "length" to an NSURL. – xyzzycoder Jan 05 '13 at 00:33
  • You can set a breakpoint on exceptions to see which line of code is sending the offending "length" message. http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4 – xyzzycoder Jan 05 '13 at 00:34
  • To clarify, I was saying that my method and your method of passing in a URL rather than a string would return the same, length, error. – Paulius Dragunas Jan 05 '13 at 00:47
  • The issue ended up being inside the stream link itself. I needed to add qualifications to the soundcloud link with my client_id – Paulius Dragunas Jan 08 '13 at 00:23