Audio in XCode 5 does not play - ios

I am relatively new to coding, and I was trying to make my own game in XCode. I do not have a developer's license yet, so I don't think I can put the game on my phone to try it out. I found several different ways to play sounds, and I wanted to put a 30 second song I made in garageband in the background to see how it works. I tried it with a .mp3 file, but that did not work. I found more resources on how to put a .wav file in, so I converted it and tried with that. I followed several different tutorials and instructions exactly, but none of them worked. Finally I ended up doing it like this and trying to run it, but it did not play in the simulator. I have tried unchecking and checking the Play User Interface Sound Effects button in System Preferences like people said, but it did not work. I used AudioToolbox framework. Here is my code for the music:
.h
#import <AudioToolbox/AudioToolbox.h>
SystemSoundID BackgroundMusicID;
.m (I did not #import anything into the .m, but I tried it) The file name for the music is "BackgroundMusic.wav" exactly.
- (void)viewDidLoad
{
NSURL *BackgroundMusicURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"BackgroundMusic" ofType:#"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)BackgroundMusicURL, &BackgroundMusicID);
AudioServicesPlaySystemSound(BackgroundMusicID);
}
Thank you for any help! If you have any ideas as to why it won't play, or if it is just a problem with the simulator (I saw other people saying that, but none of their fixes worked unless I did something wrong). I would like the song to play as soon as the game loads up, but I have tried putting AudioServicesPlaySystemSound(BackgroundMusicID); under a button and under touchesbegan. Again, any help is appreciated!

Your code looks fine. But try the following:
NSString *BackgroundMusicURL = [[NSBundle mainBundle] pathForResource:#"BackgroundMusic" ofType:#"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:BackgroundMusicURL], &soundID);
AudioServicesPlaySystemSound(BackgroundMusicID);
Another thing is:
Don't try creating a clip that is exactly 30 seconds in length, instead try it first with 25-second or a 28-second clip.
And if that still does't work, please provide more code specifically where your dispose your sound and if you ave animation code, please post those as well.

Try changing the NSURL to use: URLForResource: withExtension: instead of fileURLWithPath:
- (void)viewDidLoad
{
NSURL *BackgroundMusicURL = [[NSBundle mainBundle] URLForResource:#"BackgroundMusic"
withExtension:#"wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)BackgroundMusicURL, &BackgroundMusicID);
AudioServicesPlaySystemSound(BackgroundMusicID);
}

Related

play a sound from an arbitrary position

I'm looking for a way to play an audio file from just any point in the file.
Currently, whenever I play the file, it just starts from the beginning.
Since it is a loop, I could choose to let it loop, and open/close the volume at will, but I think it is kind of a lame solution, not in the least because of battery concerns.
I really did some research on this, but it is either a freak requirement, or I don't know how to phrase my queries.
Thanks in advance for any tip.
That should be a nice easy problem to solve. You don't post any code, so we can't see what you're trying at the moment. What I'd do is this:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"SoundFile" ofType:#"wav"];
AVAudioPlayer * soundData =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
[soundData stop];
soundData.currentTime = 22; //This will start playback 22 seconds into the file
[soundData play];
It goes without saying that you need to check the duration of your sound file so that you don't select a time beyond its end!
currentTime is a float (so you can specify millisecond values). You can read more here: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/#//apple_ref/occ/instp/AVAudioPlayer/currentTime

How to stream mp3 files (iOS)

I have found many related questions on SO, but or because I am very new, or because it doesn't exactly fit my problem, I can't find a way to succeed.
So, all I would like to do is playing an audio file stored on an exernal server directly on the iOS device (iPhone in my case)
I have tried differents solution on the net, and this is what I get until now.
NSString* resourcePath = #"http://www.chiptape.com/chiptape/sounds/medium/laugh.mp3"; //your url
NSURL *url = [[NSURL alloc]initWithString:resourcePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithURL:url error:&error];
audioPlayer.delegate = self;
[url release];
[audioPlayer prepareToPlay];
[audioPlayer play];
The compiler show me this error:
No visible #interface for 'AVAudioPlayer' declares the selector
'initWithURL:error:'
I am not sure at all about my code, and would like your guys to know I have started iOS coding yesterday, so it's not impossible I'm missing something so obvious that tutorials/posts don't mention it.
Also, I have imported the library AVFoundation and imported it on my .h file.
I have also declared this on my .h file
AVAudioPlayer *audioPlayer;
I hope there is an easy way to stream audio file on iOS as provide android/java
Thank you and if you need more details comment and I'll update my post.
Try this:
NSString* resourcePath = #"http://www.chiptape.com/chiptape/sounds/medium/laugh.mp3"; //your url
NSURL *audioURL = [NSURL URLWithString:resourcePath];
AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL: audioURL error:&error]autorelease];
[audioPlayer play];
I'm working with it recently.
AVAudioPlayer can't work with audio stream. In fact, there isn't an easy way to do that.
You need to learn some Core Audio things, get the raw data of media, play them in audioQueue (audioToolBox framework). Real-time audio processing is not a simple work, it's full of c-level api and so many dirty work, don't rush, have fun :)
Check out the book Learning Core Audio and this open-source audio streamer.
Let me know if you need more help.

AVAsset from app sandbox

I'm working with AVAsset in my app and met next problem.
When I'm writing something like this:
AVAsset *audioAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/Hello.mp3", [[NSBundle mainBundle] resourcePath]]]];
its work fine. But when I trying get fresh recorded file from my tmp directory from in app's sandbox like that:
AVAsset *audioAsset2 = [AVAsset assetWithURL:[NSURL URLWithString:[NSTemporaryDirectory() stringByAppendingPathComponent:[#"speechRecord" stringByAppendingPathExtension:#"m4a"]]]];
it doesn't work too. Even if I trying add video file to asset from sandbox - result is the same.
I even tried work with AVURLAsset, but asset always empty too. I need it to mix two audio files between themselves and then merge it with recorded video. If I can do that without AVAssets and there is another way, I will appreciate if You tell me. Or may there is some another function for that?
You create the second URL for audioAsset2 with the URLWithString selector. Your first asset loaded correctly, because you used the fileURLWithPath selector.
If you want to load a file at a given URL always create the NSURL object with the fileURLWithPath selector.
So simply try:
AVAsset *audioAsset2 = [AVAsset assetWithURL:[NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[#"speechRecord" stringByAppendingPathExtension:#"m4a"]]]];

Playing short selfmade sound with immediate start in iOS

I have a self-generated DTMF sound (with a wav header) generated by program that I want to be able to play quickly, in fact as soon as the user touches a button. This DTMF sound must play/loop infinitely, until I stop it. Some other sounds must be able to be played at the same time.
I'm very new to Audio programming, and I tested many ways of doing that and I'm lost now.
How can I achieve that ?
Needs :
very quick playback start (including the first time)
many sounds at the same time (short sounds +- 2-6 seconds)
infinite DTMF sound without gaps
having control over the different sounds that are playing / being able to stop just one played sound
AVAudioPlayer if you can live with some latency, OpenAL (for example Finch) if you really need to have the latency as low as possible.
I use already exist .wav file. and I can easily play it.
For run following code. include AudioToolbox framework.
write into .h file #import <AudioToolbox/AudioToolbox.h>
Write into .m file
-(IBAction)startSound{
//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:#"%#%#", [[NSBundle mainBundle] resourcePath], #"/sound1.wav"];
//declare a system sound
SystemSoundID soundID;
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
//Use audio sevices to create the sound
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
//Use audio services to play the sound
[self sound];
timer =[ [NSTimer scheduledTimerWithTimeInterval:2.1 target:self selector:#selector(sound) userInfo:nil repeats:YES]retain];
}

iPhone SDK: Play a single WAV from a button

I am currently trying out this code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"dream" ofType:#"m4a"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
However, the SDK says that the ViewController does not implement the AVAudioPlayer Delegate.
Any body any ideas on how to play a WAV (or M4a) using the 2.2 SDK?
You need to add <AVAudioPlayerDelegate> to the end of your controller's class declaration, along these lines:
#interface MyViewController : UIViewController <AVAudioPlayerDelegate>
This'll probably generate some more warnings about delegate methods you need to implement - follow those and you'll be good to go.
Interesting. Try NSURL -URLWithString: - that's what I use in combination with the bundle method you're using. I don't see anything that could be causing a problem other than that. You're testing it on the device, right?
The answer I posted here should definitely help you. Just use audioservices for small wave sounds, unless you need concurrency, it's faster.
iPhoneToot.com has its own itootSound class and a VIDEO to show you how to play a wav file in 2 lines of code. Check them out at iPhoneToot.com. it will save you hours if not days of time!!

Resources