Error '!dat' AvAudioPlayer - objective-c

I'm getting no sound and this error when I build ( Error '!dat' trying to set the (null) audio devices' sample rate). is something wrong with the music file or is it the code.
NSString *playM = [[NSBundle mainBundle] pathForResource:#"wildM" ofType:#"m4a"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:playM];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[player prepareToPlay];
player.currentTime = 0;
[player play];

I had the same problem. Here is what I did:
Close xcode and the iOS simulator
Unplug the headphone jack and any usb headphones you might have
Restart xcode and run your simulation (problem should be solved)
Plug back in any audio devices you want to use

Code looks ok. You don't have to call prepareToPlay, that just buffers things... nor do you need to set currentTime. More than likely your m4a file is incorrectly encoded.

Related

iOS not playing my sound?

I've went to COUNTLESS links and tutorials for this, but I either get no errors but no sound plays, or I get an error.
Using the code
NSString *toneFilename = [[NSBundle mainBundle] pathForResource:#"spoken" ofType:#"mp3"];
NSURL *toneURLRef = [NSURL fileURLWithPath:toneFilename];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL: toneURLRef error: nil];
player.currentTime = 0;
player.volume = 1.0f;
[player play];
My application runs, but doesn't play the sound on load. I was listening to music and tried the app, and it stopped my music as if it were going to play something in-app, but it never did.
Why is this happening?
If you're using ARC, and you declare an AVAudioPlayer as a local variable, the player will be dealloc'ed by ARC before it has a chance to play the sound. To fix the problem, declare the player as a property.

playing music by url

I want to play the music from internet by url.
I create a simply project that has one button with the following code:
NSURL *url = [[NSURL alloc] initWithString:#"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie play];
It works but with some delay (I think because it waits while file has been fully downloaded).
So what I need to do that the music starts to play immediately (when, for example, 10% of file has been downloaded)?
Use -[QTMovie autoplay] to automatically play the music once enough data has been downloaded.
In your case:
NSURL *url = [[NSURL alloc] initWithString:#"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie autoplay];
From the QTMovie class reference:
The autoplay method configures a QTMovie object to begin playing as soon as enough data is available that the playback can continue uninterrupted to the end of the movie.
If you can consider displaying a Quicktime window over your app, you can use this code :
NSString *url = #"http://someURL.mp3";
UIWebView* tempAudioPlayer = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[tempAudioPlayer loadHTMLString:[NSString stringWithFormat:#"<iframe frameborder=\"0\" width=\"0\" height=\"0\" src=\"%#\"></iframe>", url] baseURL:nil];
[self addSubview:tempAudioPlayer];
I first create a UIWebView which will not be displayed. Then I loadHTMLString a <iframe> in it, with the URL of my file as the src value. And I add it to my view. Quicktime appears immediately.

AVAudioPlayer Won't Play Repeatedly

I have a relatively simple app that plays a sound using AVAudioPlayer, like this:
NSURL *file3 = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/ball_bounce.aiff", [[NSBundle mainBundle] resourcePath]]];
player3 = [[AVAudioPlayer alloc] initWithContentsOfURL:file3 error:nil];
[player3 prepareToPlay];
to later be called to play, like this:
[player3 play];
But this sound is called in multiple places (collisions with a bouncing ball), many times. Sometimes, the ball hits two things in quick enough succession where the sound from the first bounce is still playing when the ball collides a second time. This second bounce then does not produce a sound. How can I make it play a sound every time it collides, even if it involves paying a sound that overlaps an already playing sound?
Any help is greatly appreciated.
One way to solve this problem would to be create a new instance of AVAudioPlayer if the previous one is already playing:
if([player3 isPlaying]) {
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[player3 url]];
[newPlayer setDelegate:self];
[newPlayer play];
}
Then, in the delegate method audioPlayerDidFinishPlaying:successfully:, you should send the player the -release message:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
if(player != player3) {
[player release];
}
}
This is just the way it has to be, as you do need a new source buffer for each concurrent sound playing. :(
It is an old thread but maybe someone can benefit from this answer. Creating AVAudioPlayer repeatedly can be a burden for the app and can creates lags. In this case you can use SystemSound.
NSString *pathToMySound = [[NSBundle mainBundle] pathForResource:#"yourMusic" ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath: pathToMySound]), &soundID);
AudioServicesPlaySystemSound(soundID);
Downside of this solution, you can't adjust the volume.

Waiting until another sound is finished before playing a new one

I am developing an iPhone application that plays sounds. I do not want interrupt or play another sound, if a sound is already playing, regardless of where it was "spawned" from to play. My code is as follows:
if(distance <= 10)
{
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound(soundID);
}
Simply, I do not want to play a new sound if one is already playing.
I do not want to queue another sound to play if one is already playing.
Is this possible?
I suggest you use the AVAudioPlayer API rather than AudioServicesPlaySystemSound. It has a delegate which you can use, which defines a method -audioPlayerDidFinishPlaying:successfully: which will tell you when your sound finishes playing. The AVPlayer class also has a -playing method which you can check to see if a sound is currently playing.
So, in other words, do this instead:
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundPath error:&error];
if(!player)
[self doSomethingWithError:error];
[player setDelegate:self];
[player play];

Playing one mp3 file (in loop) in OS X 10.5 or higher

How to play an mp3 file, on repeat, on OS X 10.5 or higher?
Please reply with a code snippet. Thanks.
NSSound *sound = [[NSSound alloc] initWithContentsOfURL:url byReference:YES];
[sound setLoops:YES];
[sound play];
// Later, when finished with sound
[sound release];