All device is playing the sound in foreground except iPod touch not playing the sound in foreground, When the app receive notification,
Below we have paste the exact code
NSString *playSoundOnAlert = #"Sound.wav";
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#",[[NSBundle mainBundle] resourcePath],playSoundOnAlert]];
NSError *error;
aVAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
aVAudioPlayer.numberOfLoops = 0;
[aVAudioPlayer play];
check the code:
NSString *playSoundOnAlert = #"Sound";
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource: playSoundOnAlert ofType:#".wav"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *audioError = [[NSError alloc] init];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&audioError];
if (!audioError) {
[audioPlayer play];
NSLog(#"playing!");
}
else {
NSLog(#"Error!");
}
Related
I have used the following code to initialise an AVAudioPlayer object with NSData, which in turn has the data of an mp3 in my main bundle.
NSError *error;
NSString *filePathForBgAudio = [[NSBundle mainBundle] pathForResource:#"beebuzz10" ofType:#"mp3"];
NSData *bgAudioData = [NSData dataWithContentsOfFile:filePathForBgAudio];
bgAudioPlayer = [[AVAudioPlayer alloc] initWithData:bgAudioData error:&error];
bgAudioPlayer.delegate = self;
I have declared bgAudioPlayer in the .h file. But when I checked by setting breakpoint, I found that after this line bgAudioPlayer = [[AVAudioPlayer alloc] initWithData:bgAudioData error:&error]; the bgAudioPlayer is still shown to be nil. But the filePathForBgAudio and bgAudioData are initialised correctly and has data. just the bgAudioPlayer is nil. What is the problem in the above code? How to rectify this?
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"bubble"
ofType:#"mp3"]];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
} else
{
_audioPlayer.delegate = self;
[_audioPlayer prepareToPlay];
}
}
- (IBAction)playsound:(id)sender
{
[_audioPlayer play];
}
NSError *error;
NSURL *filePathForBgAudio1 =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"beebuzz10" ofType:#"mp3"]];
//NSString *filePathForBgAudio = [[NSBundle mainBundle] pathForResource:#"beebuzz10" ofType:#"mp3"];
// NSData *bgAudioData = [NSData dataWithContentsOfFile:filePathForBgAudio1];
bgAudioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:filePathForBgAudio1 error:&error];
I am new in Objective-C. Please help me for this.
I have one viewController which contains scrollview.
scrollView are paging enabled so i have 10 pages on each page i have 30 buttons .
My problem is when click on multiple button i want to play multiple sound..
My code : -
-(void)buttonClicked:(UIButton *)sender{
if (sender.tag == 1) {
NSString *audioPath = [[NSBundle mainBundle]pathForResource:#"v1" ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
audioPlayer.delegate = self;
[audioPlayer play];
}
if (sender.tag == 2) {
NSString *audioPath = [[NSBundle mainBundle]pathForResource:#"v2" ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
audioPlayer.delegate = self;
[audioPlayer play];
}
if (sender.tag == 3) {
NSString *audioPath = [[NSBundle mainBundle]pathForResource:#"v3" ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
audioPlayer.delegate = self;
[audioPlayer play];
}
problem is when i pressed on buttons only one sound is enable. how to enable multiple sounds ??
please help me...
thanks in advance
First import #import <AudioToolbox/AudioServices.h>
NSString *path = [[NSBundle mainBundle] pathForResource:#"soundeffect" ofType:#"m4a"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
SystemSoundID audioEffect;
AudioServicesCreateSystemSoundID((CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
In each UIButton you need change the path with the sound that you need play.
Hope this help :)
I am having trouble with my audio.
here is the code:
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle]pathForResource:#"Cheering" ofType:#"mp3"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
It will not play like this. The code steps through just fine but no audio. If I put a break on the last line and then step through it plays properly. I have tried everything I can to get this to work but have no clue where to go from here.
It is acting like it needs a delay before the play command.
Try this Sure It will Works
NSURL* url = [[NSBundle mainBundle] URLForResource:#"Cheering" withExtension:#"mp3"];
NSAssert(url, #"URL is valid.");
NSError* error = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
if(!audioPlayer)
{
NSLog(#"Error creating player: %#", error);
} else
{
[audioPlayer play];
}
Did you try
[audioPlayer prepareToPlay]
before you actually try to play it?
https://developer.apple.com/library/ios/DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html#//apple_ref/occ/instm/AVAudioPlayer/prepareToPlay
I want to play a sound file (which I have dragged into xcode and copy to the project) using av foundation with the following code but fail.
I think NSURL *url = [[NSURL alloc] initWithString:#"sound.caf"]; this is where it goes wrong, but I have no idea other than this way, how I could instantiate an AVAsset with this the sound file (of course, it would be other place that goes wrong). Anyway, can someone offer me some helps? thanks
AVMutableComposition *composition = [AVMutableComposition composition];
CMPersistentTrackID trackID = kCMPersistentTrackID_Invalid;
AVMutableCompositionTrack *compositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:trackID];
NSURL *url = [[NSURL alloc] initWithString:#"sound.caf"];
AVAsset *songAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVAssetTrack *assetTrack = [[songAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
CMTime startTime = CMTimeMakeWithSeconds(0, 1);
CMTime endTime = songAsset.duration;
CMTimeRange tRange = CMTimeRangeMake(startTime, endTime);
NSError *error = nil;
[compositionTrack insertTimeRange:tRange ofTrack:assetTrack atTime:CMTimeMake(0, 44100) error:&error];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition];
self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[self.player play];
This code should help you:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"wav"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error = nil;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
if (error) {
NSLog(#"%#",[error localizedDescription]);
}
[player play];
Try this
NSString *shutterplayerPath =
[[NSBundle mainBundle] pathForResource:#"shutter" ofType:#"mp3"];
NSString *tickplayerPath =
[[NSBundle mainBundle] pathForResource:#"tick" ofType:#"wav"];
shutterAudioPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc]
initFileURLWithPath: shutterplayerPath] error:NULL];
tickAudioPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc];
initFileURLWithPath:tickplayerPath] error:NULL];
[shutterAudioPlayer play];
[tickAudioPlayer play];
Here is my Code(ARC enabled) where memory leak is seen.
Please help me to solve this issue.
- (void) setMusic
{
/*
Initialise and set the music for the game.
*/
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"actionMusic" ofType:#"caf"]];
NSError *error = nil;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!error)
{
_audioPlayer.delegate = self;
if(_musicFlag)
{
[_audioPlayer play];
}
[_audioPlayer setNumberOfLoops:INT32_MAX];
}
error = nil;
url =nil;
url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"pew" ofType:#"wav"]];
_moveMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!error)
{
_audioPlayer.delegate = self;
}
error = nil;
url =nil;
url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Won" ofType:#"wav"]];
_winningMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!error)
{
_winningMusic.delegate = self;
[_winningMusic setNumberOfLoops:1];
}
}
I found the answer for the leak.
This leak is due to AVAudioPlayer foundation class which is yet to be fixed by Apple.
Thus Usage of AVAudioPlayer foundation class leads to leaks.
Check This link for more detail