代码之家  ›  专栏  ›  技术社区  ›  Tirth

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

  •  0
  • Tirth  · 技术社区  · 15 年前

    我写了以下代码:

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

    5 回复  |  直到 11 年前
        1
  •  0
  •   JOM agenthunt    15 年前

    检查 苹果的样板工程 BubbleLevel . 你想在你的项目中加入soundeffect.h和soundeffect.m,然后打电话给:

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

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

        2
  •  1
  •   TechZen    15 年前

    与主要问题无关,但计时器选择器调用的方法应为…

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

    ……或者它可能没有被正确地调用。选择器应该如下所示:

    myTicker=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(aribtaryMethodName:) userInfo:nil repeats:YES];
    
        3
  •  0
  •   kennytm    15 年前

    对.假设你有一个 .caf 文件,你可以玩它

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

    播放您可能要使用的音乐 AVAudioPlayer 相反。

        4
  •  0
  •   Costique    15 年前

    当然。请参阅avaudioplayer类。

        5
  •  0
  •   liza    11 年前
    -(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;
    
    }