从计时器停止另一个视图控制器中的音频播放器

时间:2011-10-19 12:50:16

标签: ios uiviewcontroller triggers avaudioplayer nstimer

我在一个视图中运行了一个音频播放器,在另一个视图中播放了一个计时器,我希望在倒计时“myTicker”达到零时触发音频文件停止播放。

我尝试了各种各样的东西,包括分配视图控制器,只是触发一个停止命令,零等等。我没有把努力包括在内,因为我有兴趣看看我有多远。过。

以下是播放音频的视图控制器中的一些代码:

-(IBAction)stoneFireStop {

    [self.stoneFire stop];
    [self.bineuralDreamscape stop];
    [self.bineuralSleep stop];

}

- (void)viewDidLoad
{
    [super viewDidLoad];

NSString *stoneFireFilePath = [[NSBundle mainBundle] pathForResource:@"stoneFire" 
                                                                  ofType:@"m4a"];

    // Convert the file path to a URL.
    NSURL *stoneFireFileURL = [[NSURL alloc] initFileURLWithPath:stoneFireFilePath];

    //Initialize the AVAudioPlayer.
    self.stoneFire = [[AVAudioPlayer alloc] 
                      initWithContentsOfURL:stoneFireFileURL error:nil];

    // Preloads the buffer and prepares the audio for playing.
    [self.stoneFire prepareToPlay];
    stoneFire.numberOfLoops = -1;

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

}


- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    stoneFireView = nil;
    stoneFire = nil;
    bineuralDreamscape = nil;
    bineuralSleep = nil;

}

这里是视图中包含计时器'stuff'的代码:

- (void)showActivity{


    if (blackBg1.hidden) {
        blackBg1.hidden = NO;
    }
    if (blackBg2.hidden) {
        blackBg2.hidden = NO;
    }

    timerSecond = timerSecond - 1;
    inputSeconds = timerSecond;
    hours =  inputSeconds / 3600;
    minutes = ( inputSeconds - hours * 3600 ) / 60; 

    int secondz = timerSecond - (hours * 3600 + minutes * 60);

    if (timerSecond > 0) {
        secondsLabel.text = [NSString stringWithFormat:@"%d",secondz];
    }
    else {
        secondsLabel.hidden = YES;
    }

    NSLog(@"%d:%d:%d",hours,minutes,secondz);

    if (secondz == 59) {
        timer = [NSTimer timerWithTimeInterval:speed target:self selector:@selector(setMinuteInTimer) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

        if (minutes == 0 && hours == 0) {
            isAlarm = YES;

        }
    }
    if (secondz == 10 && minutes == 0 && hours == 0) {
        newTime = YES;
        redColorTurnOn = YES;
        timer = [NSTimer timerWithTimeInterval:speed target:self selector:@selector(setMinuteInTimer) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];       

        NSString *bgrnd = [NSString stringWithFormat:@"redBgrnd.png"];
        whiteBg1.image = [UIImage imageNamed:bgrnd];
        whiteBg2.image = [UIImage imageNamed:bgrnd];

    }

    if (secondz == 0 && minutes == 0 && hours == 0) {
        redColorTurnOn = NO;
        [myTicker invalidate];
    }

}

如前所述,所有帮助,想法等都将受到高度赞赏。如果您需要更多信息,我显然很乐意。

0 个答案:

没有答案