AVAudioPlayer initWithData returning nil - ios

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];

Related

iPod touch not Playing sound

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!");
}

Audio file not play in objective C

- (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];
}

AVAudioPlayer initWithURL not working to stream radio

I'm trying to stream an radio in my app.
I'm using AVAudioPlayer, but I have an error I don't understand :
NSString *urlString = #"http://broadcast.infomaniak.net/radionova-high.mp3";
NSURL *myURL = [NSURL URLWithString:urlString];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:myURL error:&error];
if (self.audioPlayer == nil){
NSLog(#"Error loading clip: %#", [error localizedDescription]);
}
else {
self.audioPlayer.delegate = self;
self.audioPlayer.volume = 0.5;
[self.audioPlayer play];
}
And I have the following error :
Error loading clip: The operation couldn’t be completed. (OSStatus error -43.)
Does anyone see what's wrong ?
EDIT :
This is what the Apple docs say:
The AVAudioPlayer class does not provide support for streaming audio
based on HTTP URL's. The URL used with initWithContentsOfURL: must be
a File URL (file://). That is, a local path.
Now what ? :(
download file to document directoty then play file
NSString *urlString = #"http://broadcast.infomaniak.net/radionova-high.mp3";
NSURL *myURL = [NSURL URLWithString:urlString];
NSData *audioData = [NSData dataWithContentsOfURL:myURL];
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#.mp3", docDirPath , fileName];
[audioData writeToFile:filePath atomically:YES];
NSError *error;
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (self.audioPlayer == nil) {
NSLog(#"AudioPlayer did not load properly: %#", [error description]);
} else {
self.audioPlayer.delegate = self;
self.audioPlayer.volume = 0.5;
[self.audioPlayer play];
}

How to solve leak in this Code (ARC enabled)?

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

AVAudioPlayer crash after updating to xCode 4.6

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.

Resources