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

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

Related

How to play a wav file placed in Documents directory in OSX using objective c

I have been trying to play an audio file but unable to succeed.
SoundFilePath:
/Users/admin/Library/Containers/com.abc.myApp/Data/Documents/MyAudioSample1.wav
NSSound:
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:soundFilePath byReference:YES];
sound.volume = 1;
sound.delegate = self;
[sound play];
Only once out of 5 times my audio is played.
AVFoundation:
NSURL* file = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[audioPlayer prepareToPlay];
Add [audioPlayer play];
You are preparing to play but not playing it. After adding above line your code will be:
NSURL* file = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayeralloc]initWithContentsOfURL:file error:nil];
[audioPlayer prepareToPlay];
[audioPlayer play];
Hope it helps

audioPlayer sound played on IPhone different than IPAD (Iphone is low volume and has to be held to ear)

I am playing a sound file in an APP and on the iPhone plays with volume like it is a phone call (you have to hold the phone to your ear).
When played on the iPad it plays like you would expect, sounds like a music file being played.
Does anyone know how to make the iPhone play a sound like it is music playing?
Here is what I use to play the sound:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/30perMinute2.caf", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog(#"selection didn't work");
else {
// [audioPlayer stop];
[audioPlayer play];
}
I have set the volume in audioPlayer.
Here is whatr ended up fixing the problem:
In AppDelegate.h
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
In AppDelegate.m
In application didFinishLaunchingWithOptions add
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

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

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,

not able to play audio from internet using AVAudioPlayer

i tried to play an audio file from internet using AVAudioplayer but there is no response from UIView but if i m to play an mp3 file from bundle it works ..cud u guys help me out below is the code...
-(IBAction)playPause:(id)sender
{
NSURL *url=[NSURL URLWithString:#"http://www.1040communications.net/sheeba/stepheni/iphone/images/today.mp3"];
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!player) NSLog(#"Error: %#", error);
[player prepareToPlay];
[player play];
}
The URL has to be a file URL, see Apple’s Technical QA1634. You can download the file to the device and then play it locally.
your url is not returning any data.That's why it is not playing.

Audio playback does not start

NSError *err;
// Initialize audio player
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
audioPlayer.delegate = self;
[audioPlayer play];
With the code above, I'm trying to initialize playback of a .mp3 file, however the playback does not start at all. There is no sound. What am I doing wrong? I have inspected 'err' and there is nothing there.
Edit: After adding AVAudioSession, I'm getting the following error from AVAudioPlayer
The operation couldn’t be completed. (OSStatus error -43.)
Apparently AVAudioPlayer does not support streaming via. HTTP as I was trying to do, so by using AVPlayer instead, I got it working.
Did you initialize a AVAudioSession?
The following works fine for me. Might give you a hint of whats not working for you.
Initializing:
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryAmbient error:nil];
[session setActive:TRUE error:nil];
Loading:
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
audioPlayer.numberOfLoops = 0;
[audioPlayer prepareToPlay];
audioPlayer.volume = 1.0;
Playing:
[audioPlayer play];
Update
To find the right NSRUL for fileUrl, I used this code:
NSURL* fileUrl = [NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"MySound" ofType:#"wav"] isDirectory:NO];
And then I added MySound.wav to the project (bundle).