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。那可能吗?

4

5 回答 5

1

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

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

...或者它可能无法正确调用。它的选择器应该是这样的:

myTicker=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(aribtaryMethodName:) userInfo:nil repeats:YES];
于 2010-02-01T12:57:06.457 回答
0

是的。假设你有一个.caf文件,你可以用

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

要播放音乐,您可能想AVAudioPlayer改用。

于 2010-02-01T12:17:08.500 回答
0

当然。请参阅 AVAudioPlayer 类。

于 2010-02-01T12:18:02.653 回答
0

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

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

警告:我只能播放通过 iTunes 转换的 AIF 声音。其他任何事情都失败了,包括 CAF 文件。简而言之:将您的任何声音导入 iTunes(拖放),将导出更改为 AIF 格式,然后导出文件。

于 2010-02-02T08:10:23.987 回答
0
-(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;

}
于 2013-03-23T17:00:55.750 回答