Music Not Playing - XCode - ios

I have this code to play sounds:
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) #"audio2", CFSTR ("mp3"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
It works fine for an audio file that I have that is about 1 second long. It does not, however, work for one of my files that is about 4 minutes long. How can I fix this? They're both mp3 files and placed in the same directory.

According to the apple's doc, AudioServicesPlaySystemSound has a limits as below:
Sound files that you play using this function must be:
- No longer than 30 seconds in duration
You can use AVAudioPlayer instead.

Related

Sound not playing when iPhone is in silent mode

I am making an app that needs to play sounds even if the iPhone is in silent mode, but with the code I have now (look at bottom of this post) it wont't play sounds if the iPhone is in silent mode.
I am using the following code:
Viewcontroller.h:
#interface SoundViewController : UIViewController <UICollectionViewDataSource, UIApplicationDelegate, AVAudioPlayerDelegate, ADBannerViewDelegate, UIActionSheetDelegate> {
SystemSoundID SoundID;
NSString *listPath;
}
ViewController.m:
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (__bridge CFStringRef) [mainArray objectAtIndex:indexPath.row], CFSTR("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
The way you are triggering sounds using AudioServicesPlaySystemSound will ALWAYS obey the ringer/silent switch.
To play sound when the device is set to silent you need to use another way such as AVAudioPlayer class.

iOS sound plays while muted

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.

Configure TableViewCell to play audio

I have an app that currently plays a 5 sec sound when a button is pressed within a view controller. I am trying to change this set up to where if I have a tableview, a specific cell is touched and the sound plays. I do not want an action where you select the cell and it pushes to another window to play the sound. I simply want the sound to play on touch of the cell.
Currently for my button set up I had the following to play the sound in my view controller:
In the SoundLibViewController.h
// Identifying UIButton associated with sound file
-(IBAction)CarHorn:(id)sender;
#end
In the SoundLibViewController.m
-(IBAction)CarHorn:(id)sender {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) #"CarHorn", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
Unfortunately I can not find any help online to point me in the direction to change my code into incorporate a tableviewcell.
To give an example of what I'm looking to do: You go into your iPhone/iPad and under settings you select "Sounds". Then you choose "Ringtone", which takes you to a list of sounds. If you press the cell related to the sound it plays. THAT is what I want to do.
You need something like this in your UITableViewDelegate
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * soundName = [self.soundNames objectAtIndex: indexPath.row]; // Assuming you have an array of sound names
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef)soundName, CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}

playing audio when apps running

I've been trying lately to get my app to play a sound from when it open to when its closed and I'm have difficulty. I have some code on playing a sound when I press a button and it's fine I'm just wondering if there is any tweaks I could do to make it play on its own and from the start or is there a way I could say press play and make the sound loop why I'm playing the game.
.h
#import <AudioToolbox/AudioToolbox.h>
- (IBAction)t1:(id)sender;
.m
- (IBAction)t1:(id)sender {
CFBundleRef mainbundle = CFBundleGetMainBundle();
CFURLRef soundFileUrlRef;
soundFileUrlRef = CFBundleCopyResourceURL(mainbundle, (CFStringRef) #"t1", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileUrlRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
how about trying AVAudioPlayer and turning on the continuous playback by setting number of loops to -1:
myPlayer.numberOfLoops = -1
[myPlayer prepareToPlay];
[myPlayer play];
//when application did enter background or something
[myPlayer stop];
http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html
Also make sure your Audio Sessions are set properly for background playback:
http://developer.apple.com/library/ios/#DOCUMENTATION/Audio/Conceptual/AudioSessionProgrammingGuide/Basics/Basics.html#//apple_ref/doc/uid/TP40007875-CH2-SW2

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