2

We have crashes when playing using cocoalibspotify: The problem looks like that in the callback function music_delivery() under SPSession.m, the instance initiated callback is not a SPSessionAudioDeliveryDelegate compatible instance. However, the only playback related instance we used is SPPlaybackManager. And the function we used to play the song using spotify is:

#pragma mark - Domain function
- (void)searchForKeywords:(NSString *)keywords{
    //construct search string
    SPSearch *search = [[SPSearch alloc] initWithSearchQuery:keywords inSession:[SPSession sharedSession]];

    //search track
    [SPAsyncLoading waitUntilLoaded:search timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *results, NSArray *notLoadedResults) {
    if ([results count]>0) {
        //search returns results.
        SPSearch *thisSearch = [results objectAtIndex:0];
        if ([[thisSearch tracks] count]>0) {

            // Add loading flag for album art
            [[ImageManager sharedInstance] setLoadingFlagForSong:self.namedSong];

            SPTrack *track = [[thisSearch tracks]objectAtIndex:0];

            [SPAsyncLoading waitUntilLoaded:track timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *tracks, NSArray *notLoadedTracks) {

                self.currentTrack = track;
            }];
        }else{
        }

    }else{
        //search didn't return results.
    }
    }];
}

- (void)play
{
[SPSession dispatchToLibSpotifyThread:^{
    [[SPSession sharedSession]setPlaybackDelegate:self];

    // Change the current track of the session
    if (currentTrack) {

        self.playbackManager = [[SPPlaybackManager alloc] initWithPlaybackSession:[SPSession sharedSession]];
        [[SPSession sharedSession]setPlaybackDelegate:self];

        [self.playbackManager playTrack:currentTrack callback:^(NSError *error) {

            // Failure playing
            if (error) {
            }
            // Success
        }];
    }

    // Play
    else {
    }
}];
}

- (void)pause
{
[SPSession dispatchToLibSpotifyThread:^{
    [SpotifyHelper sharedInstance].playbackManager.isPlaying = NO;
} waitUntilDone:YES];
}


- (void)stop
{
[SPSession dispatchToLibSpotifyThread:^{
    [SpotifyHelper sharedInstance].playbackManager.isPlaying = NO;
    [[SpotifyHelper sharedInstance].playbackManager sessionDidEndPlayback:[SPSession sharedSession]];
} waitUntilDone:YES];
}

-(void)seekToLocation:(float)location
{
[SPSession dispatchToLibSpotifyThread:^{
    [[SpotifyHelper sharedInstance].playbackManager seekToTrackPosition:location * self.currentTrack.duration];
} waitUntilDone:YES];
}

The error message: -[__NSCFType session:shouldDeliverAudioFrames:ofCount:streamDescription:]: unrecognized selector sent to instance 0x21328a90

Stack:
0    libsystem_kernel.dylib  __pthread_kill + 8
1    libsystem_c.dylib   pthread_kill + 58
2    libsystem_c.dylib   abort + 94
3    libc++abi.dylib     abort_message + 74
4    libc++abi.dylib     default_terminate() + 24
5    libobjc.A.dylib     _objc_terminate() + 146
6    libc++abi.dylib     safe_handler_caller(void (*)()) + 78
7    libc++abi.dylib     std::terminate() + 19
8    libc++abi.dylib     __cxa_throw + 122
9    libobjc.A.dylib     objc_exception_throw + 94
10   CoreFoundation  __methodDescriptionForSelector
11   CoreFoundation  ___forwarding___ + 392
12   CoreFoundation  _CF_forwarding_prep_0 + 24
crash->13    OURAPP      music_delivery
14   OURAPP  sp_playlist_get_offline_download_completed
15   OURAPP  sp_error_message
16   OURAPP  sp_error_message
wao813
  • 468
  • 1
  • 5
  • 14

1 Answers1

2

Two things:

1) Don't dispatch to the CocoaLibSpotify thread when calling CocoaLibSpotify methods — that's done internally. Remove the dispatchToLibSpotifyThread line.

2) Remove the [[SPSession sharedSession]setPlaybackDelegate:self]; line. That's what's causing your crash.

iKenndac
  • 18,730
  • 3
  • 35
  • 51
  • Thanks a lot! So the dispatchToLibSpotifyThread should not be used in the pause, stop, seektoposition scenarios right? Please check my original post for my code. – wao813 Jul 23 '13 at 23:51
  • Also, if I remove the setplaybackdelegate here, should I put it somewhere else? Like in the -init method. – wao813 Jul 24 '13 at 00:03
  • If you're not sure if you should use `dispatchToLibSpotifyThread`, don't use it. It's very rarely needed, and if you do need to use it the library will tell you at runtime. And no, just delete the line of code and don't put it anywhere else. – iKenndac Jul 24 '13 at 10:24
  • Hi iKenndac, if we delete the setPlaybackDelegate, how do we get notified by the Spotify library? We are currently using it to track when the song finished play by having (-(void)sessionDidEndPlayback:(id)aSession{}) – wao813 Jul 26 '13 at 22:15