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
Related
I have a simple code to a play sound through AVAudioPlayer
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"beep"
ofType:#"mp3"]];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.player.volume = 1;
[self.player prepareToPlay];
My exception breakpoint will stop at prepareToPlay and console log wrote:
ERROR: AVAudioSessionUtilities.h:111: GetProperty:
AudioSessionGetProperty ('prp?') failed with error: 'pty?
After 2x tapping to pass through this breakpoint, application will not crash. This happened only in the simulator.
Any suggestions?
Following code might be helpful.
NSURL *url;
url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/beep.mp3", [[NSBundle mainBundle] resourcePath]]];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
self.audioPlayer .delegate = self;
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
I think its a Simulator specific error. This error doesnt come up on the device. There is a discussion here if you want to know more.
AVSpeechSynthesizer error AudioSession
in my app i need to play alert sounds.
if another app (like spotify) is running, when i run my code it stops the music from spotify whenever the alert sound plays.
is there anyway to avoid this?
this is my play sound code:
- (void)playSendSound
{
NSString* path;
NSURL* url;
path = [[NSBundle mainBundle] pathForResource:#"soundBit" ofType:#"aif"];
url = [NSURL fileURLWithPath:path];
player = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player play];
}
You are using AVAudioPlayer to play your alert sound and sure it will stop whatever music that is playing.
Modify your code to the following:
NSString* path;
NSURL* url;
path = [[NSBundle mainBundle] pathForResource:#"soundBit" ofType:#"aif"];
url = [NSURL fileURLWithPath:path];
player = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
[player play];
This should prevent the sound that is playing to stop when you play your alert sound.
Hope this helps.
I am trying to play an MP3 file, however I am having problems with my code. This is what I have:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (!audioPlayer) {
NSLog(#"localizedDescription : %#", [error localizedDescription]);
} else {
[audioPlayer play];
}
and this is the error I am reciving
localizedDescription : The operation couldn’t be completed. (OSStatus error -43.)
How do I stop this from happening? I want to play my sound once then stop it, but it can't even start.
The issue is probably caused by the path being invalid.
Try building it as follows
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"audiofile" ofType:#"mp3"];
NSURL *soundURL = [NSURL URLWithString:soundPath];
instead.
Also make sure that audiofile.mp3 is included in the current target.
I'm following in a tutorial on how to play mp3 sounds when pressing a button.
I created a button (playSound).
I added it to the view controller interface:
- (IBACTION)playSound:(id)sender;
in the implementation I declared the needed header files and I wrote the following:
#import "AudioToolbox/AudioToolbox.h"
#import "AVFoundation/AVfoundation.h"
- (IBAction)playSound:(id)sender {
//NSLog(#"this button works");
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"audio" ofType:#"mp3"];
//NSLog(#"%#", audioPath);
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
//NSLog(#"%#", audioURL);
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
}
I'm not getting any errors. the NSlogs are logging the URL fine, and now I have no clue where to look further. I also checked if my MP3 sound was maybe damaged, but that was also not the case. all i hear is a little crackling noise for 1 second. then it stops.
You declared a local variable audioPlayer to hold a pointer to the player. As soon as your button handler returns the player is being released before it has a chance to play your sound file. Declare a property and use it instead of the local variable.
In YourViewController.m file
#interface YourViewController ()
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
#end
or in YourViewController.h file
#interface YourViewController : UIViewController
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
#end
Then replace audioPlayer with self.audioPlayer in your code.
try this its working for me
in .h
AVAudioPlayer * _backgroundMusicPlayer;
in .m
NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:#"Theme" ofType:#"mp3"];
NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
NSError *error;
_backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
[_backgroundMusicPlayer setDelegate:self]; // We need this so we can restart after interruptions
[_backgroundMusicPlayer setNumberOfLoops:-1];
[_backgroundMusicPlayer play];
Edited
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Name of your audio file"
ofType:#"type of your audio file example: mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
audioPlayer.delegate=self;
[audioPlayer play];
Try this and let me know. Make sure you set the delegate for audioPlayer.
Firstly re add your music file in your project , then try this code
With the log you can see error.
NSError *error;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"bg_sound" ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error){
//Print Error
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
} else {
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer setNumberOfLoops: -1];
[audioPlayer play];
audioPlayer.volume=1.0;
}
Make sure your music files are properly added in project
I've an AVAudioPlayer with a MP3-file. But before playing, you'll hear a hitch.
This is my code:
NSString *geluid = [NSString stringWithFormat:#"RG_%#", nummer];
NSString *path = [[NSBundle mainBundle] pathForResource:geluid ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio prepareToPlay];
[theAudio play];
NSURL *url = [NSURL fileURLWithPath:path];
avplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[avplayer prepareToPlay];
[avplayer play];
For example: You hear "Ssstop here", in stead off "Stop here".
Please help me to fix it.
Is this code running two AVAudioPlayer instances at the same time ?
Why are you using both avplayer and theAudio ?
From looking at it it looks like you should get rid of one of them...