-2

I want to record audio and streaming on another iPhone. Is this format correct for record and streaming?

format -> mSampleRate = 44100.00; //
    format -> mFormatID = kAudioFormatLinearPCM; //
    format -> mFramesPerPacket = 1;
    format -> mChannelsPerFrame = 1; //
    format -> mBitsPerChannel = 16; //
    format -> mReserved = 0;
    format -> mBytesPerPacket = 2;
    format -> mBytesPerFrame = 2;
    format -> mFormatFlags =  kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;

call for start recording:

- (void) startRecordingInQueue {
    [self setupAudioFormat:&recordState.dataFormat];

    recordState.currentPacket = 0;

    OSStatus status;

    status = AudioQueueNewInput(&recordState.dataFormat, AudioInputCallback, &recordState, CFRunLoopGetCurrent(),kCFRunLoopCommonModes, 0, &recordState.queue);



    status = 0;
    if(status == 0) {
        //Prime recording buffers with empty data
        AudioQueueBufferRef buffer;
        for (int i=0; i < NUM_BUFFERS; i++) {


            AudioQueueAllocateBuffer(recordState.queue, SAMPLERATE, &recordState.buffers[i]);
            AudioQueueEnqueueBuffer(recordState.queue, recordState.buffers[i], 0, NULL);

        }

        status = AudioFileCreateWithURL(fileURL, kAudioFileAIFFType, &recordState.dataFormat, kAudioFileFlags_EraseFile, &recordState.audioFile);


        NSLog(@"ss %i",status);
        status = 0;
        if (status == 0) {
            recordState.recording = true;
            status = AudioQueueStart(recordState.queue, NULL);

            if(status == 0) {
                NSLog(@"-----------Recording--------------");
                NSLog(@"File URL : %@", fileURL);
                NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",fileURL] relativeToURL:NULL];
                [[NSUserDefaults standardUserDefaults] setURL:url forKey:@"fileUrl"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
        }
    }

    if (status != 0) {
        [self stopRecordingInQueue];
    }
}

if it ok،

How to get out audio buffer data for sending to the server in this code? And how to play its data in other devices?

void AudioInputCallback(void * inUserData,
                        AudioQueueRef inAQ,
                        AudioQueueBufferRef inBuffer,
                        const AudioTimeStamp * inStartTime,
                        UInt32 inNumberPacketDescriptions,
                        const AudioStreamPacketDescription * inPacketDescs)

{

    RecordState * recordState = (RecordState*)inUserData;

    if (!recordState->recording)
    {
        printf("Not recording, returning\n");
    }

    printf("Writing buffer %lld\n", recordState->currentPacket);
    OSStatus status = AudioFileWritePackets(recordState->audioFile,
                                            false,
                                            inBuffer->mAudioDataByteSize,
                                            inPacketDescs,
                                            recordState->currentPacket,
                                            &inNumberPacketDescriptions,
                                            inBuffer->mAudioData);




    if (status == 0)
    {
        recordState->currentPacket += inNumberPacketDescriptions;


        AudioQueueEnqueueBuffer(recordState->queue, inBuffer, 0, NULL);


    }
}

if anyone has complete code for this project please link me to source. thanks

iEhsan
  • 47
  • 1
  • 10

1 Answers1

0

This one of my code you try it to change the formate

Here I stopped the audio recording after that I'm sending to server. This is my requirement. For this I'm converting audio file into .wav. After sending into server also it's playing successfully. Try it...

- (IBAction)stop:(id)sender { // On tap stop button

NSString *urlString;

NSLog(@"Stop Recording");
if ([audioRecorder isRecording]) // If recording audio 
{
    [audioRecorder stop]; //Stop it 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    [audioSession setActive:NO error:nil];

    NSLog(@"%@", audioRecorder.url); 

    unsigned long long size = [[NSFileManager defaultManager] attributesOfItemAtPath:[audioRecorder.url path] error:nil].fileSize;//Get audio file path
    NSLog(@"This is the file size of the recording in bytes: %llu", size); // Audio file size in bytes

Here my file saved in documents directory with filename "Audio". So no fetched that file and I changed file name into 56e254fa816e8_1519650589.wav and I'm sending that file to server.

    self.sendFileName = [@"56e254fa816e8" stringByAppendingString:@"_"];
    self.sendFileName = [self.sendFileName stringByAppendingString:[@(self.unixTimeStamp) stringValue]];
    self.sendFileName = [self.sendFileName stringByAppendingString:@".wav"]; // Here name converted in to .wav completely

    NSLog(@"%@", self.sendFileName);//See here the file formate

    urlString = [audioRecorder.url.absoluteString stringByReplacingOccurrencesOfString:@"Audio" withString:self.sendFileName];

    self.sendingURL = [NSURL URLWithString:urlString];

    NSLog(@"%@", self.sendingURL);//Complete file url

}

}

Try this...

Naresh
  • 16,698
  • 6
  • 112
  • 113