AVAudioPlayer crash after updating to xCode 4.6 - ios

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.

Related

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

AVAudio player always nil

I'm not sure why my AVAudioPlayer is always nil?
#import <AVFoundation/AVFoundation.h>
#property (nonatomic) AVAudioPlayer *audioPlayer;
#synthesize audioPlayer;
- (void)viewDidLoad {
[super viewDidLoad];
// Construct URL to sound file
NSString *path = [NSString stringWithFormat:#"%#/background-music-aac.caf", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
// Create audio player object and initialize with URL to sound
NSError *audioError = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&audioError];
if (audioPlayer == nil) {
NSLog(#"Error? %#", audioError);
NSLog(#"AudioPlayer: %#", audioPlayer.description);
}
I feel like I have this all set up right, but it doesn't work and I'm getting nil/null for the audioPlayer.
The .caf file was the problem, thanks to Brandon for the help

No sound when button clicked

I am trying to play a sound when I click a button, the sound clip is 10 sec, the application runs fine and when I click the button nothing happens there are no errors or nothing, this is my code
.m file
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
- (IBAction)Play:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle]pathForResource:#"Blesgaes" ofType:#"mp3"];
NSURL *audioURL =[NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
}
.h file
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
- (IBAction)Play:(id)sender;
I have added AVFoundation framework and I have followed these steps
http://looksok.wordpress.com/2013/01/19/ios-tutorial-play-sound/
http://www.techotopia.com/index.php/Playing_Audio_on_an_iPhone_using_AVAudioPlayer_(iOS_6)
http://code-and-coffee.blogspot.in/2012/09/how-to-play-audio-in-ios.html
Any ideas what I am doing wrong.
Below is the simple code:-
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle
mainBundle] pathForResource:#"Woof"
ofType:#"mp3"];
NSURL *audioURL = [NSURL
fileURLWithPath:audioPath];
NSError *audioError = [[NSError alloc] init];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:audioURL
error:&audioError];
if (!audioError) {
[audioPlayer play];
NSLog(#"Woof!");
}
else {
NSLog(#"Error!");
}
}

Objective C - IOS - Xcode 4.6.2 - can't hear sound from mp3 files

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

Playing sounds on IOS

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

Resources