更改程序,使用URL从iTunes获取音乐到本地文件

时间:2015-07-13 07:52:19

标签: iphone xcode audio stream multipeer-connectivity

我目前正在尝试通过mulipeer连接从麦克风制作应用流原始数据。 我一直在使用本教程作为基础https://robots.thoughtbot.com/streaming-audio-to-multiple-listeners-via-ios-multipeer-connectivity 但是现在我正在努力将URL从itunes库更改为我的本地文件。 我不是高级程序员,这是某种夏季项目。 当程序从itunes库获取音乐时,它使用以下代码:

- (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];

但我希望它能从录音机中获取音乐:

NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                           @"MyAudioMemo.m4a",
                           nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];

我一直在努力解决这个问题,并希望得到任何帮助!

0 个答案:

没有答案