问题
I'm in the early stages of developing my first iPad application, and for simplicity I have so far been using AudioServicesPlaySystemSound and the associated functions to play sounds. My code is based the SoundEffect class from Apple's Metronome example.
The specific symptom is that I can hear the sounds in the simulator but not on the device, though I have verified that I can hear sounds in other applications on the device. AudioServicesCreateSystemSoundID is returning valid sound identifiers, so it isn't anything as simple as the name of the sound file having different case, i.e. "sound.mp3" vs. "Sound.mp3".
I recognize that I may need to switch to a different library such as OpenAL for unrelated reasons, but I would like to know what is going on here. Does anyone have any suggestions? Is there a function I can call to get an OSStatus value or something?
* BUMP -- I've been working on other projects for the past few weeks but I'm back on this now and I'd really appreciate an answer. Thanks.
回答1:
I don't think you can play an mp3 file on the device using AudioServicesPlaySystemSound
. Take a look at the documentation for supported file types. I'm not sure why they play correctly in the simulator, but I had the same issue. Try using AVAudioPlayer instead:
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource: @"sound" withExtension: @"mp3"] error:&error];
if (error) {
NSLog(@"Error creating audio player: %@", [error userInfo]);
} else {
[audioPlayer play];
}
[audioPlayer release];
回答2:
I came across this question via a Google search. My problem is exactly as stated in the title of this question: "AudioServicesPlaySystemSound not working on iPad device". However, the description of the issue is different.
Simply put, after upgrading my iPad to iOS 4.3, AudioServicesPlaySystemSound stopped working.
I finally found the solution to my version of the issue, so hopefully this helps someone else.
Go to the Settings app on the iPad and choose General. Tap Sounds. Make sure "Change with Buttons" is set to ON. This will fix the issue.
来源:https://stackoverflow.com/questions/4036498/audioservicesplaysystemsound-not-working-on-ipad-device