AVAudioPlayer Sound Different between identical devices with identical settings - ios

I play a sound effect using:
-(void)playCorrectSound
{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Yes" ofType:#"mp3"];
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
if(! self.myAudioPlayer || ![soundURL isEqual:self.myAudioPlayer.url]){
self.myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
self.myAudioPlayer.delegate = self;
}
[self.myAudioPlayer setVolume:1.0];
[self.myAudioPlayer prepareToPlay];
[self.myAudioPlayer play];
}
I've added this code to three devices, all iPhone 5, all iOS 7.0.6 Two of them play the sound at significantly lower volume than another phone. All System preferences appear the same across all three devices. Would anyone know why one would be so much louder (and how I want it)?

Related

AVAudioPlayer - Why is there a gap between loops (uncompressed audio)

NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"caf"];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[audioPlayer setNumberOfLoops:-1];
[audioPlayer setVolume: 0.1];
[audioPlayer play];
This is the basic code I've got, let me know if you need to know more.
tried both compressed and uncompressed formats both have a gap of about ~1 second when running on iPhone simulator (don't have dev license to run on device to test).
Am i missing something, I've been searching posts but i can only see issues with compressed audio (which makes sense).
Xcode: Version 6.2 (6C131e)
OS: OS X Yosemite Version 10.10.2 (14C1510)
Hardware: Macbook Air 13" (Early 2014) - 4GB
have you try a "prepareToPlay" before playing like this :
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"caf"];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[audioPlayer setNumberOfLoops:-1];
[audioPlayer setVolume: 0.1];
[audioPlayer prepareToPlay];
[audioPlayer play];
I had the same issue and I solve it using AVQueuePlayer, Create one AVQueuePlayer player and try to buffer (enqueue) two or three AVPlayerItem with mp3 media files in advance in a lazy mode via AVPlayerActionAtItemEnd

Volume on my device doesn't effect the volume

.h
SystemSoundID sound1;
.m
NSURL *soundURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Woosh" ofType:#"mp3"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL1, & sound1);
AudioServicesPlaySystemSound(sound1);
This is the only code i use to play my Woosh.mp3 sound. I ran the app on my iPad and when i changed the volume on the device, the volume of the sound didn't change. I was wondering if there might be another way to implement sound into the app, or add the capability to change volume to this way.
NSURL *soundurl = [[NSBundle mainBundle] URLForResource: #"mysound" withExtension: #"caf"];
AVAudioPlayer *mySoundPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:soundurl error:&error];
mySoundPlayer .volume=1.0f; //between 0 and 1
[mySoundPlayer prepareToPlay];
mySoundPlayer.numberOfLoops=0; //or more if needed
[mySoundPlayer play];

Playing a sound in background

I have read some threads about this topic, and currently have this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *soundFilePath = [NSString stringWithFormat:#"%#/testsoundsr.aiff",
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath: soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
}
The sound is not played though, and I can only imagine that the problem is with the NSString definition which is used to reference the file
The file I'm trying to play is named 'testsoundsr.aiff' and I have dragged that into the project. Also I have added several frameworks to the project, including AudioToolBox.framework
---UPDATE---
I have now tried using an .mp3 file instead, but still getting no sound through.
Try player.delegate = self before [player play]

xcode 5 no sound from mp3 on iOS 6 or earlier

The app plays some mp3 files and is working well on iOS 7, but on iOS 6 or earlier, the mp3 files are not played or at least I can hear nothing.
This is the code I use for playing mp3:
fileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/puerta.m4a", [[NSBundle mainBundle] resourcePath]]];
//Initialize the AVAudioPlayer.
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
// Preloads the buffer and prepares the audio for playing.
[self.audioPlayer prepareToPlay];
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
Does anyone had a similar issue and how fixed it - Many thanks for help.

Pick songs from plist(NSArray) and play using AVAudioPlayer?

I'm student working on a coding exercise and I am stumped!
I have two instances of AVAudioPlayer in my app - I can load the songs into each player using a direct path no problem.
What I'd like to be able to do is play multiple songs held within an array on each player.
Is this possible? and if so how would I go about doing this?
In my plist I have the key set to "One.mp3" as a string and the value as the path to the file... (was guessing at that part).
Thanks for any insight.
- (void)viewDidLoad {
[super viewDidLoad];
//multiple song array
NSString *soundsPath = [[NSBundle mainBundle] pathForResource:#"soundslist"
ofType:#"plist"];
soundsList = [[NSArray alloc] initWithContentsOfFile:soundsPath];
NSString* filename = [soundsList objectAtIndex:0];
NSString *path = [[NSBundle mainBundle] pathForResource:filename
ofType:#"mp3"];
AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:NULL];
self.audioPlayer = newAudio; // automatically retain audio and dealloc old file if new file is loaded
[newAudio release]; // release the audio safely
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer setNumberOfLoops:0];
[audioPlayer play];
}
It sounds like you want to try to play multiple songs on a single player. This isn't possible as stated in the AVAudioPlayer Overview (4th bullet). It is possible to play multiple songs with multiple AVAudioPlayers. Something like this:
NSMutableArray *audioPlayers = [NSMutableArray arrayWithCapacity:[soundsList count]];
for (NSString *soundPath in soundsList) {
NSError *error = nil;
AVAudioPlayer *audioPlayer = [AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
[audioPlayers addObject:audioPlayer];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer setNumberOfLoops:0];
[audioPlayer play];
}
I would suggest that you hardcode the file path instead of reading it in with a plist. Once you get that working, then you can try reading it in with the plist. Something like this would work:
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:#"One" withExtension:#"mp3"];
AVAudioPlayer *soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]
This way, you remove one level of indirection and one point of failure.

Resources