AVAudioPlayer will not play audio in one project with all correct parameters, but will work fine in another project

StackOverflow https://stackoverflow.com/questions/22522605

  •  17-06-2023
  •  | 
  •  

Frage

I have a huge Xcode project, which I have been working on an update to. After installing the iOS 7.1 SDK, playing audio on the AVAudioPlayer in this project no longer works. I created a new, blank project to test out the exact same code, and it worked perfectly.

I know for certain that the file is copied under the bundle resources, the file is added to the target, the URL is perfect because I was able to get the NSData from the NSURL of the file, and it matched. The AVAudioPlayer is a property with both the strong and nonatomic attributes, but it will not play in this one project. I also made sure to set the AVAudioSession to playback mode.

I even created some blank view controller classes to test out the AVAudioPlayer in the project, and it would not work in any of them, but in the new, blank iOS project I made, the sounds plays fine.

In the .h

NSURL *soundUrl;
@property (nonatomic, strong) AVAudioPlayer *soundPlayer;

In the .m

@synthesize soundPlayer = _soundPlayer;

- (void)playSomeSound {
     NSError *audioError;
     soundUrl = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:soundFile ofType:@"m4a"]];
     _soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&audioError];
     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
     _soundPlayer.delegate = self;
     [_soundPlayer play];
}

soundFile is just a string of the name of the sound file that I am trying to play.

I try to log for errors in all the delegate methods of the audio player and in the initialization of the audio player. All errors return (null).

Unlike on iOS 7, where the audio simply does not play, on iOS 6.1, initializing the audio player causes a crash with an EXC_BAD_ACCESS code 2.

Also, the AVAudioPlayer in CocosDenshion seems to work. (I have cocos2d used in parts of my project, but it is not a game.)

Another (possibly important) note is that I use AVAudioRecorder in my project as well. That works perfectly without any issues, and I make sure to switch the AVAudioSessionCategory to playback when I am not recording.

War es hilfreich?

Lösung 4

Thanks everyone, but somehow it ended up being a conflict between some Frameworks. There was a bug in Kamcord SDK for iOS, but I am in contact with their development team, and they are working to fix it.

Andere Tipps

Its due to the change that apple made to AVAudioSession properties in iOS 7.

As per Apple's documentation:

The audio category has changed (e.g. AVAudioSessionCategoryPlayback has been changed to AVAudioSessionCategoryPlayAndRecord).

So change the line [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL]; to [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];

Considering the fact that exactly the same code works in your other project, it's hard to tell what is the problem in this project. But I can give you a couple of suggestions on further debugging it:

  1. Call your -playSomeSound as the first thing you do in application:didFinishLaunchingWithOptions:. This way you will see whether it's not working because of something else you do before calling it.
  2. Add another sound to your project, and try playing that one. Perhaps still the problem is with the original sound.

Also a couple questions:

  1. Where is audioError declared?
  2. What's the result of [_soundPlayer play] (should return YES on success)?

hava a try:

    [_soundPlayer prepareToPlay];
    [_soundPlayer play];

This issue is still apparent in the latest Kamcord SDK, so instead of everyone running in to this issue having to contact them, here's the smallest amount of code/changes you need to fix AVAudioPlayer, and to have Kamcord also include audio from AVAudioPlayer in it's recordings.


First, add OpenAL to your frameworks.

Import OpenAL headers in AppDelegate.m

#import <OpenAl/al.h>
#import <OpenAl/alc.h>

Set up static variables for OpenAL device and context:

static ALCdevice *openALDevice;
static ALCcontext *openALContext;

Create two functions to start and to stop OpenAL

- (void)startOpenAL
{
    NSLog(@"OpenAL: Starting");

    openALDevice = alcOpenDevice(NULL);
    openALContext = alcCreateContext(openALDevice, NULL);
    alcMakeContextCurrent(openALContext);
}
- (void)stopOpenAL
{
    NSLog(@"OpenAL: Stopping");

    alcMakeContextCurrent(NULL);
    alcDestroyContext(openALContext);
    alcCloseDevice(openALDevice);
}

In AppDelegate.m's didFinishLaunchingWithOptions, start OpenAL (I do it before initializing Kamcord)

[self startOpenAL];

Finally, in applicationWillTerminate, call the stop OpenAL

[self stopOpenAL];

You're done. AVAudioPlayer should now start working again and all of your audio is correctly being recorded by Kamcord.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top