【问题标题】:Stream audio to other iOS devices (using multipeer) AND play audio through local device将音频流式传输到其他 iOS 设备(使用多点)并通过本地设备播放音频
【发布时间】:2014-01-25 20:12:03
【问题描述】:

基本上,我通过多点连接将音频流式传输到其他 iOS 设备。我正在使用this tutorial,现在我可以将音乐流式传输到其他设备并让其他设备播放音乐。但是,我的本地设备主机不播放音乐。为了做到这一点,我基本上试过了

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
  self.outputStreamer = [[TDAudioOutputStreamer alloc] initWithOutputStream:[self.session outputStreamForPeer:peers[0]]];

        [self.outputStreamer streamAudioFromURL:[self.song valueForProperty:MPMediaItemPropertyAssetURL]];

        [self.outputStreamer start];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[self.song valueForProperty:MPMediaItemPropertyAssetURL]error: NULL];
    [self.player play];

peers 是一组连接的对等点,一切正常。如果我注释掉最后两行(AVAudioPlayer),那么到其他设备的流式传输可以工作,反之亦然。似乎我只能做一个或另一个。 (self.player在.h中声明,没问题。)

这种双音频播放有什么解决方案吗?提前致谢。

【问题讨论】:

  • 您找到问题的答案了吗?我正在寻找相同的解决方案.. 谢谢!
  • 嗨@Spenciefy我使用了相同的教程,但它可以播放到一个主机和一个客人,如果我尝试3个设备,它只能播放一个首先连接的设备,你是如何解决这个问题的?提前致谢!
  • @EranLevi 你对此有什么想法吗?

标签: ios objective-c audio avaudioplayer multipeer-connectivity


【解决方案1】:

你必须创建对象

TDAudioInputStreamer

也在客户端。

self.inputStream = [[TDAudioInputStreamer alloc] initWithInputStream:stream];
[self.inputstream start];

当你创建输出流时。

【讨论】:

    【解决方案2】:

    您可以使用媒体选择器选择您的歌曲,然后需要转换您的资产

    **- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
    `
    
    
    ----------
    
    
    -
    
    `**
        [self dismissViewControllerAnimated:YES completion:nil];
        someMutableArray = [mediaItemCollection items];
        NSLog(@"%@",someMutableArray);
        MPMediaItem *song=[mediaItemCollection.items objectAtIndex:0];
        NSString * type = [song valueForProperty:MPMediaItemPropertyMediaType];
        NSURL * url = [song valueForProperty:MPMediaItemPropertyAssetURL];
        NSDictionary*dict=[[NSDictionary alloc] init];
    
        AVAsset *asset = [AVAsset assetWithURL:url];
        NSArray * metadata = [asset commonMetadata];
        NSArray * metadata1 = [asset metadata];
        NSArray * metadata2 = [asset availableMetadataFormats];
    
        NSMutableDictionary *info = [NSMutableDictionary dictionary];
        info[@"title"] = [song valueForProperty:MPMediaItemPropertyTitle] ? [song valueForProperty:MPMediaItemPropertyTitle] : @"";
        info[@"artist"] = [song valueForProperty:MPMediaItemPropertyArtist] ? [song valueForProperty:MPMediaItemPropertyArtist] : @"";
        NSNumber *duration=[song valueForProperty:MPMediaItemPropertyPlaybackDuration];
    
        int fullminutes = floor([duration floatValue] / 60); // fullminutes is an int
        int fullseconds = trunc([duration floatValue] - fullminutes * 60);  // fullseconds is an int
    
        info[@"duration"] = [NSString stringWithFormat:@"%d:%d", fullminutes, fullseconds];
    
        MPMediaItemArtwork *artwork = [song valueForProperty:MPMediaItemPropertyArtwork];
    
        UIImage *image = [artwork imageWithSize:CGSizeMake(150, 150)];
        NSData * data = UIImageJPEGRepresentation(image, 0.0);
        image = [UIImage imageWithData:data];
    
        if (image)
            self.songArtWorkImageView.image = image;
        else
            self.songArtWorkImageView.image = nil;
    
        self.songTitleLbl.text = [NSString stringWithFormat:@"%@ \n[Artist : %@]", info[@"title"], info[@"artist"]];
        [_session sendData:[NSKeyedArchiver archivedDataWithRootObject:[info copy]] toPeers:_session.connectedPeers withMode:MCSessionSendDataReliable error: nil];
    
        @try {
            if(_session && _session.connectedPeers && [_session.connectedPeers count] > 0)   {
                for(int i=0;i<someMutableArray.count;i++){
                    MPMediaItem *song = [someMutableArray objectAtIndex:i];
                    for(int i=0;i<someMutableArray.count;i++){
                        MPMediaItem *song = [someMutableArray objectAtIndex:i];
    
                        AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[song valueForProperty:MPMediaItemPropertyAssetURL] options:nil];
    
                        [self convertAsset: asset complition:^(BOOL Success, NSString *filePath) {
                            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                                if(Success) {
                                    if(image)   {
                                        [self saveImage: image withComplition:^(BOOL status, NSString *imageName, NSURL *imageURL) {
                                            if(status)  {
                                                @try {
                                                    [_session sendResourceAtURL:imageURL withName:imageName toPeer:[_session.connectedPeers objectAtIndex:0] withCompletionHandler:^(NSError *error) {
                                                        if (error) {
                                                            NSLog(@"Failed to send picture to %@", error.localizedDescription);
                                                            return;
                                                        }
                                                        //Clean up the temp file
                                                        NSFileManager *fileManager = [NSFileManager defaultManager];
                                                        [fileManager removeItemAtURL:imageURL error:nil];
                                                    }];
                                                }
                                                @catch (NSException *exception) {
    
                                                }
                                            }
                                        }];
                                    }
                                    @try {
                                        if(!self.outputStream)  {
                                            NSArray * connnectedPeers = [_session connectedPeers];
                                            if([connnectedPeers count] != 0)    {
                                                [self outputStreamForPeer:[_session.connectedPeers objectAtIndex:0]];
                                            }
                                        }
                                    }
                                    @catch (NSException *exception) {
    
                                    }
    
                                    if(self.outputStream)    {
                                        if(!self.outputStreamer)    {
                                            self.outputStreamer = [[TDAudioOutputStreamer alloc] initWithOutputStream:self.outputStream];
                                        }
    
                                        [self.outputStreamer initStream:filePath];
    
                                        if(self.outputStreamer)    {
                                            [self.outputStreamer start];
                                        }
                                    }
                                }
                                else    {
                                    [UIView showMessageWithTitle:@"Error!" message:@"Error occured!" showInterval:1.5];
                                }
    
    
                            });
                        }];
    
                    }
                }
            }
        }
        @catch (NSException *exception) {
            NSLog(@"Expection: %@", [exception debugDescription]);
        }
    }
    

    【讨论】:

    • 我尝试了多点连接,如果我们连接了 3 个设备,它可以用于数据传输,但不能用于音频流,那么如何解决?
    猜你喜欢
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多