简体   繁体   English

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

[英]iPhone: How to play a sound at a particular time?

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. 我想播放音频文件而不是振动iPhone。 Is that possible? 那可能吗?

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];
-(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;

}

Yes. 是。 Assume you have a .caf file, you can play it with 假设您有一个.caf文件,则可以使用

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

To play music you may want to use AVAudioPlayer instead. 要播放音乐,您可能需要改用AVAudioPlayer

Sure. 当然。 See AVAudioPlayer class. 请参见AVAudioPlayer类。

Check Apple's sample project BubbleLevel . 检查Apple的示例项目BubbleLevel You want the SoundEffect.h and SoundEffect.m into your project and then just call: 您希望将SoundEffect.h和SoundEffect.m放入您的项目中,然后调用:

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

Warning: I was able to play only AIF sounds converted via iTunes. 警告:我只能播放通过iTunes转换的AIF声音。 Anything else failed, including CAF files. 其他所有失败,包括CAF文件。 In short: import your whatever sound into iTunes (drag'n'drop), change export to AIF format and then export the file. 简而言之:将任何声音导入iTunes(拖放),将“导出”更改为AIF格式,然后导出文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM