iPhone:如何在特定时间播放声音?

发布于 2024-08-19 20:15:45 字数 717 浏览 5 评论 0原文

我编写了以下代码:

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深白境迁sunset 2024-08-26 20:15:45

与主要问题无关,但计时器选择器调用的方法应该采用以下形式...

-(void) aribtaryMethodName:(NSTimer *) theTimer;

...否则可能无法正确调用。它的选择器应该如下所示:

myTicker=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(aribtaryMethodName:) userInfo:nil repeats:YES];

Tangential to the main question but the method called by the timer selector should be of the form...

-(void) aribtaryMethodName:(NSTimer *) theTimer;

...or it may not be called properly. It's selector should look like this:

myTicker=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(aribtaryMethodName:) userInfo:nil repeats:YES];
豆芽 2024-08-26 20:15:45

检查Apple的示例项目BubbleLevel 。您希望将 SoundEffect.h 和 SoundEffect.m 放入您的项目中,然后只需调用:

mySound = [[SoundEffect alloc]
    initWithContentsOfFile:[mainBundle pathForResource:@"mySound"
    ofType:@"aif"]];
[mySound play];    

警告: 我只能播放通过 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:

mySound = [[SoundEffect alloc]
    initWithContentsOfFile:[mainBundle pathForResource:@"mySound"
    ofType:@"aif"]];
[mySound play];    

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.

强辩 2024-08-26 20:15:45

是的。假设您有一个 .caf 文件,您可以使用它来播放它。

SystemSoundID sound_id;
AudioServicesCreateSystemSoundID(NSURL_of_your_caf_file, &sound_id);
...
AudioServicesPlaySystemSound(sound_id);

要播放音乐,您可能需要使用 AVAudioPlayer 来代替。

Yes. Assume you have a .caf file, you can play it with

SystemSoundID sound_id;
AudioServicesCreateSystemSoundID(NSURL_of_your_caf_file, &sound_id);
...
AudioServicesPlaySystemSound(sound_id);

To play music you may want to use AVAudioPlayer instead.

剧终人散尽 2024-08-26 20:15:45

当然。请参阅 AVAudioPlayer 类。

Sure. See AVAudioPlayer class.

乖乖公主 2024-08-26 20:15:45
-(IBAction)slider1Changed:(id)sender
{
    [timer invalidate];
    float sliderValue=slider1.value;
    int totalSecond=(int)sliderValue;
    time=totalSecond;
    int sec= totalSecond %60;
    int min= (totalSecond -sec)/60;
    sliderValue=(float)totalSecond;
    runTime.text=[NSString stringWithFormat:@"%d:%.2d",min,sec];
    [slider1 setValue:sliderValue];
    timer =   [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerChanged) userInfo:nil repeats:YES];

    // to play the audio from particular time duration
    [player setCurrentTime:totalSecond];
}

-(void)timerChanged
{
   int totalSecond=[player duration];

   float slider1Time=(float)time;
   if (time <= totalSecond) {
      int sec= time %60;
      int min= (time -sec)/60;
      runTime.text=[NSString stringWithFormat:@"%d:%.2d",min,sec];
      [slider1 setValue:slider1Time];
   }
   else
   {
      [player stop];
      [timer invalidate];
   }
   time +=1;

}
-(IBAction)slider1Changed:(id)sender
{
    [timer invalidate];
    float sliderValue=slider1.value;
    int totalSecond=(int)sliderValue;
    time=totalSecond;
    int sec= totalSecond %60;
    int min= (totalSecond -sec)/60;
    sliderValue=(float)totalSecond;
    runTime.text=[NSString stringWithFormat:@"%d:%.2d",min,sec];
    [slider1 setValue:sliderValue];
    timer =   [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerChanged) userInfo:nil repeats:YES];

    // to play the audio from particular time duration
    [player setCurrentTime:totalSecond];
}

-(void)timerChanged
{
   int totalSecond=[player duration];

   float slider1Time=(float)time;
   if (time <= totalSecond) {
      int sec= time %60;
      int min= (time -sec)/60;
      runTime.text=[NSString stringWithFormat:@"%d:%.2d",min,sec];
      [slider1 setValue:slider1Time];
   }
   else
   {
      [player stop];
      [timer invalidate];
   }
   time +=1;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文