Playing back audio using AVAudioPlayer iOS 7 - ios

I'm struggling to follow Apple's documentation regarding playing back a small .wav file using the AVAudioPlayer class. I'm also not sure what toolboxes I need for basic audio playback. So far I have imported:
AVFoundation.framework
CoreAudio.framework
AudioToolbox.framework
Here is my code .h and .m:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController <AVAudioPlayerDelegate>
- (IBAction)playAudio:(id)sender;
#end
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)playAudio:(id)sender {
NSLog(#"Button was Pressed");
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"XF_Loop_028" ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
// create new audio player
AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
[myPlayer play];
}
#end
I don't seem to have any errors. However, I am not getting any audio.

You're having an ARC issue here. myPlayer is being cleaned up when it's out of scope. Create a strong property, assign the AVAudioPlayer and you're probably all set!
#property(nonatomic, strong) AVAudioPlayer *myPlayer;
...
// create new audio player
self.myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
[self.myPlayer play];

Is the filePath coming back with a valid value? Is the fileURL coming back with a valid value? Also, you should use the error parameter of AVAudioPlayer initWithContentsOfURL. If you use it it will likely tell you EXACTLY what the problem is.
Make sure you check for errors and non-valid values in your code. Checking for nil filepath and fileURL is the first step. Checking the error parameter is next.
Hope this helps.

What I do is create an entire miniature class just for this purpose. That way I have an object that I can retain and which itself retains the audio player.
- (void) play: (NSString*) path {
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
NSError* err = nil;
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: &err];
// error-checking omitted
self.player = newPlayer; // retain policy
[self.player prepareToPlay];
[self.player setDelegate: self];
[self.player play];
}

Related

ios remove avaudioplayer loop delay

I have an avaudioplayer that loads a sound file and loops. The problem is that everytime the file loops, there is a pause/delay which makes music sound bad.
I found this question which had a similar issue, but is for 2 players. I have a single player that has a 1 second pause at each loop: AVAudioPlayer eliminating one second pause between sound files
Here is what my audio player setup looks like:
#interface HomeViewController ()
#property (strong, nonatomic) DataController *dataController;
#end
#implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.dataController = [DataController sharedInstance];
[self createAndStartAudioPlayer];
}
- (void) viewWillAppear:(BOOL)animated{
[self.dataController.player play];
}
- (void) createAndStartAudioPlayer {
NSTimeInterval shortStartDelay = 0.01; // seconds
NSTimeInterval now = self.dataController.player.deviceCurrentTime;
[self.dataController.player playAtTime: now + shortStartDelay];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"jingle-loop" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
self.dataController.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL fileTypeHint:AVFileTypeMPEGLayer3 error:nil];
self.dataController.player.numberOfLoops = -1; //infinite
[self.dataController.player prepareToPlay];
}
How do I stop this 1 second pause and play the loop like it is a continuous song?
Mr. T in the comments was in fact right. The solution was to fix the white space that is added automatically by garage band.

AVAudioPlayer not playing long sounds?

I have a series of sound files ranging from 0.3s to 3.0s.
When trying to play with AVAudioPlayer, I get nothing, unless I add a sleep(4) call after to ensure the sound can play. Really weird.
Apart from that, no errors with the error param that gets passed in, and the [player play] returns YES every time. The delegate methods are not called, though, oddly enough.
Here's my code.
(.m file)
#interface MyClass ()
#property (nonatomic, strong) AVAudioPlayer *soundPlayer;
#end
#implementation SLInspireKit
//view did load here
- (void)playRandomSound {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
NSError *error;
NSString *musicFileName = [NSString stringWithFormat:#"my-sound-%d.aiff", arc4random_uniform(8)];
NSString *path = [NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], musicFileName];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
self.soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&error];
self.soundPlayer.delegate = self;
[self.soundPlayer setVolume:1.0];
[self.soundPlayer prepareToPlay];
if ([self.soundPlayer play]){
NSLog(#"Played fine");
} else {
NSLog(#"Did not play fine");
}
sleep(1); //uncomment this out, and nothing. Set to 1, first second of each sound, 2, 2 seconds and so on.
}
Any ideas? I don't think it's an ARC thing, but it could be :/ I have to initiate each time because the sound changes each time.
Quick summary of the comments:
Turns out the sleep(n) was needed since the entire class containing the AVAudioPlayer was deallocated.
Retaining the class fixes the issue.
Cheers!

AVPlayer doesn't play anything

I am doing something incredibly simple, just play an audio.
but it doesn't play anything. not on the simulator or device.
audio file is in bundle and preview can play the audio, I have also tried different audios nothing plays.
What could be the problem?, do I have to add delegate methods for avplayer?(if so that doesn't make sense to me)
I have also tried without avplayer item and different nsurl methods too...
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"JohnsPizza" ofType:#"aiff"];
//NSURL *assetURL = [[NSBundle mainBundle] URLForResource:#"JohnsPizza" withExtension:#"aiff"];
NSURL *url = [NSURL fileURLWithPath:path];
AVPlayer *player = [[AVPlayer alloc] init];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:playerItem];
[player play];
}
I have also tried but no luck
// SystemSoundID sound1;
// AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &sound1);
// AudioServicesPlaySystemSound(sound1);
// AudioServicesRemoveSystemSoundCompletion(sound1);
// AudioServicesDisposeSystemSoundID(sound1);
you gotta retain AVPlayer object as it may set to nil automatically
#property (strong, nonatomic) AVPlayer *player;

Performing actions from accelerometer

I'm very new to programming in Xcode and have already hit a brick wall.
What I am trying to do is to get a sound to play if there is sufficient acceleration detected.
Can someone please advise me on what I am doing wrong? (I suspect its everything).
CODE:
#import "LZDViewController.h"
#interface LZDViewController ()
#end
#implementation LZDViewController
- (void)viewDidLoad;{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CMAccelerometerData *accelerometerData = self.motionManager.accelerometerData;
if (fabsf(accelerometerData.acceleration.x) > 1.2
|| fabsf(accelerometerData.acceleration.y) > 1.2
|| fabsf(accelerometerData.acceleration.z) > 1.2)
{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audio.wav", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
}
}
#end
EDIT:
Here is the code for my .h
#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>
#import <AVFoundation/AVFoundation.h>
#interface LZDViewController : UIViewController
//{AVAudioPlayer *audioPlayer;}
#property (strong, nonatomic) CMMotionManager *motionManager;
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
#end
I'm still not getting any joy at all, and I do apologise for the hand holding.
All I get is a white screen but the sound file never plays. I have imported the correct framework and can get the sound playing with just a button, but I really want to play with the accelerometer.
Thanks
Alright, the first thing you're going to want to do is define two properties in either your interface file, or in the interface extension at the top of your implementation file. These will be the motion manager, and the audio player.
#property (strong, nonatomic) CMMotionManager *motionManager;
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
Then we can get everything else done in viewDidLoad. The code below sets up a new operation queue for the motion manager to receive accelerometer events on, initializes the audio player and the motion manager, and then starts getting accelerometer updates at an interval of 30 times per second.
Finally, within the accelerometers update block, just use the same logic you already build to check the acceleration data and if it meets your requirements continue on to the next condition.
**I have configured this example to not start playing the sound again if the player is already playing
- (void)viewDidLoad
{
[super viewDidLoad];
NSOperationQueue *motionQueue = [NSOperationQueue new];
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"audioFile" ofType:#"m4a"]];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.audioPlayer prepareToPlay];
self.motionManager = [CMMotionManager new];
[self.motionManager setAccelerometerUpdateInterval:1.0f/30.0f];
[self.motionManager startAccelerometerUpdatesToQueue:motionQueue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (fabsf(accelerometerData.acceleration.x) > 1.8 || fabsf(accelerometerData.acceleration.y) > 1.8 || fabsf(accelerometerData.acceleration.z) > 1.8) {
NSLog(#"%#",accelerometerData);
if (self.audioPlayer) {
if (!self.audioPlayer.isPlaying) {
[self.audioPlayer play];
}
}
}
}];
}

AVFoundation not playing sound

I have always used audio toolbox to play my sounds, but users have saying there is no sound, this is because when you are on silent it doesnt play. I have tried many times to swap instead to av foundation but it never works for me. this is the code i am attempting to use now:
- (IBAction)soundbutton:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:#"EASPORTS" ofType:#"mp3"];
AVAudioPlayer * theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];}
Reason could be that in ARC your audio player is being released by ARC, therefore you need to make a strong reference of the AVAudioPlayer, you can do that by making the AVAudioPlayer as class level property.
here is how you do it, in your .h file like this
#property (strong, nonatomic) AVAudioPlayer *theAudio;
and in .m file synthesize it like this
#synthesize theAudio;
and finally your code would look like this
- (IBAction)soundbutton:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:#"EASPORTS" ofType:#"mp3"];
self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[self.theAudio play];
}
check also if your delegate methods are responding something
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error;

Resources