tumtumtum / StreamingKit

A fast and extensible gapless AudioPlayer/AudioStreamer for OSX and iOS (iPhone, iPad)
Other
2.43k stars 525 forks source link

delegate receive incorrect stopReason #248

Open jiayuzhang opened 8 years ago

jiayuzhang commented 8 years ago

Expected STKAudioPlayerStopReasonEof, but got STKAudioPlayerStopReasonNone from audioPlayer:didFinishPlayingQueueItemId:withReason:andProgress:andDuration

Here's a little background, my app maintains its own playlist (w/o using StreamingKit queue), only play one audio at a time, when my delegate receives didFinishPlayingQueueItemId and stopReason is EOF, it will go fetch the next one and play.

But, occasionally (very high odds) it got wrong stopReason, here's my observation

- (void) audioQueueFinishedPlaying:(STKQueueEntry*)entry
{
    STKQueueEntry* next = [bufferingQueue dequeue];
    [self processFinishPlayingIfAnyAndPlayingNext:entry withNext:next];
    [self processRunloop];
}

- (BOOL)processRunloop
{
 // ...
    if (self.internalState != STKAudioPlayerInternalStateStopped)
    {
        // This line executed and set the EOF stopReason correctly
        [self stopAudioUnitWithReason:STKAudioPlayerStopReasonEof];
    }
 // ...
}

-(void) processFinishPlayingIfAnyAndPlayingNext:(STKQueueEntry*)entry withNext:(STKQueueEntry*)next
{
// ...
    [self playbackThreadQueueMainThreadSyncBlock:^
    {
        // Sometimes (but very high chance), the delegate is triggered BEFORE
        // stopAudioUnitWithReason: in a different thread with the non-up-to-date
        // stopReason
        [self.delegate audioPlayer:self didFinishPlayingQueueItemId:queueItemId withReason:stopReason andProgress:progress andDuration:duration];
    }];
// ...
}

Seems like it is a mutlithreading issue, kinda of new to this library, anyone could help fixing it? THANKS

cuongthai commented 8 years ago

Can you post the url that cause the problem? I had the same issue but most of the URLs are ok. E.g http://www.hubharp.com/web_sound/WalloonLilliShort.mp3

cuongthai commented 8 years ago

I also notice that the library work just fine when I archive the app and run on real device. I will keep an eye on that. Can you confirm in your case?

navern commented 8 years ago

@jiayuzhang Hello, did you already managed how to fix this?

navern commented 8 years ago

I'm not good in thread management, so I made a quickfix for this:

- (void)audioPlayer:(STKAudioPlayer *)audioPlayer didFinishPlayingQueueItemId:(NSObject *)queueItemId withReason:(STKAudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration
{
    if (stopReason == STKAudioPlayerStopReasonNone)
    {
        __weak typeof(self) weakSelf = self;
        [self bk_performBlock:^(id obj) {

            [weakSelf checkPlayerRealStopReason];

        } afterDelay:0.1];
    }

    // Switch to next track if EOF
}

- (void)checkPlayerRealStopReason
{
    if (_streamingAudioPlayer.stopReason == STKAudioPlayerStopReasonEof)
    {
        // Switch to next track
    }
}

It works, but I still hope that someone will fix this the right way.

KenmuHuang commented 8 years ago

+1, the same issue. How to fix it?

mmoloksher commented 7 years ago

Also experiencing this issue, I do something similar to what @navern does, I check the player's stopReason after a millisecond, seems to work.

Denages commented 6 years ago

+1. Everything as said above