- (void)play {
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:#"sound" withExtension:#"mp3"];
NSString *path = [imgPath path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
NSLog(#"%#",data);
NSError *error;
self.avPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
[self.avPlayer play];
}
Rather then converting into NSData, you can pass URL to AVAudioPlayer.
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:#"sound" withExtension:#"mp3"];
NSError *error;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:imgPath
error:&error];
self.player.numberOfLoops = 0; //Infinite
self.player.delegate = self;
[self.player prepareToPlay];
[self.player play];
Your code works fine. The problem is while you try to Log the Data.
Or else try with initWithContentsOfURL method.
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
#property (nonatomic, strong) AVAudioPlayer *avPlayer;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:#"1" withExtension:#"mp3"];
NSString *path = [imgPath path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
// NSLog(#"%#",data);
NSError *error;
self.avPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
// self.avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:imgPath error:&error];
[self.avPlayer play];
}
Related
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!");
}
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];
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
After updating to xCode 4.6 my audio player crashes with no error logs. I cannot log the error as it crashes on play. I get no message on the debugger only a thread/ breakpoint on play.Anyone any ideas?
NSError *error;
NSString *stringPath1 = [[NSBundle mainBundle] pathForResource:#"beep" ofType:#"mp3"];
NSURL *url1 = [NSURL fileURLWithPath:stringPath1];
playerForRecording = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:&error];
[playerForRecording setDelegate:self];
[playerForRecording play];
http://i1107.photobucket.com/albums/h385/snksnk1/stack%20overflow/ScreenShot2013-02-05at54421PM_zps72842a8f.png
I'm using the same compiler and this works for me:
NSError *error;
NSString *stringPath1 = [[NSBundle mainBundle] pathForResource:#"beep" ofType:#"mp3"];
NSURL *url1 = [NSURL fileURLWithPath:stringPath1];
NSAssert(url1, #"URL is valid.");
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:&error];
if(!self.player)
{
NSLog(#"Error creating player: %#", error);
}
[self.player play];
Of course, in the .h, are the lines:
#import <AVFoundation/AVFoundation.h>
#interface myProgramViewController : UIViewController<AVAudioPlayerDelegate>
...
#property (nonatomic, strong) AVAudioPlayer* player;
with, in the .m, the corresponding:
#synthesize player = mPlayer;
Hope this helps.
I want to play a sound when a button is pressed. I tried this:
-(void)PlayClick
{
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"BubblePopv4"
ofType:#"mp3"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click play];
}
…and imported #import <AudioToolbox/AudioToolbox.h> and #import <AVFoundation/AVFoundation.h>
but for some reason the sound is not playing. The name and type are right because the file I added to the project is "BubblePopv4.mp3". The volume on the iPad is on maximum, and the sound plays fine on iTunes! Any ideas?
Leave it, Don't do it in a new Class, simple stuff for you:
- (void)playSoundWithOfThisFile:(NSString*)fileNameWithExtension {
// Eg - abc.mp3
AVAudioPlayer *audioPlayerObj;
NSString *filePath;
filePath= [[NSString stringWithFormat:#"%#", [[NSBundle mainBundle] resourcePath]] retain];
if(!audioPlayerObj)
audioPlayerObj = [AVAudioPlayer alloc];
NSURL *acutualFilePath= [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#",filePath,fileNameWithExtension]];
NSError *error;
[audioPlayerObj initWithContentsOfURL:acutualFilePath error:&error];
[audioPlayerObj play];
}
Do this:
In .h file:
Use this delegate - <AVAudioPlayerDelegate>
#import "AVFoundation/AVAudioPlayer.h"
AVAudioPlayer *audioPlayerObj;
NSString *filePath;
In .m file , In some method:
filePath= [[NSString stringWithFormat:#"%#", [[NSBundle mainBundle] resourcePath]] retain];
if(!audioPlayerObj)
audioPlayerObj = [AVAudioPlayer alloc];
And add these two methods:
- (id)initWithFileName:(NSString*)fileNameWithExtension {
if ((self = [super init])) {
NSURL *acutualFilePath= [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#",filePath,fileNameWithExtension]];
NSError *error;
[audioPlayerObj initWithContentsOfURL:acutualFilePath error:&error];
}
return self;
}
- (void)playSound {
[audioPlayerObj play];
}
You can pass the filename to this Method - initWithFileName
and then call playSound method to play sound for you.
Hope this helps.
Replace url initialization with:
NSURL* musicFile = [NSURL initFileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"BubblePopv4"
ofType:#"mp3"]];