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. :-)
Related
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>
From what I understand it is currently not possible to play a sound from your watch kit app on the watch. If this is the case what is the best way to play a sound? Currently I am using openParentApplication:reply: to run code on the phone that plays the sound. Is this the best way?
Update: I also found that I can play audio directly from the watch extension using the same methods as in the parent app. No Idea if this will actually play when not on the simulator.
There currently isn't any way to play audio on the Watch. Your assumptions are 100% correct. Apple doesn't want to have to transfer the audio clips from the Watch Extension to the Watch at runtime. Currently, the only supported transfer caching system is for images. For now, you will have to play the audio on the iOS device. You can use one of the following approaches to trigger playing the sound:
openParentApplication:reply: - opens app in the background if not currently running
Darwin notifications through something like MMWormhole
Playing the sound from the Watch extension is certainly a gray area. My guess is that it will play on the iOS Device or not play at all when testing with devices. I would certainly advise against that approach though. Your Watch Extension will typically only be open for a VERY short period of time. It would be a much better idea to play the audio from the iOS app since that is guaranteed to continue running.
As of watchOS 2, you can play audio files using WKAudioFilePlayer on the watch. It appears that mp3 files are not supported, but caf and wav are. Copy the sound file to the extension target.
Setup:
NSURL *assetURL = [[NSBundle mainBundle] URLForResource:#"file" withExtension:#"wav"];
WKAudioFileAsset *asset = [WKAudioFileAsset assetWithURL:assetURL];
WKAudioFilePlayerItem *playerItem = [WKAudioFilePlayerItem playerItemWithAsset:asset];
WKAudioFilePlayer *audioFilePlayer = [WKAudioFilePlayer playerWithPlayerItem:playerItem];
Play:
if (audioFilePlayer.status == WKAudioFilePlayerStatusReadyToPlay) {
[self.audioFilePlayer play];
}
Alternatively, you can use the WKInterfaceMovie class:
[self presentMediaPlayerControllerWithURL:assetURL options:nil completion:nil];
I'm trying to build a custom keyboard for iOS 8 that a custom play sound when a key is pressed. I'm using a AVAudioPlayer and small mp3-files.
This works fine in the simulator, but on a real device I don't get any sounds.
RequestOpenAccess is enabled.
Please not that I'm not trying to play the default keyboard click sound, but a custom sound. Most existing questions seems to be about the default system sound.
edit: I've also tried using .caf and .aiff files and loading and playing them with SystemSoundID.
This snippet plays custom sound in my keyboard project.
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"your_custom_sound"
ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
Cited from:
How to play tock sound when tapping custom keyboard buttons
I'm positive this is not your case but it often happens that the sound on the device is turned off. I made the mistake searching for an hour why sound didn't work. If it could only be that, it would be a quick fix ;)
I have an audio file that is 2 seconds long. Im trying to play that many times before it is done playing. But when I try it with AVAudioPlayer by creating a new instance of it, it sounds like it stops the audio that was played before and plays the new one instead of playing them at the same time. Should I use another way to play sounds or is this not achievable?
Code:
NSURL *url = [[NSBundle mainBundle] URLForResource:#"PistolShootSound" withExtension:#"mp3"];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer play];
audioPlayer is declared in the header file.
Have you used AVAudioPlayer's property numberOfLoops to play audio file repeatedly?
UPDATE: I didn't understand your goal before. If you want to play short files many times you could use AudioServices. Here is a good example of how to use it.
in my application I use AudioServicesPlaySystemSound to play little caf files.
When run my app on the iPhone and I change volume by lateral buttons, the app's sound changes
too, but on iPad the sound's volume in my app is always the same.
Maybe because on the iPhone is the ring volume, instead on the iPad is device volume.
How can I obtain same iPhone behavior on iPad?
Excuse me for my bad English....
I had the same problem but if you change the system sounds volume with buttons all the system sounds will be modified along with your app's. I found this really annoying.
The solution is to use AVAudioPlayer in place of AudioServices: as easy, but way more versatile. And there you can finely tune the volume for each sound, programmatically.
NSURL *soundurl = [[NSBundle mainBundle] URLForResource: #"mysound" withExtension: #"caf"];
AVAudioPlayer *mySoundPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:soundurl error:&error];
mySoundPlayer .volume=0.4f; //between 0 and 1
[mySoundPlayer prepareToPlay];
mySoundPlayer.numberOfLoops=0; //or more if needed
[mySoundPlayer play];