简体   繁体   English

将音频流式传输到其他iOS设备(使用多路复用)并通过本地设备播放音频

[英]Stream audio to other iOS devices (using multipeer) AND play audio through local device

Basically I'm streaming audio to other iOS devices through multipeer connectivity. 基本上我是通过多种连接将音频流式传输到其他iOS设备。 I am using this tutorial , and right now I can stream music to other devices and have the other devices play the music. 我正在使用本教程 ,现在我可以将音乐流式传输到其他设备,让其他设备播放音乐。 However, my local device host doesn't play the music. 但是,我的本地设备主机不播放音乐。 In order to do this, I basically tried 为了做到这一点,我基本上尝试过

- (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 is an array of connected peers, everything is working fine with that. peer是一组连接的对等体,一切都正常。 If I comment out the last two lines (the AVAudioPlayer), then the streaming to other devices works, vice versa. 如果我注释掉最后两行(AVAudioPlayer),那么流向其他设备的工作,反之亦然。 It seems like I can only do one or the other. 好像我只能做一个或另一个。 (self.player is declared in the .h, it is fine.) (self.player在.h中声明,很好。)

Any solution to this double audio playing? 这个双音频播放的任何解决方案? Thanks in advance. 提前致谢。

You have to create object of 你必须创建对象

TDAudioInputStreamer TDAudioInputStreamer

on client end too. 在客户端也是如此。

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

When you create output stream. 创建输出流时。

you can pick your song with media picker then need to convert your assets 您可以使用媒体选择器选择您的歌曲然后需要转换您的资产

**- (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]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM