【问题标题】:StreamingKit repeats song iosStreamingKit 重复歌曲 ios
【发布时间】:2017-06-03 11:57:32
【问题描述】:

我在播放本地(保存在设备上)歌曲队列时遇到问题。我正在使用这个 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,这是正确的行为。在这种情况下我该怎么办?所有远程 url 都在正确播放。 谢谢!

更新: 队列创建

    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
}

【问题讨论】:

  • 嘿。您的第一个项目是在无缘无故地完成之后再播放一次,还是因为您在所有其他项目之后排队播放?
  • @tumtumtum 无缘无故。我可以给你我创建队列的代码的一部分。也许我做错了什么
  • @tumtumtum 更新了我的问题
  • @tumtumtum 我提到我队列中的每首歌曲都以状态 .none 结尾,但播放器切换到下一首歌曲。这是正常行为吗?

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


【解决方案1】:

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

    -(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)
        {

        }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多