通过多对等连接在多台设备中播放歌曲

时间:2019-01-24 13:15:34

标签: ios objective-c multipeer-connectivity

我想在多设备连接的同时播放歌曲,例如 ampme应用。因此,我使用了this library,但遇到了以下困难。

1。)主持人暂停了歌曲,但所有来宾都没有收到暂停动作

2。)有时语音中断

3。)无法互相连接设备

4。)暂停操作后歌曲无法同步播放

5。)歌曲未在来宾中播放,但表明正在播放中

6。)播放歌曲时设备断开连接

7。)连接设备问题

8。)同样,歌曲会在多于两台设备中播放,然后只会在一台设备中播放。

这是一些代码

  

主机端

 - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    [self dismissViewControllerAnimated:YES completion:nil];

    if (self.outputStreamer) return;

    self.song = mediaItemCollection.items[0];

    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    info[@"title"] = [self.song valueForProperty:MPMediaItemPropertyTitle] ? [self.song valueForProperty:MPMediaItemPropertyTitle] : @"";
    info[@"artist"] = [self.song valueForProperty:MPMediaItemPropertyArtist] ? [self.song valueForProperty:MPMediaItemPropertyArtist] : @"";

    MPMediaItemArtwork *artwork = [self.song valueForProperty:MPMediaItemPropertyArtwork];
    UIImage *image = [artwork imageWithSize:self.albumImage.frame.size];
    if (image)
        info[@"artwork"] = image;

    if (info[@"artwork"])
        self.albumImage.image = info[@"artwork"];
    else
        self.albumImage.image = nil;

    self.songTitle.text = info[@"title"];
    self.songArtist.text = info[@"artist"];

    [self.session sendData:[NSKeyedArchiver archivedDataWithRootObject:[info copy]]];

    NSArray *peers = [self.session connectedPeers];

    if (peers.count) {
        self.outputStreamer = [[TDAudioOutputStreamer alloc] initWithOutputStream:[self.session outputStreamForPeer:peers[0]]];
        [self.outputStreamer streamAudioFromURL:[self.song valueForProperty:MPMediaItemPropertyAssetURL]];
        [self.outputStreamer start];
    }
}
  

访客用户端

#pragma mark - TDSessionDelegate

- (void)session:(TDSession *)session didReceiveData:(NSData *)data
{
    NSDictionary *info = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    [self performSelectorOnMainThread:@selector(changeSongInfo:) withObject:info waitUntilDone:NO];
}

- (void)session:(TDSession *)session didReceiveAudioStream:(NSInputStream *)stream
{
    if (!self.inputStream) {
        self.inputStream = [[TDAudioInputStreamer alloc] initWithInputStream:stream];
        [self.inputStream start];
    }
}

0 个答案:

没有答案