iPhone:如何在特定时间播放声音?
我编写了以下代码:
-(void)runTimer
{
myTicker=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
myTicker = [NSTimer scheduledTimerWithTimeInterval:60.00 target:self selector:@selector(vibrate) userInfo:nil repeats:YES];
}
-(void)showActivity
{
NSDateFormatter *formatter =
[[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate date];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
[timerLabel setText:[formatter stringFromDate:date]];
}
-(void) vibrate{
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}
我想播放音频文件而不是振动 iPhone。这可能吗?
I have written the following code:
-(void)runTimer
{
myTicker=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
myTicker = [NSTimer scheduledTimerWithTimeInterval:60.00 target:self selector:@selector(vibrate) userInfo:nil repeats:YES];
}
-(void)showActivity
{
NSDateFormatter *formatter =
[[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate date];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
[timerLabel setText:[formatter stringFromDate:date]];
}
-(void) vibrate{
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}
I want to play an audio file instead of vibrating the iPhone. Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
与主要问题无关,但计时器选择器调用的方法应该采用以下形式...
...否则可能无法正确调用。它的选择器应该如下所示:
Tangential to the main question but the method called by the timer selector should be of the form...
...or it may not be called properly. It's selector should look like this:
检查Apple的示例项目BubbleLevel 。您希望将 SoundEffect.h 和 SoundEffect.m 放入您的项目中,然后只需调用:
警告: 我只能播放通过 iTunes 转换的 AIF 声音。其他任何内容都失败,包括 CAF 文件。简而言之:将您的任何声音导入 iTunes(拖放),将导出更改为 AIF 格式,然后导出文件。
Check Apple's sample project BubbleLevel. You want the SoundEffect.h and SoundEffect.m into your project and then just call:
Warning: I was able to play only AIF sounds converted via iTunes. Anything else failed, including CAF files. In short: import your whatever sound into iTunes (drag'n'drop), change export to AIF format and then export the file.
是的。假设您有一个
.caf
文件,您可以使用它来播放它。要播放音乐,您可能需要使用
AVAudioPlayer
来代替。Yes. Assume you have a
.caf
file, you can play it withTo play music you may want to use
AVAudioPlayer
instead.当然。请参阅 AVAudioPlayer 类。
Sure. See AVAudioPlayer class.