How to use AVCapturePhotoOutput

Updated to Swift 4 Hi it’s really easy to use AVCapturePhotoOutput. You need the AVCapturePhotoCaptureDelegate which returns the CMSampleBuffer. You can get as well a preview image if you tell the AVCapturePhotoSettings the previewFormat class CameraCaptureOutput: NSObject, AVCapturePhotoCaptureDelegate { let cameraOutput = AVCapturePhotoOutput() func capturePhoto() { let settings = AVCapturePhotoSettings() let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first! let … Read more

How to play a sound using Swift?

Most preferably you might want to use AVFoundation. It provides all the essentials for working with audiovisual media. Update: Compatible with Swift 2, Swift 3 and Swift 4 as suggested by some of you in the comments. Swift 2.3 import AVFoundation var player: AVAudioPlayer? func playSound() { let url = NSBundle.mainBundle().URLForResource(“soundName”, withExtension: “mp3”)! do { … Read more

Can use AVCaptureVideoDataOutput and AVCaptureMovieFileOutput at the same time?

I can’t answer the specific question put, but I’ve been successfully recording video and grabbing frames at the same time using: AVCaptureSession and AVCaptureVideoDataOutput to route frames into my own code AVAssetWriter, AVAssetWriterInput and AVAssetWriterInputPixelBufferAdaptor to write frames out to an H.264 encoded movie file That’s without investigating audio. I end up getting CMSampleBuffers from … Read more

How can I reduce the file size of a video created with UIImagePickerController?

With AVCaptureSession and AVAssetWriter you can set the compression settings as such: NSDictionary *settings = @{AVVideoCodecKey:AVVideoCodecH264, AVVideoWidthKey:@(video_width), AVVideoHeightKey:@(video_height), AVVideoCompressionPropertiesKey: @{AVVideoAverageBitRateKey:@(desired_bitrate), AVVideoProfileLevelKey:AVVideoProfileLevelH264Main31, /* Or whatever profile & level you wish to use */ AVVideoMaxKeyFrameIntervalKey:@(desired_keyframe_interval)}}; AVAssetWriterInput* writer_input = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:settings]; Edit: I guess if you insist on using the UIImagePicker to create the movie in the … Read more

Looping a video with AVFoundation AVPlayer?

You can get a Notification when the player ends. Check AVPlayerItemDidPlayToEndTimeNotification When setting up the player: ObjC avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[avPlayer currentItem]]; this will prevent the player to pause at the end. in the notification: – (void)playerItemDidReachEnd:(NSNotification *)notification { AVPlayerItem *p = [notification object]; [p seekToTime:kCMTimeZero]; } this will rewind … Read more

How to play audio in background with Swift?

You need to set your app Capabilities Background Modes (Audio and AirPlay) and set your AVAudioSession category to AVAudioSessionCategoryPlayback and set it active From Xcode 11.4 • Swift 5.2 do { try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay]) print(“Playback OK”) try AVAudioSession.sharedInstance().setActive(true) print(“Session is Active”) } catch { print(error) }

Making the iPhone vibrate

From “iPhone Tutorial: Better way to check capabilities of iOS devices“: There are two seemingly similar functions that take a parameter kSystemSoundID_Vibrate: 1) AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); 2) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); Both of the functions vibrate the iPhone. But, when you use the first function on devices that don’t support vibration, it plays a beep sound. The second function, on … Read more

How to play video with AVPlayerViewController (AVKit) in Swift

Swift 3.x – 5.x Necessary: import AVKit, import AVFoundation AVFoundation framework is needed even if you use AVPlayer If you want to use AVPlayerViewController: let videoURL = URL(string: “https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4”) let player = AVPlayer(url: videoURL!) let playerViewController = AVPlayerViewController() playerViewController.player = player self.present(playerViewController, animated: true) { playerViewController.player!.play() } or just AVPlayer: let videoURL = URL(string: “https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4”) … Read more