Sound does not play while using AVAudioPlayer - iphone

Here is my code on button click
- (void)playButton:(id)sender
{
NSLog(#"Play Button Clicked!!!...");
audioPlayer = [self setUpAudioPlayer:[songs objectAtIndex:i]];
}
-(AVAudioPlayer *)setUpAudioPlayer:(NSString *)filename
{
NSLog(#"File name : %#",filename);
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:#"caf"];
NSLog(#"File path = %#",path);
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
//audioPlayer.volume = 0.5;
[audioPlayer prepareToPlay];
[audioPlayer play];
NSLog(#"Song is playing....");
return [audioPlayer autorelease];
}
here songs is NSMutableArray which contains file name... All things go correct with NSLog... But no sound is coming and no error comes.. What is wrong with the code..???

Your audio player is probably getting released as soon as it starts playing. Try to retain it after setUpAudioPlayer is called.

Most probably you need to initialize an audio session, some like this
- (void)setupAudioSession
{
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil;
[session setCategory: AVAudioSessionCategoryPlayback error: &error];
if (error != nil)
NSLog(#"Failed to set category on AVAudioSession");
// AudioSession and AVAudioSession calls can be used interchangeably
OSStatus result = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, RouteChangeListener, self);
if (result) NSLog(#"Could not add property listener! %d\n", result);
BOOL active = [session setActive: YES error: nil];
if (!active)
NSLog(#"Failed to set category on AVAudioSession");
}

Related

IPhone, Audio MP3 or M4A audio file plays no sound

I'm trying to play a sound method within the object didSelectedIndex UITableViewController
The audio files are files with extension m4a, but I also tried with mp3 files and the result does not change.
Below I post the code that I have implemented in the method of TableViewController:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *soundFilePath = [NSString stringWithFormat:#"%#/testaudio.m4a", [[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(#"soundFileURL %#",soundFileURL);
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
if(!player){
NSLog(#"ERRORE: %#",error);
}
[player setDelegate:self];
[player prepareToPlay];
[player play];
}
I also tried directly on the device and it still does not play any sound.
I also added a class AVSession as follows:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
The result did not change, does anyone know what the problem is?
Try this (tested):
Set a property for your AVAudioPlayer object in your .m:
#property(strong, nonatomic) AVAudioPlayer *player;
Then add this in your:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *soundFilePath;
if ((soundFilePath = [[NSBundle mainBundle] pathForResource:#"testaudio" ofType:#"m4a"]))
{
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSError *error;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
if(self.player)
{
[self.player setDelegate:self];
[self.player prepareToPlay];
[self.player play];
}
else
{
NSLog(#"ERRORE: %#", error);
}
}
}
You don't need AVAudioSession in your case.

An AVPlayerItem cannot be associated with more than one instance of AVPlayer

I am getting an error when using AVAudioPlayer in Iphone. i got this error sometime not every time. error is :
An AVPlayerItem cannot be associated with more than one instance of AVPlayer
Code which i am using for playing a file :
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"abc" ofType:#"m4a"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error;
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[theAudio setDelegate:self];
[theAudio setNumberOfLoops:0];
if(theAudio == nil)
{
NSLog([error description]);
}
else
{
[theAudio play];
}
can anyone tell me how can i solve this issue.

Using AVCaptureSession and AVAudioPlayer together

I am able to record video and output the movie to a file correctly. However, I have a problem with video recording (no video ouput) when trying to use AVAudioPlayer to play some audio. Does it mean that I cannot use AVCaptureSession and AVAudioPlayer at the same time? Here is my code to create the capture session and to play the audio. The video capture is started first, then during the capturing, I want to play some audio. Thanks so much for any help.
Code to create the capture session to record video (with audio) - output to a .mov file:
- (void)addVideoInput
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
//... also some code for setting up videoDevice/frontCamera
NSError *error;
AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput
deviceInputWithDevice:frontCamera error:&error];
AVCaptureDeviceInput *audioIn = [AVCaptureDeviceInput
deviceInputWithDevice:audioDevice error:&error];
if (!error) {
if ([captureSession canAddInput:audioIn])
[captureSession addInput:audioIn];
else
NSLog(#"Couldn't add audio input.");
if ([captureSession canAddInput:videoIn])
[captureSession addInput:videoIn];
else
NSLog(#"Couldn't add video input.");
}
- (void)addVideoOutput
{
AVCaptureMovieFileOutput *m_captureFileOutput = [[AVCaptureMovieFileOutput alloc] init];
[captureSession addOutput:m_captureFileOutput];
[captureSession startRunning];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSMutableString *filePath = [NSMutableString stringWithString:#"Movie.mov"];
NSString* fileRoot = [docDir stringByAppendingPathComponent:filePath];
NSURL *fileURL = [NSURL fileURLWithPath:fileRoot];
AVCaptureConnection *videoConnection = NULL;
for ( AVCaptureConnection *connection in [m_captureFileOutput connections] )
{
for ( AVCaptureInputPort *port in [connection inputPorts] )
{
if ( [[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
}
}
}
[videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
[m_captureFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self];
[m_captureFileOutput release];
}
Code to play the audio, this function is call during the video capture session. If I don't call this function, the video is recorded and I am able to save to the .mov file. However, if I call this function, there's no output .mov file.
- (void)playAudio
{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"AudioFile" ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[fileURL release];
self.audioPlayer = newPlayer;
[newPlayer release];
[audioPlayer setDelegate:self];
[audioPlayer prepareToPlay];
[audioPlayer play];
}
I fixed the problem by setting up the audio session. I called the following function before creating the audio player object to play the audio. That way, I was able to record video (with audio) and play audio at the same time.
- (void)setupAudioSession {
static BOOL audioSessionSetup = NO;
if (audioSessionSetup) {
return;
}
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
audioSessionSetup = YES;
}
- (void)playAudio
{
[self setupAudioSession];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"AudioFile" ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[fileURL release];
self.audioPlayer = newPlayer;
[newPlayer release];
[audioPlayer setDelegate:self];
[audioPlayer prepareToPlay];
[audioPlayer play];
}

playing audio while in background mode doesnt work

In my application audio is not being played when i put my application in background mode ..i have seen such issues in this site itself.it is mentioned that
1) in the app-info.plist add to background modes "audio"
2) in the code i added support for audio sessions
if([player isPlaying])
{
[player stop];
}
NSError *error=nil;
int trackid=arc4random()%10+1;
NSString *trackname=[NSString stringWithFormat:#"trk-%d",trackid];
NSLog(#"%#",trackname);
NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:trackname ofType:#"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (setCategoryError) { /* handle the error condition */ }
NSError *activationError = nil;
[audioSession setActive:YES error:&activationError];
if (activationError) { /* handle the error condition */ }
if(error) {
NSLog(#"%#",&error);
}
[player prepareToPlay];
[player play];
player.numberOfLoops=1;
but still it is not working when i put the application in backgroud mode.it resumes when i put the application back.have i missed something?
use this line in the - (void)viewDidLoad
[UIApplication sharedApplication].idleTimerDisabled = YES;

restarting a (audio) loop after it has finished

I want to play an audio loop by turning on a switch but i do not want the loop to end until the switch is turned off. When the loop ends i want it to simply start back at the beginning as to be continuos.
Try something like this. Note the -1 tells the music to repeat indefinitely:
Import this file at the top of your file:
#import <AVFoundation/AVFoundation.h>
Then to use it:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:resourcePath ofType:#"caf"];
NSError *activationError = nil;
NSError *audioPlayerInitError = nil;
[[AVAudioSession sharedInstance] setActive: YES error:&activationError];
NSURL *newURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:newURL error:&audioPlayerInitError];
[newPlayer prepareToPlay];
[newPlayer setVolume:.8];
[newPlayer setNumberOfLoops:-1]; // -1 means play indefintely
[newPlayer setDelegate: self];
[newPlayer play];
[newPlayer release];
And then to stop it you would do:
if ([newPlayer isPlaying]) {
[newPlayer stop];
}