Sound wont play using avfoundation - ios

Any reason why this will not be working?
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *showsound;
NSString *audiopath = [[NSBundle mainBundle] pathForResource:#"mouse1" ofType:#"wav"];
NSURL *audiourl = [NSURL fileURLWithPath:audiopath];
showsound = [[AVAudioPlayer alloc]initWithContentsOfURL:audiourl error:Nil];
[showsound play];

Yes, the reason is that your audio player instance, showsound, is created as a local variable so will be deallocated as soon as the method that code is in goes out of scope. Create a strong property for show sound, and it will work properly.

Related

AVPlayer doesn't play anything

I am doing something incredibly simple, just play an audio.
but it doesn't play anything. not on the simulator or device.
audio file is in bundle and preview can play the audio, I have also tried different audios nothing plays.
What could be the problem?, do I have to add delegate methods for avplayer?(if so that doesn't make sense to me)
I have also tried without avplayer item and different nsurl methods too...
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"JohnsPizza" ofType:#"aiff"];
//NSURL *assetURL = [[NSBundle mainBundle] URLForResource:#"JohnsPizza" withExtension:#"aiff"];
NSURL *url = [NSURL fileURLWithPath:path];
AVPlayer *player = [[AVPlayer alloc] init];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:playerItem];
[player play];
}
I have also tried but no luck
// SystemSoundID sound1;
// AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &sound1);
// AudioServicesPlaySystemSound(sound1);
// AudioServicesRemoveSystemSoundCompletion(sound1);
// AudioServicesDisposeSystemSoundID(sound1);
you gotta retain AVPlayer object as it may set to nil automatically
#property (strong, nonatomic) AVPlayer *player;

No Visible interface for 'AVAudioPlayer declares the selector initWithUrl:'

I am trying to play a button click sound,also that i should be able to manage click sound along with volume button.
What i have done is
Added AVFoundationFramework to project,
in .h file
#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVFoundation.h>
in .m file under button click method
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"sound1" ofType:#"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithURL:soundUrl];
[player play];
but i get the above error.Any help would be appreciated.
It's "initWithContentsOfURL:" not "initWithURL:".
As in:
AVAudioPlayer *player = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundUrl];

AVfoundation AudioPlayer not working

I am trying to play a sound on button click using the AVFoundation framework. Previously, i used audio toolbox however when i use the audioplayer nothing comes out. What is weird is that when i test it on my phone and i press the button, the volume controls aren't the normal ringer volume controls. This suggests that it should be playing but i can't hear anything, on my phone or the simulator. I have used different files, created new project changed the code, nothing has helped.
Here's the code in .h:
#interface MainViewController : UIViewController <AVAudioPlayerDelegate>
- (IBAction)test;
Here's the code in .m:
- (IBAction)test {
NSString *path = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
can you do this before you play the sound i think you might have the wrong category:
AudioSessionInitialize (NULL,NULL,NULL,NULL);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,sizeof (sessionCategory),&sessionCategory);
AudioSessionSetActive(true);

AVAudioPlayer: Simple sound not playing

I'm trying to figure out why sounds aren't playing in my app, so I created what I think is as simple an implementation as possible:
NSError *error;
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"canary-trills" ofType:#"wav"];
NSLog(#"string=%#", soundFilePath);
NSURL *url = [[NSURL alloc] initFileURLWithPath:soundFilePath];
NSLog(#"URL=%#", url);
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url error:&error];
[avPlayer prepareToPlay];
BOOL success = [avPlayer play];
NSLog(#"The sound %# play", success ? #"did" : #"didn't");
Per the console, it looks like it finds the resource, creates the URL correctly. Even the play call indicates success. However, no sound play in the simulator nor the device.
AVAudioPlayer wasn't being retained & was going out of scope.
Adding:
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
to the class & using that member solved the issue.

AVFoundation not playing sound

I have always used audio toolbox to play my sounds, but users have saying there is no sound, this is because when you are on silent it doesnt play. I have tried many times to swap instead to av foundation but it never works for me. this is the code i am attempting to use now:
- (IBAction)soundbutton:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:#"EASPORTS" ofType:#"mp3"];
AVAudioPlayer * theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];}
Reason could be that in ARC your audio player is being released by ARC, therefore you need to make a strong reference of the AVAudioPlayer, you can do that by making the AVAudioPlayer as class level property.
here is how you do it, in your .h file like this
#property (strong, nonatomic) AVAudioPlayer *theAudio;
and in .m file synthesize it like this
#synthesize theAudio;
and finally your code would look like this
- (IBAction)soundbutton:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:#"EASPORTS" ofType:#"mp3"];
self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[self.theAudio play];
}
check also if your delegate methods are responding something
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error;

Resources