我已经使用
ReplayKit
来录制我的应用程序的屏幕。我也使用了microphoneEnabled = true
。设备第一次请求使用麦克风录音的许可。它还可以用音频录制屏幕。
但是下次我尝试录制屏幕时,文件中没有声音。
我附上了两种方法,一种是开始录音,另一种是停止录音。
func startRecording(withFileName fileName: String, recordingHandler:@escaping (Error?)-> Void)
{
if #available(iOS 11.0, *)
{
let fileURL = URL(fileURLWithPath: ReplayFileUtil.filePath(fileName))
assetWriter = try! AVAssetWriter(outputURL: fileURL, fileType:
AVFileType.mp4)
currentVideoURL = fileURL
let videoOutputSettings: Dictionary<String, Any> = [
AVVideoCodecKey : AVVideoCodecType.h264,
AVVideoWidthKey : floor(UIScreen.main.bounds.size.width*1.2/16)*16,
AVVideoHeightKey : floor(UIScreen.main.bounds.size.height*1.2/16)*16
];
videoInput = AVAssetWriterInput (mediaType: AVMediaType.video, outputSettings: videoOutputSettings)
let audioSettings = [
AVFormatIDKey : kAudioFormatMPEG4AAC,
AVNumberOfChannelsKey : 2,
AVSampleRateKey : 44100.0
//AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue
//AVEncoderBitRateKey: 192000
] as [String : Any]
audioAssetWriterInput = AVAssetWriterInput(mediaType: AVMediaType.audio, outputSettings: audioSettings)
audioAssetWriterInput.expectsMediaDataInRealTime = true
videoInput.expectsMediaDataInRealTime = true
assetWriter.add(videoInput)
assetWriter.add(audioAssetWriterInput)
guard recorder.isAvailable else {
print("Recording is not available at this time.")
return
}
self.recorder.isMicrophoneEnabled = true
recorder.startCapture(handler: { (sample, bufferType, error) in
// print(sample,bufferType,error)
recordingHandler(error)
if CMSampleBufferDataIsReady(sample)
{
if self.assetWriter.status == AVAssetWriterStatus.unknown
{
self.assetWriter.startWriting()
self.assetWriter.startSession(atSourceTime: CMSampleBufferGetPresentationTimeStamp(sample))
}
if self.assetWriter.status == AVAssetWriterStatus.failed {
print("Error occured, status = \(self.assetWriter.status.rawValue), \(self.assetWriter.error!.localizedDescription) \(String(describing: self.assetWriter.error))")
return
}
if (bufferType == .video)
{
if self.videoInput.isReadyForMoreMediaData
{
self.videoInput.append(sample)
}
}else if (bufferType == .audioMic)
{
if self.audioAssetWriterInput.isReadyForMoreMediaData
{
self.audioAssetWriterInput.append(sample)
}
}
}
func stopRecording(handler: @escaping (Error?) -> Void)
{
if #available(iOS 11.0, *)
{
recorder.stopCapture
{ (error) in
handler(error)
self.assetWriter.finishWriting
{
print(ReplayFileUtil.fetchAllReplays())
UISaveVideoAtPathToSavedPhotosAlbum(self.currentVideoURL!.relativePath, self, nil, nil)
}
}
final class Player {
static var share = Player()
private var player = AVPlayer()
private init() {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.multiRoute)
} catch let error as NSError {
print(error)
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch let error as NSError {
print(error)
}
}
func play(url: URL) {
player = AVPlayer(url: url)
player.allowsExternalPlayback = true
player.appliesMediaSelectionCriteriaAutomatically = true
player.automaticallyWaitsToMinimizeStalling = true
player.volume = 1
player.play()
}
}
像这样使用音频播放器,
Player.share.play(url: url)
另一方面,通过启用
isMicrophoneEnabled
, 来开始屏幕录制
let screenRecorder = RPScreenRecorder.shared()
func startRecording() {
screenRecorder.isMicrophoneEnabled = true
screenRecorder.isCameraEnabled = false
screenRecorder.startRecording { err in
guard err == nil else {
print("Failed to start recording...")
return
}
print("Recording starts!")
}
}