目标C - 是否可以录制实时音频流+将其保存到设备?

时间:2015-12-13 19:12:39

标签: objective-c avaudioplayer avplayer avaudiorecorder avaudiosession

我想录制现场音频流。我正在使用AVPlayer播放音频流。我在网上找到关于录制音频的所有内容都涉及使用AVAudioRecorder和AVAudioPlayer播放/录制本地文件。我错过了什么吗?

我真的被困 - 任何帮助都会受到高度赞赏。

编辑:这位有效:

    @interface ViewControllerPlayer ()

    @end

    @implementation ViewControllerPlayer

    @synthesize receivedStreamReferenceNumber;

    - (void)convertData:(NSData *) data {
        NSString *urlString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSURL *url = [[NSURL alloc] initWithString:urlString];
        [self loadPlayer:url];
    }

    - (void) loadPlayer:(NSURL *) url {
        audioPlayer = [AVPlayer playerWithURL:url];
        [audioPlayer play];
    }

    - (void) start {
        NSLog(@"%@", receivedStreamReferenceNumber);
        NSString *urlHalf = @"http://getstreamurl.php?KeyRef=";
        NSMutableString *mutableUrlString = [NSMutableString stringWithFormat:@"%@%@", urlHalf, receivedStreamReferenceNumber];
        NSURL *url = [NSURL URLWithString: mutableUrlString];
        NSData *data = [NSData dataWithContentsOfURL:url];
        [self convertData:data];
    }

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self start];
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    @end

我认为我已经走上正轨了......但我不清楚如何正确实现它:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [stopButton setEnabled:YES];
    [playButton setEnabled:YES];

    // Set the audio file
    NSArray *pathComponents = [NSArray arrayWithObjects:
                               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                               @"xxx.mp3",
                               nil];
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

    // Setup audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];


    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];
}

0 个答案:

没有答案