简体   繁体   中英

Audio and vibrate working in simulator not on ios device

First off, I'm fairly new with objective C. I want to play a sound and vibrate under a certain condition. My iPhone 4 is not on mute, I've checked case sensitivity and used both AVAudioPlay and SystemSound to try and play the file but even the vibrate will not work. AudioToobox and AVfoundation are imported in .h. The audio plays with both methods on the iOS simulator.

Heres the code using SystemSound:

if (al == 1){
CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef =CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Alarm", CFSTR ("wav"), NULL);
        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);
        AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);}

And with AVAudioPlayer:

if (al == 1) {NSString *path = [[NSBundle mainBundle] pathForResource:@"alarmtwo" ofType:@"wav"];
        AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];        
        theAudio.numberOfLoops = 0;
        theAudio.volume = 1.5;
        [theAudio prepareToPlay];
        [theAudio play];}

By the way, the variable al sets to 0 after it plays. Is my coding wrong to have the sound and vibrate work on the device itself? Is there a specific type of .wav file that needs to be played? Any help would be appreciated.

I was running into this same issue. There is actually a second volume setting specially for alerts that at least in my case was set to mute, even though my regular volume was up. Take a look under the sounds menu in the Settings app on the device and turn that up.

You probably don't want to use the alerts for an actual released app though if it is important that the sound actually play, since it will require users to make sure this separate audio setting is up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM