xcode cannot get sound file to play with AVAudioPlayer - xcode

I am new to iOS and trying to add sound to my app. I have followed several tutorials about adding a simple sound file to my project, but ALL with the same result. I get no errors, but no sound plays. Any ideas on what I am doing wrong? Any help would be appreciated!
- (IBAction)playSound:(id)sender {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(soundFilePath);
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
player.numberOfLoops = 1;
[player play];
}
**UPDATE***
So I never had any luck with the above code which is very discouraging, but I did end up getting it to work using the AudioToolbox Framework instead, using this:
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:#"test" ofType:#"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);
Can anyone explain to me the difference, why this one works and the other does not?? I am curious more than anything. Would love some clarification.

Finally figured it out. I needed to create a property for AVAudioPlayer in my header file in order to get it to work. Here is what I did:
// .h file
#interface ThirdViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
- (void) playSound //method in .m file
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test"
ofType:#"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer play];
}

Simplify your code by doing the follow. Also drop your test.mp3 to the root of your project directory in xcode. Your mp3 placement and location is very important. Also ensure volume is up, and vibrate is off of your device, and it's unmuted.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test"
ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];

Make sure you install quartz.h, or a specific framework that supports audio players. Just double check.
Had the same problem a while back and the framework must be installed properly.

Related

Can't play mp3 file in an infinite loop

I am trying to play a music file with eternal loop.
My code is:
NSString *path = [[NSBundle mainBundle] pathForResource:#"theme" ofType:#"mp3" inDirectory:#"assets/audios" ];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
audioPlayer.volume = 1.0;
if (audioPlayer == nil){
NSLog(#"Error creating player: %#", error);
}
[audioPlayer prepareToPlay];
[audioPlayer play];
But it does nothing. Nothing is played and any message log is printed.
The code below works great:
NSString *path = [[NSBundle mainBundle] pathForResource:#"theme" ofType:#"mp3" inDirectory:#"assets/audios" ];
NSURL *url = [NSURL fileURLWithPath:path];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)CFBridgingRetain(url), &soundID);
AudioServicesPlaySystemSound (soundID);
But I need to control the volume and the loop.
I dont know what Im doing wrong.
Can anyone help me?

video play in mac application using qtmovieview

I've written code like this
NSString *path = [[NSBundle mainBundle] pathForResource:#"iOS app development " ofType:#"mp4"];
NSLog(#" path %#",path);
NSURL *url = [NSURL fileURLWithPath:path] ;
movie1 = [QTMovie movieWithURL:url error:nil];
[Movieview setMovie:movie1];
[[Movieview movie]play];
when i run application only audio is playing. if any one know the answer please help me
thank you.
Add
[[movie1 window] makeKeyAndOrderFront:self];
It should fix your problem

Trying to add movie in xcode 4.6.1 but crash

I'm trying to add a video in xcode but it crashes: [NSURL initFileURLWithPath:]: nil string
(IBAction)Play:(id)sender {
/////
NSString *videoUrl=[[NSBundle mainBundle]pathForResource:#"tnween" ofType:#"mov"];
MPMoviePlayerController *player=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:videoUrl]];
[player play];
}
Maybe if you try:
NSString *videoUrl=[[NSBundle mainBundle]pathForResource:#"tnween" ofType:#"mov"];
NSURL *url = [NSURL urlWithString:videoUrl];
MPMoviePlayerController *player=[[MPMoviePlayerController alloc]initWithContentURL:url];

Can I add sounds to Xcode's Storyboard mode?

I'm new to creating apps for iOS and I'm using storyboard mode. I wanted to know how can I add sounds. Also, does it require coding?
You can use this for playback of simple sounds. It uses the AVFoundation framework, which will require you to add AVFoundation to your project, and use #import <AVFoundation/AVFoundation.h>
- (IBAction) myPlayAction {
AVAudioPlayer *data;
NSString *name = [[NSString alloc] initWithFormat:#"SoundName"];
NSString *source = [[NSBundle mainBundle] pathForResource:name ofType:#"mp3"];
if (data) {
[data stop];
data = nil;
}
data=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: source] error:NULL];
data.delegate = self;
[data prepareToPlay];
[data play];
}

Can't use two "audioPlayerDidFinishPlaying"

Can you please tell me how to fix this? I want to release two different AVAudioPlayer when they finish playing, but separately.
Here's my code:
.h File
#interface ViewController : UIViewController <AVAudioPlayerDelegate>
{
NSString *path;
}
- (IBAction)Short:(id)sender;
- (IBAction)BeatLong:(id)sender;
.m File
AVAudioPlayer *media;
AVAudioPlayer *media2;
- (IBAction)Short:(id)sender
{
path = [[NSBundle mainBundle] pathForResource:#"Short" ofType:#"wav"];
media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[media setDelegate:self];
[media play];
}
- (IBAction)Beat:(id)sender
{
path = [[NSBundle mainBundle] pathForResource:#"Beat" ofType:#"mp3"];
media2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[media2 setDelegate:self];
[media2 play];
}
(Open the image in a new tab to see it better ^^,)
Nay, that's illegal on the grounds of the language.
You have to distinguish the different players according to the AVAudioPlayer * pointer
submitted with the message.
If you just want to release it, just write
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)aPlayer successfully:(BOOL)flag
{
[aPlayer release];
}
and you're done and illegal too, since you don't own aPlayer.
But a better solution would be to detect which audio player you own and release it.
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)aPlayer successfully:(BOOL)flag
{
if ( aPlayer == self.media )
[self.media release];
else if ( aPlayer == self.media2 )
[self.media2 release];
// other players cannot be released, since we don't know anything about their owner.
}

Resources