AVAudioPlayer通知问题

时间:2011-10-02 16:27:41

标签: avaudioplayer avplayer

所以我差不多完成了我的应用程序而且我已经碰到了墙,无法解决这个最终问题。

我基本上有2个视图。主页面和游戏界面。

在第一次播放时游戏运行正常,但是当我离开主屏幕然后返回游戏屏幕时,所有声音都重复并且提前触发了playerItemDidReachEnd(因为我猜它有2个实例,但它似乎无法让玩家停止此操作。以下是导致此问题的基本代码。任何帮助都会有很长的路要走,谢谢。我不确定我的问题是我是在View1中创建View2的多个实例还是如果我在view2中创建多个播放器对象,从而复制通知。 我知道我的 - (void)playerItemDidReachEnd :( NSNotification *)通知中有很多内容,但它在第一次加载页面时工作正常,只有当我点击“返回view1”然后返回到View2问题发生了。

View1ViewController.h
----------------------
#import "(i know here are arrows here, but can't make them show)UIKit/UIKit.h>
#import "ApplicationViewController.h"

@interface MonsterSpellViewController : UIViewController {
}
-(IBAction)showView1;

View2ViewController.m
----------------------
-(IBAction)showView2{

    ApplicationViewController *view2 = [[ApplicationViewController alloc]initWithNibName:@"ApplicationViewController" bundle:nil];

    view2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:view2 animated:YES];

}



View2ViewController.h
------------------------
#import "(i know here are arrows here, but can't make them show)UIKit/UIKit.h>
#import "(i know here are arrows here, but can't make them show)AVFoundation/AVFoundation.h>

@class AVAudioPlayer;

@interface ApplicationViewController : UIViewController{

AVAudioPlayer *avPlayer;
}

View2ViewController.m
-------------------------
#import "View2ViewController.h"

@synthesize avPlayer;

-(AVAudioPlayer *)avPlayer {
    if(!avPlayer) avPlayer = [[AVAudioPlayer alloc]init];
    return avPlayer;
}

-(void) viewDidLoad
{

[[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(playerItemDidReachEnd:)
     name:AVPlayerItemDidPlayToEndTimeNotification
     object:avPlayer];
}

-(IBAction)playSoundTest
{
 NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/monster3.mp3", [[NSBundle mainBundle] resourcePath]]];

    self.avPlayer = [AVPlayer playerWithURL:url];

    [self.avPlayer play];
}

- (void)playerItemDidReachEnd:(NSNotification *)notification {
    //[player seekToTime:kCMTimeZero];
    if (playEscape == YES) {
        [self btnEscapeSound];
        playEscape = NO;
    }
    if ((startButton.hidden == NO) && (letterCount != -1) && (playFinal == YES))
    {
        [self btnFinalSound];
        playFinal = NO;
        playEscape = YES;
    }

    NSLog(@"Player Check accessed");
    if (letterCount == 3) {

        if (Winner == letterCount) {
            //moveNextSound = YES;
            if (intLoopCount < 104) 
            {
                if (intLoopCount<104) {


                    [self btnStartOver:intLoopCount];
                    playFinal = NO;
                    //intLoopCount++;
                }
                if (intLoopCount==104) {
                    startButton.hidden=NO;
                    playFinal = YES;

                }
            }
        }
    }

    if (letterCount == 4) {

        if (Winner == letterCount) {
            //moveNextSound = YES;
            if (intLoopCount < 105) 
            {
                [self btnStartOver:intLoopCount];
                //intLoopCount++;
                if (intLoopCount==105) {
                    startButton.hidden=NO;
                    playFinal = YES;
                }
            }
        }
    }





}
}

-(IBAction)goBack:(id)sender{


    [self dismissModalViewControllerAnimated:YES];

}

1 个答案:

答案 0 :(得分:0)

尝试添加[view2 release];之后[self presentModalViewController:view2 animated:YES];