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

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

Related

Play iOS built-in sounds with AVAudioPlayer

Is is possible to play built-in sounds with AVAudioPlayer, to get around the limitations of AudioServicesPlaySystemSound?
For example:
AudioServicesPlaySystemSound(1360);
vs.
AVAudioPlayer
NSURL* url = [[NSBundle mainBundle] URLForResource:#"???" withExtension:#"??"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[player play];
What would the path or code be (if possible)?

Audio Normalize in xcode (iOS)

I'm trying to build a music player for iPhone and iPad.
I've used AVFoundation for playback controls. But I'm trying to implement audio normalization as "Sound Check" in "Music" settings in my player, I couldn't find any helpful resource for the same. Any suggestion would be helpful.
Simple answer: This is not possible.
Expert answer: It is not possible with your chosen frameworks. You need to go down on the lower layer of Core Audio: Audio Units and AudioQueue. But here you will find C code and the need of some signal processing knowledge.
What you can easily do is to change the volume:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.volume = 0.5; // from 0.0 to 1.0
if(audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer 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]

AVAudioPlayer Sound Different between identical devices with identical settings

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)?

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.

Resources