AVAudioPlayer 和 NSUserDefaults
[PlaySound stop];
[PlaySound prepareToPlay];
[[NSUserDefaults standardUserDefaults] synchronize];
text = [[NSUserDefaults standardUserDefaults] stringForKey:@"defaulttext"];
NSLog(@"%@",text);
BOOL loop = [prefs boolForKey:@"switch"];
NSLog(@" %d",loop);
do {
[PlaySound play];
} while (loop ==YES);
这段代码面临两个主要问题。
1)这是一个在点击按钮时调用的函数...我在此函数中分配音频播放器。每当调用此函数时,我都会在音频播放器上调用停止函数,但它不会停止并且两个声音同时播放。
2)我的默认设置没有正确注册。字符串和布尔值在控制台中都显示为零。
我在我的应用程序中添加了设置包。
编辑:其余的方法
NSUInteger selectedRow= [SoundPicker selectedRowInComponent:0];
NSString *userchoice =[self pickerView:SoundPicker titleForRow:selectedRow forComponent:0];
NSString *soundpath = [[NSBundle mainBundle] pathForResource:userchoice ofType:@"wav"];
NSURL *fileURl = [[NSURL alloc] initFileURLWithPath:soundpath];
NSError **error = nil;
PlayFartSound = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURl error:error];
我已经解决了音频播放器的问题。 任何人都可以帮忙解决 NSUserDefaults
问题吗?
在我的设置 root plist 中,我有一个带有标识符名称 name_preference
的文本字段,因此我将其放入代码中:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
text = [defaults stringForKey:@"name_preference"];
NSLog(@"%@ abc",text);
日志是 (null) abc
? [我已在plist中将默认值设置为hello
]
[PlaySound stop];
[PlaySound prepareToPlay];
[[NSUserDefaults standardUserDefaults] synchronize];
text = [[NSUserDefaults standardUserDefaults] stringForKey:@"defaulttext"];
NSLog(@"%@",text);
BOOL loop = [prefs boolForKey:@"switch"];
NSLog(@" %d",loop);
do {
[PlaySound play];
} while (loop ==YES);
There are two major problems I am facing with this code.
1) This is a function which is called when a button is tapped... I am allocating the audio player in this function. Whenever this function is called I called the stop function on audio player yet it does not stop and two sounds play simultaneously.
2) My defaults are not registering correctly. Both the string and bool are showing nil in console.
I haves added settings bundle in my app.
Edit: Rest of the method
NSUInteger selectedRow= [SoundPicker selectedRowInComponent:0];
NSString *userchoice =[self pickerView:SoundPicker titleForRow:selectedRow forComponent:0];
NSString *soundpath = [[NSBundle mainBundle] pathForResource:userchoice ofType:@"wav"];
NSURL *fileURl = [[NSURL alloc] initFileURLWithPath:soundpath];
NSError **error = nil;
PlayFartSound = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURl error:error];
I have sorted the problem of audio player.
Can anyone please help on NSUserDefaults
.
In my settings root plist I have a text field with identifier name name_preference
so I put this in code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
text = [defaults stringForKey:@"name_preference"];
NSLog(@"%@ abc",text);
The log is (null) abc
? [I have set the default value to hello
in plist]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你如何在 NSUserDefaults 中设置默认值?你说你已经在 plist 中获得了默认值,但是什么时候将该 plist 加载到 NSUserDefaults 中?
要设置默认的 NSUserDefaults 值,您需要在程序的早期某个地方调用它,最好是在 applicationDidFinishLaunching 之类的地方,
其中“defaults”是默认设置的字典,可能从 plist 加载。
How are you setting the default values in your NSUserDefaults? You say you've got the defaults in a plist, but when do you load that plist into NSUserDefaults?
To set the default NSUserDefaults values, you need to call this somewhere early in your program, ideally in somewhere like applicationDidFinishLaunching
Where 'defaults' is a dictionary of default settings, possibly loaded from a plist.