3

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

- (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中声明,没问题。)

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

4

2 回答 2

0

你必须创建对象

TDAudioInputStreamer

在客户端也是。

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

当您创建输出流时。

于 2015-08-10T08:48:25.547 回答
0

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

**- (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]);
    }
}
于 2016-03-16T09:28:54.783 回答