IPhone SDK: AVAudioRecorder will not record after a call [AVPlayer play]

I have a view controller that uses AVPlayer. this view manager can load the modal view manager where the user can record audio using AVAudioRecorder.

this is what happens:

if the user plays the song in the first controller using [AVPLayer play], AVAudioRecorder will not be recorded in the modal view controller. There are no errors, but the current time returned by AVAudioRecorder is 0.0;

if the user rejects the modal dialog and reloads it. AVAudioRecorder works great.

it repeats again and again AVAudioRecorder does not work on the first call after the call [AVPlayer play]

I struggled with this for several days and simply rebuilt my code related to both AVPlayer and AVAudioRecorder, and it still acts weird.

Any help or pointer is greatly appreciated

Thanks in advance

Jean pierre

+7
source share
7 answers

I also had a problem, and I found this solution.

When recording, write this line code after initializing AVAudioRecord :

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:NULL];

Then call the record method.

+8
source

How do you set the AVAudioSession category, i.e. lose or record? Try something like

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 
+3
source

I have the same problem. I use AVPlayer to play songs (previous recordings that I used AVAudioRecord for). However, I found that as soon as I used AVPlayer, I could no longer use AVAudioRecorder. After some searching, I found that as long as AVPlayer is created in memory and played at least once (which usually happens immediately after its creation), AVAudioRecorder will not record. However, once AVPlayer is canceled, AVAudioRecorder can then record again. It looks like AVPlayer supports some kind of connection that AVAudioRecorder requires, and it's greedy ... it won't let it go until you get it out of your cold dead hands.

This is the solution I found. Some people claim that creating an instance of AVPlayer takes too much time to continue to break and configure backups. However, it is not. Activating AVPlayer is actually pretty trivial. An instance of AVPlayerItem is also created. What is not trivial is downloading AVAsset (or any of its subclasses). You really want to do this only once. They should use this sequence:

  • Download AVAsset (for example, if you are loading from a file, use AVURLAsset directly or add it to AVMutableComposition and use this) and save the link to it. Do not let this go until you are done with it. Downloading is what takes all the time.
  • Once you're ready for the game: create AVPlayerItem with your asset, then AVPlayer with AVPlayerItem and play it. Do not keep the link to AVPlayerItem, AVPlayer will save the link to it, and you will not be able to reuse it with another player.
  • Once this happens, immediately destroy AVPlayer ... release it, set its var to zero, whatever you do. **
  • Now you can record. AVPlayer does not exist, so AVAudioRecorder can do its best.
  • When you are ready to play again, recreate an instance of AVPlayerItem with the resource you already downloaded and AVPlayer. Again, this is trivial. The asset is already loaded, so there should be no delay.

** Note that killing AVPlayer may take more than just letting go of it and setting var to nil. Most likely, you also added a periodic time observer to track playback progress. When you do this, you will get back an opaque object that you must hold onto. If you do not remove this item from the player and release it / set it to zero, AVPlayer will not delete it. It looks like Apple is creating a deliberate save cycle, which you must manually chop. Therefore, before destroying AVPlayer, you need (example):

 [_player removeTimeObserver:_playerObserver]; [_playerObserver release]; //Only if you're not using ARC _playerObserver = nil; 

As an extra note, you can also set up NSNotifications (I use one to determine when a player completed the game) ... don't forget to delete them as well.

+1
source

If you need to use AVPlayer and AVAudioRecorder at the same time, follow these steps:

  • set the audio category for “playback and recording” (as described above or using the audio C session function)
  • "record" should be called after calling the "play" method with some delay. (I set 0.5 sec)

If you do not, playback will begin, but recording will not start.

+1
source

should not be [AVAudioRecorder record]; instead of play ?

0
source

I had the same problem: AVAudioRecorder did not start recording. My example is a little different: I have a tab-based application with several AVAudioPlayers in different views. For example, suppose the first loaded view has 3 AVAudioPlayers, and the second one AVAudioPlayer and one AVAudioRecorder. All of them are initiated in the viewDidLoad method.

The recorder did not work in the second mode. When I pressed the record button, the view slightly staggered and the recording did not start. Oddly enough, the simulator worked fine and did not show the same symptoms ... (who has an idea why?)

After some research and reading this topic, I realized that I did not set the players in the first view to zero, so I think they were still in memory so as not to start the recorder. Once I set them to the viewDidDisappear method, it works again.

here is what helped me:

 -(void)viewDidDisappear:(BOOL)animated { firstPlayer = nil; secondPlayer = nil; thirdPlayer = nil; } 

ViewDidUnload did not help, because View, and therefore the variables in the class itself were not unloaded when switching views.

Hope this can be helpful to others as well.

0
source

@ Aaron Hayman explains the exact results, but the solution for me was very simple. Just implement the player delegate didFinishPlaying method and release the player there.

 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ self.playerWord = nil; } 
0
source

All Articles