How to sound iPhone's default shutter sound [duplicate] - ios

This question already has answers here:
Need to access the default camera shutter sound used by the iPhone
(2 answers)
Closed 5 years ago.
Currently, I use AVFoundation's AVCaptureVideoDataOutput to create a camera application.
It is a specification that I tap the shooting button and save the necessary frame as a still image on the camera roll.
(I do not save movies.)
About the shutter sound of the iPhone, I ask you a question.
・Question
What kind of coding should I do in order to sound the iPhone's default shutter sound?
At first, I created a camera application without shutter sound using AVCaptureVideoDataOutput.
Next, I would like to create a camera with shutter sound using AVCaptureVideoDataOutput.
I do not know how to sound the iPhone's default shutter sound while using AVCaptureVideoDataOutput.
Is there a way to forcibly sound the iPhone's default shutter sound?
■Supplement
xcode8.3.3
swift3.1
Deployment Target:10.2
I'd like to implement AVCaptureVideoDataOutput because I want to implement both movie shooting and still shooting in the future.

Yes,
use this:
AudioServicesPlaySystemSound(1108);
You can check all full sounds here:
Sounds
For a custom sound:
AVAudioPlayer *audioPlayer;
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Name of your audio file"
ofType:#"type of your audio file example: mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
Make sure you import
<AVFoundation/AVFoundation.h>

Related

AVPlayer video preview issue iOS 14.0.1

I am trying to play local and remote videos using AVPlayer and AVPlayerViewController but I am facing an issue. The player is playing the audio for video but not showing the preview.
It is working fine on all the iOS versions less than iOS 14.0.1.
Please help me on this issue
Here is the sample code
movieUrl = [NSURL fileURLWithPath:filePath];
AVPlayer *player = [AVPlayer playerWithURL:movieUrl];
theMoviPlayer = [[AVPlayerViewController alloc] init];
theMoviPlayer.player = player;
[self addSubview:theMoviPlayer.view];
theMoviPlayer.view.frame = self.bounds;
[theMoviPlayer.player play];
Look at this image for better understanding
I have found the actual reason of this issue. It seems that for certain non-standard encoding or fps, AVPlayer is not able to play video. I have exported the same video in correct encoding using some tool i.e, QuickTimePayer, and the video is playable now.
This issue is only producing on iOS 14.0.1, may be it is a bug/feature in AVPlayer in this update.
Conclusion:
Always check your video encoding if your video is not playing or you are just hearing only audio without any video frames.

AVPlayer removes background music

I've been using giffycat to decode, store, and play gifs in my app. I am making it so that it can easily load a gif in a UICollectionView's cell, so I have decided for each gif model to have its own AVPlayer. I have noticed that simply by creating an AVPlayer, shown bellow, audio from other apps is killed! Annoying for both the user and the creater!
// Create an AVURLAsset with an NSURL containing the path to the video
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:_mp4] options:nil];
// Create an AVPlayerItem using the asset
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
_player = [AVPlayer playerWithPlayerItem:item]; //if this line is commented out, I hear audio, else audio from Spotify is quickly killed...
Since these videos are just gifs, I am wondering if there is some way to unassign the audio session. I do not know much about this. ples help!
Turns out the answer is pretty easy, after a little googling and documentation reading...
The solution is
// audio session
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setCategory(AVAudioSessionCategoryAmbient,
withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
oops, just realized I am posting my question in objC and answer in Swift. Well tough, because that's life sometimes.
AudioSession is a singleton for your entire app to rule how your application mingles with the other sounds of the system and other apps! The default audio session is
playback is enabled, recording is disabled
when user moves silent switch to "silent" your audio is silenced
when user presses sleep/wake button to lock screen or auto-lock period expires, your audio is silenced
when your audio starts, other audio on device (music) is silenced.
CategoryAmbient tells it not to do 4
Nice documentation!
https://developer.apple.com/library/ios/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/ConfiguringanAudioSession/ConfiguringanAudioSession.html#//apple_ref/doc/uid/TP40007875-CH2-SW1
Set the audioMix property of the AVPlayerItem to nil before creating an AVPlayer from it to remove the audio track from the asset.

iOS: how to conveniently store and play many mp3 files

I have a program with about 2000 short mp3 files. I am now storing all those file into folder Supporting Files and when I want to play I call this function:
-(void)playSound:(NSString *)mySoundFileName{
NSString *filePath = [[NSBundle mainBundle] pathForResource:mySoundFileName ofType:#"mp3"];
if ([NSData dataWithContentsOfFile:filePath]) {
url = [NSURL fileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];
}
}
However, the first time I play the sound, it always takes long time to search/load the file. More specifically, after pressing "play sound" button to play sound, I have to wait for at least 5 seconds until it plays. It is OK to play other sound after that, i.e, it play almost immediately when I press "play sound" button. Do you have any suggestion to store and play those many files more efficiently? Thank you very much
It can sometimes take an undesirable amount of time for AVAudioPlayer to start playing initially. A good way to solve this is to make the initial alloc/init before you call play. This way the player is ready to play before the user presses the play button. Additionally, calling [player prepareToPlay]; before play will help improve performance slightly.

MPMoviePlayerController is not responding to device volume controls

I'm working on a game that during the title sequence plays a video in the background using MPMoviePlayerController. I overlay my game controls over this (just a few textured UIButtons).
The video itself has no audio, but I'm playing sounds when I press buttons via OpenAL.
The Audio Session is set to "Ambient" and whenever the MPMoviePlayerController is not around it responds correctly to device's mute button and volume. But as soon as the video starts playing it blares out the sounds with no regard to the mute or volume settings.
Can anyone help me? Is the MPMoviePlayerController interfering with the AudioSession state?
Is there a way to stop this from happening. My movie has no sound in it so it shouldn't need to do that.
The MPMoviePlayerController uses the AVAudioPlayer shared instance. So you can literally set the volume of the MPMoviePlayerController and it will turn down your background music. However, a better way is to tell MPMoviePlayerController to not use the shared instance.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"intro" ofType:#"m4v"]];
self.player = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.player.movieSourceType = MPMovieSourceTypeFile;
**[self.player setUseApplicationAudioSession:FALSE];**

iOS audio samples play only on earphone output

I'm developing a program that plays a short sample when a button is pressed. The problem is that it only plays through earphone output and not through the device's speaker. I've tried .wav and .aiff and AVAudioPlayer and SystemSoundID. On the simulator I can hear the sound. I'm using iPod Touch 4th gen. running iOS 4.1. Example code:
NSString *soundFile = [[NSBundle mainBundle] pathForResource:#"button" ofType:#"wav"];
NSURL *url = [NSURL fileURLWithPath:soundFile];
AudioServicesCreateSystemSoundID( (CFURLRef)url, &buttonID);
AudioServicesPlaySystemSound( buttonID );
Edit (solution found): I tried with another .wav file and it worked. Odd, because the original .wav's format is supported by iOS.
Sounds like you've solved the issue, but as a heads-up, you should only be using SystemSoundID style playback for user interface related sounds.
If the audio is a key part of your app, you need to set the appropriate AVAudioSession category and use (for example) the AVAudioPlayer playback methods.
Then again, this might not be relevant. :-)

Resources