简体   繁体   中英

Playing Multiple MP3 files with Multiple Buttons - iPhone SDK - xCode

I am writing a small soundboard app for the Movie The ringer and I am new to the iPhone SDK

I have linked up all the buttons to their Mp3 files with the following code however only some of the buttons play their sounds?

I have checked all of the file names and they all match the code:


- (IBAction)canWeBeAlone:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundfileURLRef;
    soundfileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Can We Be Alone", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID (soundfileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

- (IBAction)itsThreeAM:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundfileURLRef;
    soundfileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Its 3AM", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID (soundfileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

- (IBAction)iHaveSinned:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundfileURLRef;
    soundfileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"I Have Sinned", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID (soundfileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

- (IBAction)mowMyLawn:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundfileURLRef;
    soundfileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Mow My Lawn", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID (soundfileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

- (IBAction)theyAreMyFriends:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundfileURLRef;
    soundfileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"They Are My Friends", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID (soundfileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

- (IBAction)itsSteve:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundfileURLRef;
    soundfileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Its Not Jeffy Its Steve", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID (soundfileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

- (IBAction)itWasWrongOfMe:(id)sender {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundfileURLRef;
    soundfileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"It Was Wrong of Me", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID (soundfileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

Any Help Would Be Appreciated

I believe that you should use AVAudioPlayer to play MP3 files instead of AudioServicesPlaySystemSound. AVAudioPlayer supports playing "...multiple sounds simultaneously, one sound per audio player, with precise synchronization". See the AVAudioPlayer Class Reference. Here's also a link to a project showing how you use AVAudioPlayer: [http://www.techotopia.com/index.php/Playing_Audio_on_an_iPhone_using_AVAudioPlayer_(iOS_4)][1]

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