AVAudioPlayer not playing any sound

2020-01-26 03:29发布

I'm working on an iOS application that needs to play some sounds using the AVFoundation framework. The workspace structure in Xcode 4 contains two projects:

  • Workspace
    • The application itself (main project)
    • A utility library

After building the utility library, it results in a static library which is used in the main application as a framework.

So, when trying to play a sound inside the main application by 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];

In contrast, when trying to play exactly the same sound (or any other) inside the utility library using the same code as above, no sound is played at all, even though error is nil and the audioPlayer property values are the right ones (number of channels, duration).

I've made sure the AVFoundation framework is in both projects.

Also, 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 is called after trying to play the sound.

If I use the AudioToolbox framework instead, then it plays the sound. But I'm interested in using AVFoundation for several reasons.

Any idea of what is going on? Am I missing something about AVFoundation? Could it be related to using AVAudioPlayer from inside a static library?

Thanks in advance.

10条回答
冷血范
2楼-- · 2020-01-26 04:08

Please stay on the page for some time, I was facing same issue. and use sleep to resolve it.


NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                             pathForResource:@"sounds-1027-are-you-kidding"
                                             ofType:@"mp3"]];
        AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
                                      initWithContentsOfURL:url
                                      error:nil];
        [audioPlayer play];
        sleep(2);

查看更多
姐就是有狂的资本
3楼-- · 2020-01-26 04:10

Have you configured the AVAudioSession?

I had a similar problem and fixed it using something like this:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];

or

[audioSession setCategory:AVAudioSessionCategoryPlayback error:NULL];

Hope it helps.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-01-26 04:11

Yes, absolutely; ARC was the reason with my code as well.

I introduced a property:

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

and that made everything work out fine.

查看更多
仙女界的扛把子
5楼-- · 2020-01-26 04:15

Even having audioPlayer var declared in the class scope, it won't play. At least on iOS 10 iPhone 5c physical device.

play() result is true so no errors appear to happen.

Had to add this (Swift 3, 4) and now it plays the sound correctly.

let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayback)
查看更多
爷、活的狠高调
6楼-- · 2020-01-26 04:20

I have created open source project for simple audio player. Take a look at it if you have any problems with playing audio in iOS (in background too) : https://github.com/wzbozon/DKAudioPlayer

查看更多
贼婆χ
7楼-- · 2020-01-26 04:27

use this it will work definitely....

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];

NSURL *url = [NSURL fileURLWithPath:@"path of the sound file :)"];
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[_player setDelegate:self];
[_player prepareToPlay];
[_player play];
查看更多
登录 后发表回答