How to implement sound into an existent app, without Cocos2D

北慕城南 提交于 2019-12-06 05:57:41

问题


For example, on the iOS SDK download page, there's sample code; I'm using the calculator app (iPhoneUnitTests). I would like to know if it is possible to add sounds to the button presses on the app that's already built, easily.


回答1:


It is actually very simple to play short sounds, like button sounds. Here is a quick example.

You must link the AudioToolbox.framework binary

SoundExampleViewController.h;

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface SoundExampleViewController : UIViewController{
    SystemSoundID mySoundID;
}
@end

SoundExampleViewController.m:

#import "SoundExampleViewController.h"
@implementation SoundExampleViewController
- (void)viewDidLoad{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"SoundFileName" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&mySoundID);
}
- (void)viewDidUnload{
    [super viewDidUnload];
    AudioServicesDisposeSystemSoundID(mySoundID);
}
@end

Then to play you simply call: AudioServicesPlaySystemSound(mySoundID);



来源:https://stackoverflow.com/questions/8867414/how-to-implement-sound-into-an-existent-app-without-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!