I am working on an iOS application that should play some sounds using the AVFoundation framework. The workspace structure in Xcode 4 contains two projects:
- Workspace
- The application itself (main project)
- Utility library
After creating the utility library, it creates a static library, which is used as a framework in the main application.
Thus, when you try to play sound inside the main application using the code below, it works as expected.
NSString *resourcePath = [[NSBundle mainBundle] resourcePath]; NSString *path = [NSString stringWithFormat:@"%@/sound.mp3", resourcePath]; NSURL *url = [NSURL fileURLWithPath:path]; NSError *error = nil; AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; [audioPlayer play];
On the contrary, when you try to play exactly the same sound (or any other) inside the utility library using the same code as above, the sound does not play at all, even if the error is zero, and the values โโof the audio player properties are correct (number of channels, duration) .
I made sure that the AVFoundation is in both projects.
In addition, my class uses the AVAudioPlayerDelegate protocol and implements these two methods:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag; - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error;
None of these methods are called after trying to play sound.
If I use the AudioToolbox structure instead, then it plays the sound. But I am interested in using AVFoundation for several reasons.
Any idea what is going on? Am I missing something about AVFoundation ? Could this be due to using AVAudioPlayer from inside the static library?
Thanks in advance.
objective-c xcode4 automatic-ref-counting avfoundation audio
msoler Nov 04 2018-11-11T00: 00Z
source share