I'm trying to stream a Deezer radio through my app but can only play the first track and can't get any callback when I want to play others after the first one has finished playing (playerstate -> finished). When I call again grabStream, I don't go through DeezerSessionRequestDelegate methods. any help would be appreciated. Thanks.
Here are the main methods used :
-(void)grabStream{
[[DeezerSession sharedSession] requestRadioForListening:kRadioId];
}
#pragma mark - DeezerSessionRequestDelegate
- (void)deezerSessionRequestDidReceiveResponse:(NSData *)data {
NSLog(@"deezerSessionRequestDidReceiveResponse");
NSDictionary* dictionary = [data objectFromJSONData];
DeezerTrack* track = [[DeezerTrack alloc] initWithDictionary:dictionary];
if ([_delegate respondsToSelector:@selector(onGetStream:forTrackId:)]) {
NSString* stream = [dictionary objectForKey:@"stream"];
if ([stream isKindOfClass:[NSString class]]) [_delegate onGetStream:stream forTrackId:[track deezerID]];
}
[track release];
}
- (void)deezerSessionRequestDidFailWithError:(NSError*)error {
NSLog(@"deezerSessionRequestDidFailWithError");
}
and in another class
#pragma mark - DeezerGrabberDelegate
- (void)onGetStream:(NSString *)stream forTrackId:(NSString *)trackId{
NSLog(@"onGetStream :: previous stream track id :: %@ next track id :: %@", _currentStreamTrackId, trackId);
if(![trackId isEqualToString:_currentStreamTrackId]){
_currentStreamTrackId = trackId;
[[DeezerAudioPlayer sharedSession] initPlayerForRadioWithDeezerId:trackId stream:stream];
}
}
#pragma mark - DeezerAudioPlayerDelegate
-(void)playerStateChanged:(DeezerPlayerState)playerState{
NSLog(@"playerStateChanged :: %i", playerState);
switch (playerState) {
case DeezerPlayerState_Initialized :
case DeezerPlayerState_Ready :
case DeezerPlayerState_Playing :
case DeezerPlayerState_Paused :
case DeezerPlayerState_WaitingForData :
case DeezerPlayerState_Stopped :break;
case DeezerPlayerState_Finished :
NSLog(@"+++++++++++++End of track, we're going to play another one");
[self.grabber grabStream];
break;
}
}
I was struggling with the same problem. The thing is
-(void)playerStateChanged:(DeezerPlayerState)playerState
with player state in
DeezerPlayerState_Finished
gets called from another thread (not main thread) and the Deezer SDK is the one to blame.
Solution is simple, in your switch statement make sure [self.grabber grabStream] is invoked using 'performSelectorOnMainThread', like so
[self.grabber performSelectorOnMainThread:@selector(grabStream) withObject:nil waitUntilDone:false];