iOS sound plays while muted - ios

I am making a learn-the-alphabet app for toddlers and I have encountered a problem with the sound output. Every sound the app plays when testing on my ipad has the same volume whether the device's sound is set at maximum or mute. In other words, the same sound level comes out regardless of the device's volume setting.
The code I use to play sounds is (using the A-sound as an example):
- (IBAction)aSpill:(id)sender {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) #"aLyd", CFSTR("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
Any help is much appreciated! :)

Please see the answer here AudioServicesPlaySystemSound Volume?
It explains that system sounds don't always use the system volume.
I suggest using AVAudioPlayer to play your sound instead.

So from here : http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html
Try following the code using the below kind of code.
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: #"sound"
ofType: #"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
[fileURL release];
self.player = newPlayer;
[newPlayer release];
[player prepareToPlay];
It is about halfway down the page.

Related

App Audio Not Playing

So in my file I have the following code with the intention that a music file plays in the background :
-(void) BackGroundMusic {
NSString *soundPath = [[NSBundle mainBundle]pathForResource:#"LostInTheSea" ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound(soundID);
}
And then in the view did load section I have :
[self BackGroundMusic];
I have all the toolboxes added such as AVFoundation and AudioToolbox but i can not hear any audio playing . Please help !
You should not be using system sound to play mp3 files as user523234 explains.
Use AVAudioPlayer instead. Here's an example that will loop infinitely:
#import "AVFoundation/AVAudioPlayer.h"
#import "AVFoundation/AVAudioSession.h"
-(void)playbgMusic {
AVAudioPlayer *soundbgMusic;
NSString *soundPath;
soundPath= [[NSBundle mainBundle] pathForResource:#"bgMusic" ofType:#"mp3"];
soundbgMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
soundbgMusic.volume = 1.0;
soundbgMusic.numberOfLoops = -1;
[soundbgMusic prepareToPlay];
soundbgMusic.currentTime = 0.0;
[soundbgMusic play];
}
According to the document for AudioServicesPlaySystemSound:
Sound files that you play using this function must be:
No longer than 30 seconds in duration
In linear PCM or IMA4 (IMA/ADPCM) format
Packaged in a .caf, .aif, or .wav file

Volume on my device doesn't effect the volume

.h
SystemSoundID sound1;
.m
NSURL *soundURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Woosh" ofType:#"mp3"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL1, & sound1);
AudioServicesPlaySystemSound(sound1);
This is the only code i use to play my Woosh.mp3 sound. I ran the app on my iPad and when i changed the volume on the device, the volume of the sound didn't change. I was wondering if there might be another way to implement sound into the app, or add the capability to change volume to this way.
NSURL *soundurl = [[NSBundle mainBundle] URLForResource: #"mysound" withExtension: #"caf"];
AVAudioPlayer *mySoundPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:soundurl error:&error];
mySoundPlayer .volume=1.0f; //between 0 and 1
[mySoundPlayer prepareToPlay];
mySoundPlayer.numberOfLoops=0; //or more if needed
[mySoundPlayer play];

Looping Audio with AudioToolbox.framework

I have would like to play audio on the startup of my app, but, first of all, the audio doesn't play! No matter how i code it. And secondly I would love to loop it if i can.
#implementation ViewController
-(void)awakeFromNib
{
SystemSoundID soundID;
NSString *soundFile1 = [[NSBundle mainBundle] pathForResource:#"ScaryMusic" ofType:#"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile1], &soundID);
AudioServicesPlaySystemSound(soundID);
NSLog(#"Sound Played");
}
Try this instead:
#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *player;
NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:#"ScaryMusic" withExtension:#"mp3"];
if (soundFileURL != nil)
{
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //infinite
}
[player play];
This is better for mp3's and such. SystemSound is mostly reserved for short .wav type sounds.
If you look in the docs you'll find that mp3 is not among the formats supported by AudioToolbox

App sounds doesn't sound with ringer off even with kAudioSessionCategory_MediaPlayback

I'm making a soundboard app, everything goes fine but I have one issue, the sounds doesn't play if the ringer is off.
I have tried this:
[super viewDidLoad];
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];
I'm using the AVFoundation Framework
The code to play the button is this:
- (IBAction)queChin:(id)sender {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) #"chin", CFSTR ("m4a"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
I have searched this topic but I couldn't find any answer that works. Maybe I'm missing something?
As alternative I can put an alert to the user saying "hey, turn your ringer on" but I don't know how to detect if the phone is silenced.
Any hints?
Thank you.
Well, I found out, you can't play System sounds with AudioServicesPlaySystemSound even if you initialize an Audio Session using this:
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];
System sounds are meant to remain silent if the user mutes the device.
I ended up using AVAudioPlayer and that solved the problem.
Old code using System Sound:
- (IBAction)queChin:(id)sender {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) #"chin", CFSTR ("m4a"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
CFRelease(soundFileURLRef);//prevents memory leak
AudioServicesPlaySystemSound(soundID);
}
New code using AVAudioPlayer:
- (IBAction)queChin:(id)sender {
NSURL *url = [[NSBundle mainBundle] URLForResource:#"chin" withExtension:#"wav"];
_chingosound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
_chingosound.delegate = self;
[_chingosound prepareToPlay];
[_chingosound play];
}
You MUST initialize the audio session to play sounds with the ringer off.

How to loop audio on a specific View Controller

-(void)viewDidAppear:(BOOL)animated
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) #"Intro",
CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundID);
AudioServicesPlaySystemSound (soundID);
}
I'm a beginner with this. I'm trying to play a wav file in the background of the view controller. I originally made a 30 second wav file, it wouldn't play, I reduced the length of the audio file to 10 seconds and it plays fine on the view controller. My question is, how to loop this audio file and/or play a longer audio file. Also, will the audio stop when I leave the view controller.
I am using xCode 4.3
I am running the app on a iPhone4 & 4s (iOS 5.1)
Thanks in advance...
Apple Documentation states
http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html
You can use System Sound Services to play short (30 seconds or shorter) sounds. The interface does not provide level, positioning, looping, or timing control, and does not support simultaneous playback: You can play only one sound at a time. You can use System Sound Services to provide audible alerts. On some iOS devices, alerts can include vibration.
So no loop is available when using system sounds.
You can check out this tutorial
http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
}
It states that if you set the numberOfLoops property to a negative number causes the audioPlayer to loop indefinitely
And dont forget to add the AVFoundation framework to your project

Resources