StreamingKit重复歌曲ios

时间:2017-06-03 11:57:32

标签: ios objective-c swift cocoapods audio-player

播放本地(保存在设备上)歌曲的队列时遇到问题。我正在使用这个cocoapod - https://github.com/tumtumtum/StreamingKit。队列中的第一个项目在完成后再开始播放一次。 它有stopReason状态NONE

    func audioPlayer(_ audioPlayer: STKAudioPlayer, didFinishPlayingQueueItemId queueItemId: NSObject, with stopReason: STKAudioPlayerStopReason, andProgress progress: Double, andDuration duration: Double) {

    if stopReason == .eof || stopReason == .pendingNext {
        checkNextTrack()
    }

    if stopReason == .none {
        print("NONE")
    }

    if stopReason == .error || stopReason == .userAction || stopReason == .disposed {
        stop()
        resetAudioPlayer()
    }
}

其他元素的状态为.eof.pendingNext,这是正确的行为。在那种情况下我该怎么办?所有远程网址都正常播放。 日Thnx!

更新 队列创建

    internal func playWithQueue(queue: [Song], index: Int = 0) {
    var audioListNew = [AudioItem]()
    for (index, value) in queue.enumerated() {
        let audioItem = AudioItem(audioItem: value, audioIndex: index)
        audioListNew.append(audioItem)
    }

    guard index >= 0 && index < audioListNew.count else { return }
    newQueue(queue: audioListNew, index: index)
}


func newQueue(queue: [AudioItem], index: Int = 0) {
    self.queue = queue
    audioPlayer.clearQueue()

    if let currentSong = self.queue[index].audioItem {
        play(file: currentSong)

        for (songIndex, _) in queue.enumerated() {
            audioPlayer.queue( (queue[Int((index + songIndex) % queue.count)].audioItem?.songRealUrl)! )
        }

    }

    currentIndex = index
}

1 个答案:

答案 0 :(得分:0)

我有同样的问题,我解决了! 你需要做那样的事情:

    -(void) audioPlayer:(STKAudioPlayer*)audioPlayer didFinishBufferingSourceWithQueueItemId:(NSObject*)queueItemId
{
    SampleQueueId* queueId = (SampleQueueId*)queueItemId;

    NSLog(@"Requeuing: %@", [queueId.url description]);

//    [self.audioPlayer queueDataSource:[STKAudioPlayer dataSourceFromURL:queueId.url] withQueueItemId:[[SampleQueueId alloc] initWithUrl:queueId.url andCount:queueId.count+1]];
}

之后您需要编写停止操作:

    -(void) audioPlayer:(STKAudioPlayer*)audioPlayer didFinishPlayingQueueItemId:(NSObject*)queueItemId withReason:(STKAudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration
{
    SampleQueueId* queueId = (SampleQueueId*)queueItemId;
    NSLog(@"Finished: %@", [queueId.url description]);


    if(stopReason == STKAudioPlayerStopReasonEof)
        {
            if(self.isMainAudioPlay)
            {
                [self.mainAudioPlayer setState:STKAudioPlayerStateStopped];
                [self.mainAudioPlayer stop];
                self.playMainAudioFrame.image = [UIImage imageNamed:PlayIconMain];
            }
            else
            {
            [self.audioPlayer setState:STKAudioPlayerStateStopped];
            [self.audioPlayer stop];

            if(self.comments.count > 0)
            {
                UITableView *tableView = self.tabelView;
                RCommentsModel *newsStop = (self.comments)[self.indexpathForStop.row];
                newsStop.commentPlayIcon = PlayIconMain;
                [self.comments replaceObjectAtIndex:self.indexpathForStop.row withObject:newsStop];

                [tableView beginUpdates];
                [tableView reloadRowsAtIndexPaths:@[self.indexpathForStop] withRowAnimation:UITableViewRowAnimationNone];
                [tableView endUpdates];
            }
            }
        }
        else if (stopReason == STKAudioPlayerStopReasonUserAction)
        {
            if(self.isMainAudioPlay)
            {
                [self.mainAudioPlayer setState:STKAudioPlayerStateStopped];
                [self.mainAudioPlayer stop];
                self.playMainAudioFrame.image = [UIImage imageNamed:PlayIconMain];
            }
            else
            {
            [self.audioPlayer setState:STKAudioPlayerStateStopped];
            [self.audioPlayer stop];

            if(self.comments.count > 0)
            {
                UITableView *tableView = self.tabelView;

                RCommentsModel *newsStop = (self.comments)[self.indexpathForStop.row];
                newsStop.commentPlayIcon = PlayIconMain;
                [self.comments replaceObjectAtIndex:self.indexpathForStop.row withObject:newsStop];

                [tableView beginUpdates];
                [tableView reloadRowsAtIndexPaths:@[self.indexpathForStop] withRowAnimation:UITableViewRowAnimationNone];
                [tableView endUpdates];
            }
            }
        }
        else if (stopReason == STKAudioPlayerStopReasonNone)
        {

        }
}

希望它有所帮助!:)我花了很多时间来修复它 - 并且它有效!