如何在目标C中为iPhone应用程序设置值?
我对Objective C非常陌生,正在为iPhone开发,所以如果这是一个简单的答案,我很抱歉。我已经尝试搜索谷歌搜索了3天了,买了iPhone开发版,但没有这样的运气 基本上,我想做的是在按下按钮时播放声音,然后我想在音频文件的值上加1。比如说如何在目标C中为iPhone应用程序设置值?,iphone,objective-c,audio,Iphone,Objective C,Audio,我对Objective C非常陌生,正在为iPhone开发,所以如果这是一个简单的答案,我很抱歉。我已经尝试搜索谷歌搜索了3天了,买了iPhone开发版,但没有这样的运气 基本上,我想做的是在按下按钮时播放声音,然后我想在音频文件的值上加1。比如说 //Play audio file (y.wav) ---y being the variable I would like to set. if (y > 13) { ---there are a total
//Play audio file (y.wav) ---y being the variable I would like to set.
if (y > 13) { ---there are a total of 14 sounds starting with file 0.wav going to 13.wav.
y = 0; ---so if the value is greater than 13 I want to start the cycle over with y equaling 0.
} else {
y++; ---if y is not greater than 13 I want to add one to it so it plays the next sound file next time.
}
我确实有一些JavaScript的历史,所以这个例子更多地反映了JavaScript而不是Objective-C。这个应用程序类似于iFart,但在你开始讨论我关于不制作另一个放屁/打嗝应用程序的问题之前,我的应用程序的这一部分也有类似的概念。因此,如果有人能帮助我学习如何将其转化为Objective-C,或者是一种完全不同的实现目标的方法,我将不胜感激
谢谢你,乔伊你会想这样做的:
for ( NSUInteger i = 0; i < 14; i++ ) {
NSString *filename = [ NSString stringWithFormat:@"%d.wav", i ];
// Code to play the sound
}
for(整数i=0;i<14;i++){
NSString*文件名=[NSString stringWithFormat:@“%d.wav”,i];
//播放声音的代码
}
还有其他方法可以做到这一点,但请查看NSString的
+stringWithFormat:
方法的开发人员文档。您需要执行以下操作:
for ( NSUInteger i = 0; i < 14; i++ ) {
NSString *filename = [ NSString stringWithFormat:@"%d.wav", i ];
// Code to play the sound
}
- (void) playMySound
{
static int soundIndex = 0;
const int soundCount = 14;
// create sound name from base string and wound index, as Jeff shows above
// play the sound
if (++soundIndex >= soundCount)
{
soundIndex = 0;
}
// or just do it in one line, viz
//soundIndex = (soundIndex + 1) % soundCount;
}
for(整数i=0;i<14;i++){
NSString*文件名=[NSString stringWithFormat:@“%d.wav”,i];
//播放声音的代码
}
还有其他方法可以做到这一点,但请查看NSString的+stringWithFormat:
方法的开发人员文档。每当您想要循环某个整数值时,最好在增量期间使用运算符。一般的模式是
- (void) playMySound
{
static int soundIndex = 0;
const int soundCount = 14;
// create sound name from base string and wound index, as Jeff shows above
// play the sound
if (++soundIndex >= soundCount)
{
soundIndex = 0;
}
// or just do it in one line, viz
//soundIndex = (soundIndex + 1) % soundCount;
}
counter = (counter + 1) % maxValue;
这将确保计数器始终在0和(maxValue-1)之间,这非常适合在包含maxValue元素的数组中循环
我还建议您在播放声音的实际数量和文件名方面要灵活。因此,您可以将声音的名称存储在NSARRY中
在你的课堂上,你应该有两个字段。包含声音名称的数组和包含要播放的当前声音编号的整数:
NSUinteger currentSound;
NSMutableArray *soundArray;
初始化数组。例如,在viewDidLoad中
currentSound = 0;
// get the base URL of the app bundle
NSURL *baseURL =[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
NSMutableArray *sounds = [[NSMutableArray alloc] init];
// remember to release the array and the player objects on dealloc.
// add sounds...
NSError *p_error = nil;
[sounds addObject: [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL URLWithString: @"1.wav" relativeToURL: baseURL] error: &p_error]];
// check error an initialize other sounds...
。。。现在,在按下按钮时触发的动作中,您可以
AVAudioPlayer *player = [soundArray objectAtIndex: currentSound];
currentSound = (currentSound + 1) % [soundArray count];
[player play];
模(%)运算符将工作,因此一旦currentSound等于数组中对象的计数,它将翻转回0。每当您想要循环某个整数值时,最好在增量期间使用该运算符。一般的模式是
counter = (counter + 1) % maxValue;
这将确保计数器始终在0和(maxValue-1)之间,这非常适合在包含maxValue元素的数组中循环
我还建议您在播放声音的实际数量和文件名方面要灵活。因此,您可以将声音的名称存储在NSARRY中
在你的课堂上,你应该有两个字段。包含声音名称的数组和包含要播放的当前声音编号的整数:
NSUinteger currentSound;
NSMutableArray *soundArray;
初始化数组。例如,在viewDidLoad中
currentSound = 0;
// get the base URL of the app bundle
NSURL *baseURL =[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
NSMutableArray *sounds = [[NSMutableArray alloc] init];
// remember to release the array and the player objects on dealloc.
// add sounds...
NSError *p_error = nil;
[sounds addObject: [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL URLWithString: @"1.wav" relativeToURL: baseURL] error: &p_error]];
// check error an initialize other sounds...
。。。现在,在按下按钮时触发的动作中,您可以
AVAudioPlayer *player = [soundArray objectAtIndex: currentSound];
currentSound = (currentSound + 1) % [soundArray count];
[player play];
模(%)操作符将工作,一旦currentSound等于数组中的对象数,它将返回到0。好的,这就是我得到的结果
- (void)viewDidLoad {
currentSound = 0;
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();
NSURL *baseURL =[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
NSMutableArray *sounds = [[NSMutableArray alloc] init];
NSError *p_error = nil;
[sounds addObject: [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL URLWithString: @"0.wav" relativeToURL: baseURL] error: &p_error]];
soundArray = sounds;
[super viewDidLoad];
}
- (IBAction)playFailSound {
AVAudioPlayer *player = [soundArray objectAtIndex: currentSound];
currentSound = (currentSound + 1) % [soundArray count];
[player play];
但是,我仍然不知道如何添加自己的声音文件。我需要在头文件中声明AVAudioPlayer吗?如果是的话,我该怎么做
我再次为把事情弄得过于复杂而道歉,但我正在努力,而且不倾向于与苹果的文档相处。好的,这就是我得到的
- (void)viewDidLoad {
currentSound = 0;
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();
NSURL *baseURL =[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
NSMutableArray *sounds = [[NSMutableArray alloc] init];
NSError *p_error = nil;
[sounds addObject: [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL URLWithString: @"0.wav" relativeToURL: baseURL] error: &p_error]];
soundArray = sounds;
[super viewDidLoad];
}
- (IBAction)playFailSound {
AVAudioPlayer *player = [soundArray objectAtIndex: currentSound];
currentSound = (currentSound + 1) % [soundArray count];
[player play];
但是,我仍然不知道如何添加自己的声音文件。我需要在头文件中声明AVAudioPlayer吗?如果是的话,我该怎么做
我再次为把事情弄得过于复杂而道歉,但我正在努力,而且不倾向于和苹果的文档相处