I'm recording it with AVAudioRecorder, recording it in the Document folder, and trying to play it back with AVAudioPlayer.Player.I put a project in GitHub that summarizes only the problems.
https://github.com/ueda-keisuke/AVAudioPlayerTest
Complexly, this code works correctly in the simulator.However, the actual device (iPad Air 2) does not play the audio in the Document folder with the following error:
Error loading
file://var/mobile/containers/Data/Application/772E3C09-5D23-4153-A11B-125D00582759/Documentsaudio.m4a:
Error Domain= NSOSStatusErrorDomain Code=2003334207"(null)"
Or
Error loading
file://var/mobile/containers/Data/Application/93108627-9DBE-453E-8D93-746E65DA48EE/Documents/F32D2D55-1978-427F-AF79-4F042C8B51F5/87.m4a:
Error Domain= NSOSStatusErrorDomain Code=1685348671"(null)"
There are three buttons.
Play audio recorded by simulator
Press this button to play the audio file on the Bundle (which contains clapping sounds).This audio file ran this program in the simulator, recorded it, took it out of the Document folder, and registered it in the project.In other words, I believe that the recording itself is successful.Also, AVAudioPlayer is not unable to play this file.
Record
Press to start recording, and press again to stop recording.Generate the file in the Document folder and overwrite any existing files.
Play
Play the audio you created in the Document folder.The simulator makes a recorded sound, but the actual machine makes an error and does not make a sound.
All of them have a lot of information, but I couldn't find anything to solve them.
I would appreciate it if you could let me know if anyone knows the solution.Thank you for your cooperation.
ios
Apple's documentation is not always easy to understand and does not provide a Japanese version, but if you use AVAudioRecorder
, you will need to configure AVAudioSession
.
Multimedia Programming Guide
- Recording with the AVAudioRecorder Class
An example code is written in Objective-C without ARC as it is, but to get to the point:
AVAudioSessionCategory(_:)
in AVAudioSession
before starting recording.true
in AVAudioSession
setActive(_:) before starting recording as wellfalse
in AVAudioSession
setActive(_:).Not found in this document, but if you want to play recorded data in AVAudioPlayer
after the above:
AVAudioSessionCategory(_:)
in AVAudioSession
before starting playback.I think I have to do what I said.
As for your Github code,
After audioRecorder?.stop()
in the record(_:)
method:
let session=AVAudioSession.sharedInstance()
try!session.setActive(false)
(It may be better to catch the error, but I'll simplify it for a quick overview here.)
After audioRecorder=try AVAudioRecorder(URL:url,settings:recordSettings)
in the setupAudioRecorder()
method:
let session=AVAudioSession.sharedInstance()
try session.setCategory (AVAudioSessionCategoryRecord)
try session.setActive(true)
Before audioPlayer=try AVAudioPlayer(contentsOfURL:url)
in the play(_:)
method:
let audioSession=AVAudioSession.sharedInstance()
tryaudioSession.setCategory (AVAudioSessionCategoryAmbient)
Try adding a code to the AVAudioSession
operation, as shown in .
Note that the error message indicates that you have fiddled with how to create the (?) file path during trial and error, but of course, be careful to point the file path for recording and playback to the same file under the Documents directory.
In this environment, the above modifications have definitely enabled playback, but it may not be exactly the same environment, so there may still be something.Please let me know if there is anything after trying.
© 2024 OneMinuteCode. All rights reserved.