问题
I am working on creating a very simple app. 2 buttons, both play sound. Each clip lasts 1 second and responds to "TouchUpInside". They are .caf files that I converted in iTunes.
Here is my code, once again, it works in the simulator but not on the device:
enter code here- (void)viewDidLoad {
NSLog(@"InView did load");
AudioServicesCreateSystemSoundID ((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"bigaif" ofType:@"caf"]], &systemSoundID);
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource: @"smallwav" ofType:@"wav"]],
&systemSoundIDTwo);
}
-(IBAction) playSound:(id) sender {
AudioServicesPlaySystemSound(systemSoundID);
}
-(IBAction) playSoundTwo:(id) sender {
AudioServicesPlaySystemSound(systemSoundIDTwo);
}
回答1:
After looking for errors in my code for the past 30 min I found that the mute button on the side of iPad was on. Check that before trying some fancy solution.
30 min well spent...
回答2:
I found my answer. I was using SystemSound and what do you know, I had my system sounds muted in my device settings.
回答3:
I had same issue. I didn't add a link reference to the AudioToolbox.framework but it still worked on simulator. I added it and it played in the foreground of my app on the device.
Then I wanted it to play the sound in the background. In info.plist
"Required background modes" add the key "plays audio". That got it working the background.
回答4:
This happened to me as well. Sounds were playing just fine in the simulator, but not on the device. I tried to play the sounds multiple different ways. I used both SystemSound and AVAudioPlayer with the same results. I even downloaded the code from a Mobile Orchard tutorial, and had the same result. Code example worked fine in simulator, but wouldn't play on the device.
After 2 hours of this, I double-tapped the home button on the iPad, scrolled to the left to the sound controls, and then tapped the speaker icon on the left. That placed a slash through the speaker icon to indicate that system volume was muted. I then tapped again, the slash went away, and I was then able to play sounds through the device.
So basically, I had to toggle through the muted state of the device using this method and that reset something on the iPad to where I could hear the sounds.
Note that this was on iOS6.
来源:https://stackoverflow.com/questions/7411467/why-does-audio-work-in-the-simulator-but-not-on-my-ipad