Problem in playing & recording sound with two diff

2019-07-30 14:44发布

问题:

I have implemented two AVAudioPlayer in my iPad project. I am playing sound with one player object and also simultaneously recording with the other player.

First Scenario

1.Player1 is recording the words I am speaking. 2.Simultaneously player1 is also recording the music playing with player2.

*This scenario is working fine if I am not plugging in any headset with microphone to my iPad.

*But, if I plugged in the headset to my iPad & repeat the above scenario, the following points are observed:

i. I can hear the music being played by player2 in my headset.

ii. But after I stopped recording my voice through the microphone of the headset, there is no music sound when I playback the recorded track. I can only hear my voice which I recorded thru the microphone of headset.This is my problem. I want that with 'headset with microphone' my application should work same as with scenario1.

I am using the following settings for my AVAudioSession:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
            NSError *err = nil;
            [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];

The following are the record settings for the recorder:

NSDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                            [NSNumber numberWithFloat: 44100.0],AVSampleRateKey,
                                            [NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,
                                            [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                            [NSNumber numberWithInt: AVAudioQualityMax],AVEncoderAudioQualityKey,nil];

The track is saved in the .wav file format.

Also, the music played by player2 is of .wav or .mp3 format.

Please help me in resolving this issue.

回答1:

I'm a bit rusty with AVAudioSession, so can't offer direct help with that. What I can say, is that what tends to happen is that when people find that they can't do what they want with AVAudioSession, they move to OpenAL. OpenAL can be a bit daunting to start, but What I did, was download Cocos2D. The nice thing about Cocos2D (CocosDenshion) is that it has these simple wrapper classes for OpenAL. And the code is all exposed, so you can poke around in it and see how it all works. It is a bit tricky to set-up (you have to mess about with frameworks and adding source code to your project), but once you have that sorted it is all good.

This was the best path for me, hope it works for you too!

...Reading the question again, could it be simply that the voice recorder (which is recording ambient sounds) isn't recording any music sounds when you have your headset/earphones on? The solution to this would seem to be to play back the original music, along with the recording (simultaneously).