AVAudioPlayer play audio properly in Ipod Touch 4g but not in Ipad, Ipad2, why? - objective-c

Here is my code:
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:[settings alarmAudioName] ofType:#"mp3"];
NSLog(#"%#",path);
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
NSLog(#"%#",[NSURL fileURLWithPath:path]);
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
{
[audioPlayer prepareToPlay];
[audioPlayer play];
NSLog(#"Should have been played");
}
NSLog:
/var/mobile/Applications/9B5B4E10-A936-4D90-8CBE-17854A0B22F7/SSCA Development.app/Song 1.mp3
file://localhost/var/mobile/Applications/9B5B4E10-A936-4D90-8CBE-17854A0B22F7/SSCA%20Development.app/Song%201.mp3
Its works correct in Ipod Touch 4g, but not working in Ipad, Ipad2, why?
Thanks the answers,

Related

AVAudioPlayer Play/Pause/Stop local audio file on three different buttons

I want to play/pause/stop local audio file. Buttons for all functionalities are different. Check attached screenshot for reference.
Below is my click events,
-(IBAction)click_Play:(id)sender {
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
[player play];
}
-(IBAction)click_Pause:(id)sender {
[player pause];
}
-(IBAction)click_Stop:(id)sender {
[player stop];
}
Here, I am facing issue in pause functionality. After pausing the audio when I click on play button again. It start playing from starting and not at the point where I pause it. Any solution?
Thanks in advance!
Change your code to this:
-(IBAction)click_Play:(id)sender {
if (!player)
{
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
[player play];
}
else {
[player play];
}
}
Don't create instance of AVAudioSession and AVAudioPlayer again and again in play button action. So, keep it simple, create instance of AVAudioSession and AVAudioPlayer in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
}
-(IBAction)click_Play:(id)sender {
[player play];
}
-(IBAction)click_Pause:(id)sender {
[player pause];
}
-(IBAction)click_Stop:(id)sender {
[player stop];
}
Edit:
Call this method when you what to change track. I'll never recommend to create instance of object again and again
-(void)changeTrackWithPath:(NSString *)path{
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
[player replaceCurrentItemWithPlayerItem:playerItem];
[player play];
}

The app crashes when audio is playing and the device receives a phone call

The app crashes when audio is playing and the device receives a phone call. When I hang up the call and return to the application, it is blocked.
Code:
NSURL *audioFileLocationURL;
NSString *MyAudioString;
MyAudioString = #"my-audio";
audioFileLocationURL = [[NSBundle mainBundle] URLForResource:MyAudioString withExtension:#"mp3"];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
[audioPlayer setNumberOfLoops:1];
[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:#selector(updatemyProgress) userInfo:nil repeats:YES];
if (error) {
NSLog(#"%#", [error localizedDescription]);
} else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[audioPlayer prepareToPlay];
[audioPlayer setDelegate:self];
[audioPlayer play];
}
Done:
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player

How to play audio, even when mute button is on?

I'm trying to play an audio file when a button is tapped.
If I step through my code it works, but if I just tap on the button no sound is played.
Here is my code:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
AVAudioPlayer * player;
NSString * path = [[NSBundle mainBundle] pathForResource:#"soundOne" ofType:#"mp3"];
NSURL * url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[player play];
Is this the appropriate way to play audio? Nothing is being flagged as being deprecated.
I also see this message in the debugger:
AudioQueue: request to trim 0 + 2690 = 2690 frames from buffer containing 1152 frames
EDIT:
This code works.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"soundOne"
ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
[self.audioPlayer prepareToPlay];
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
How can I play audio with the mute button on?
Here is the code I have tried for this:
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus audioStatus = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if (audioStatus == kAudioSessionNoError) {
//NSLog(#"audio route: %#", state);
return (CFStringGetLength(state) <+ 0);
}
return NO;
Stick this before you call play:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
That should do it.

No sound/volume in Iphone Simulator 4.3 . But plays ok in Iphone simulator 5.1

I am developing a IPhone app to play sound files. My deployment target is 4.0. While trying to play in IPhone Simulator 4.3 it plays with no sound/volume. But it playing ok in Simulator 5.1.
My code:
NSString *fileName=[NSString stringWithFormat:#"book%i_chapter_%i",book_number,chapter_number];
NSString *path=[[NSBundle mainBundle] pathForResource:fileName ofType:#"mp3" inDirectory:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if(error)
{
NSLog(#"Error in audio Player: %#",[error localizedDescription]);
}
else
{
[audioPlayer prepareToPlay];
//audioPlayer.delegate=self;
[audioPlayer setVolume:20.0];
[audioSlider setValue:0.0];
[audioSlider setMaximumValue:[audioPlayer duration]];
[audioPlayer play];
}
Please help me to fix this. Thanks

Cocoa Touch - Loading AVAudioPlayer

I have this code to play a sound but the first time you play it it takes a good 5 seconds to load...
How can I speed this up?
-(IBAction)playSound{ //play the cricket noise
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"Cricket_Sound" ofType:#"mp3"];
audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
}
You can try by putting the following section of your code in viewDidLoad so that the audioPlayer instance is ready to play before it is asked to actually play the audio:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"Cricket_Sound" ofType:#"mp3"];
audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
audioPlayer.numberOfLoops = 0;