MPMusicPlayerController行为不正常。 IOS

时间:2014-11-05 11:17:08

标签: nstimer nsnotificationcenter mpmusicplayercontroller

我正在使用MPMusicPlayerController播放3个音乐文件。我的要求是,在完成第一个文件后,它应该保持给定的时间间隔,然后播放下一个音乐文件。但它在完成第一个文件之前开始播放下一个文件。

这是我的代码:

-(void) playMusic
{
    self.player.repeatMode = MPMusicRepeatModeNone;

    MPMusicPlayerController *controller = [MPMusicPlayerController systemMusicPlayer];
    MPMediaItemCollection *collection;
    if(self.counter == 0){
         collection = [[MPMediaItemCollection alloc] initWithItems:[NSArray arrayWithObjects:self.firstItem, nil]];
        [controller setNowPlayingItem:self.firstItem];

    }else if(self.counter == 1){
        collection = [[MPMediaItemCollection alloc] initWithItems:[NSArray arrayWithObjects:self.secondItem, nil]];
        [controller setNowPlayingItem:self.secondItem];

    }else{
        collection = [[MPMediaItemCollection alloc] initWithItems:[NSArray arrayWithObjects:self.thirdItem, nil]];
        [controller setNowPlayingItem:self.thirdItem];

    }
    [controller setQueueWithItemCollection:collection];
    self.player = controller;
    [self.player prepareToPlay];
    [self.player play];

}

这就是我在通知方法中所做的:

-(void) nowPlayingNotification:(NSNotification*)notification
{
    if(self.counter == 0 && self.player.nowPlayingItem == self.firstItem){
        self.counter = 1;
        self.playerTimer = [NSTimer scheduledTimerWithTimeInterval:(float)[self.defaults doubleForKey:SECOND_TIMER_VALUE_KEY] target:self selector:@selector(playMusic) userInfo:nil repeats:NO];
        NSLog(@"%f",[self.defaults doubleForKey:SECOND_TIMER_VALUE_KEY]);

    }else if (self.counter == 1 && self.player.nowPlayingItem == self.secondItem){
        self.counter = 2;
        self.playerTimer = [NSTimer scheduledTimerWithTimeInterval:(float)[self.defaults doubleForKey:THIRD_TIMER_VALUE_KEY] target:self selector:@selector(playMusic) userInfo:nil repeats:NO];
        NSLog(@"%f",[self.defaults doubleForKey:THIRD_TIMER_VALUE_KEY]);
    }else{
        self.counter = 0;
        [self.player stop];
    }
}

0 个答案:

没有答案