2017-06-03 251 views
0

我在播放本地队列(保存在设备上)歌曲时遇到了麻烦。我正在使用这种椰子树 - https://github.com/tumtumtum/StreamingKit。我的第一个队列中的第一个项目在完成后开始再玩一次。 它stopReason状态无StreamingKit重复播放歌曲ios

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!

UPDATE: 队列生成

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 
} 
+0

嘿。你的第一个项目在没有理由完成之后再玩一次,或者因为你在所有剩下的事情之后排队玩它? – tumtumtum

+0

@tumtumtum无理由。我可以给你我的代码的一部分,我创建一个队列。也许我做错了 –

+0

@tumtumtum更新了我的问题 –

回答

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

     } 
} 

希望它能帮助:)我花了很多时间来固定它 - 它的工作!