So you want to record a video and play music from the user's library at the same time? Look no further. Below is the answer.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
For the audio playback you will use
AVAudioPlayer
. All you have to do is to declare theAVAudioPlayer
as a global variable (I named it audioPlayer) and implement the code below.Use this in after the user chose the song he/she wants to play:
You will also need to set up the audio session(in
viewDidLoad
). It's crucial if you want audio to play while recording:Now for the video recording. You will use
AVCaptureSession
. Declare the following as global variables:Then configure the session in
viewDidLoad
. Note: The video preview is in a container and the entire video related code is in a different view controller but just using a view instead of a container should work just as fine:Then you create an
@IBAction
for handling when the user presses the record button (I just used a simple button which I made red and round):Then all there is left for you to do is to save the video to (presumably) the camera roll. But I won't include that. You must put in some effort yourselves. (hint:
UISaveVideoAtPathToSavedPhotosAlbum
)So that's it folks. That's how you use
AVFoundation
to record a video and play music from the library at the same time.Once you set the AVAudioSession just like below, it'd work well.
try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.videoRecording, options: AVAudioSession.CategoryOptions.mixWithOthers)