2012-07-27 82 views
1

我有2个视图和视频播放器和一个音频播放器。当按下第一个视图上的按钮时。然后音频和视频播放器开始播放。电影停止播放后。下一个视图出现。当我按下第二个视图上的相同音频播放按钮时。不知道从哪里开始AVAudioPlayer正在播放而不被称为播放。

- (id) init { 
if (self = [super init]) { 
    movieName = @"03"; 
    self.view = [[[OtsugeView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; 
} 
return self; 
} 

- (void) toNext { 
NSLog(@"OtsugeViewController:toNext"); 
[self.navigationController popViewControllerAnimated:NO]; 
} 

- (void) toToppage 
{ 
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];   

[self.navigationController popToRootViewControllerAnimated:NO]; 
} 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
NSLog(@"Screen touch Otsuge View"); 
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
delegate:self 
cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil   otherButtonTitles:@"Retry", @"Main Menu", nil]; 
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent; 

actionSheet.cancelButtonIndex = 0; 
[actionSheet showInView:self.view]; // show from our table view (pops up in the middle of the table) 
[actionSheet release]; 

}

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex 
{ 
switch (buttonIndex) { 
    case 0: // Retry 
     [self presentModalViewController:mMoviePlayer animated:YES]; 
     [self play]; 
     break; 
    case 1: // Main Menu 
     [self toToppage]; 
     break; 
    case 2: // Cancel 
     break; 
    default: 
     break; 
} 
} 

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

mMoviePlayer.moviePlayer.backgroundView.backgroundColor = [UIColor blackColor]; 
[self playSound:@"taiko_1"]; 
[(OtsugeView *)self.view renewImageView]; 
} 

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

- (void) dealloc { 
[super dealloc]; 
} 

@end 

和movieplayerclass是

- (NSURL *)createURL 
{ 
NSURL *mvURL; 
NSBundle *bundle = [NSBundle mainBundle]; 
if (movieName != nil) { 
    if (bundle) { 
     NSString *mvPath = [bundle pathForResource:movieName ofType:@"m4v"]; 
     if (mvPath) { 
      mvURL = [NSURL fileURLWithPath:mvPath]; 
     } 
    } 
} 
return mvURL; 
} 

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { 
[aAudioPlayer setDelegate:nil]; 
[aAudioPlayer release]; 
NSLog(@"MovieViewController:audioHasFinished"); 
NSLog(@"%@", aAudioPlayer.url); 
} 

- (void)playSound:(NSString *)file { 
NSURL *avURL; 
NSString *avPath = [[NSBundle mainBundle] pathForResource:file ofType:@"m4a"]; 
if (avPath) { 
    avURL = [NSURL fileURLWithPath:avPath]; 
    aAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:avURL error:nil]; 
    [aAudioPlayer setDelegate:self]; 
    [aAudioPlayer play]; 
    NSLog(@"MovieViewController:playSound"); 
} 
} 

- (void)toNext { 
// implementation sub classes 
NSLog(@"MovieViewController:toNext"); 
} 

- (void) clearVideo{ 
[[NSNotificationCenter defaultCenter] removeObserver:self 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:mMoviePlayer.moviePlayer]; 
[mMoviePlayer release]; 
mMoviePlayer = nil;  
} 

- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{ 
NSLog(@"MovieViewController:moviePlaybackDidFinish"); 

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];   
[[NSNotificationCenter defaultCenter] removeObserver:self 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:mMoviePlayer.moviePlayer]; 
[self dismissModalViewControllerAnimated:YES]; 
[mMoviePlayer release]; 
mMoviePlayer = nil; 
mPlayerPushed = NO; 
[self toNext]; 
} 

- (void) moviePreloadDidFinish : (NSNotification *)notification{ 
[self prepareFinished]; 
[[NSNotificationCenter defaultCenter] removeObserver:self 
               name:MPMoviePlayerLoadStateDidChangeNotification 
               object:mMoviePlayer.moviePlayer];  
} 

- (void) prepareFinished{ 

} 

- (void) initPlayer{ 
if (mMoviePlayer != nil) { 
    [mMoviePlayer release]; 
} 

mMoviePlayer = [[MoviePlayerViewController alloc] initWithContentURL:[self createURL]]; 
// Added 3.2 versions 
[[NSNotificationCenter defaultCenter] removeObserver:mMoviePlayer 
               name:MPMoviePlayerPlaybackDidFinishNotification object:mMoviePlayer.moviePlayer]; 
[mMoviePlayer.moviePlayer setShouldAutoplay:NO]; 
mMoviePlayer.moviePlayer.backgroundView.backgroundColor = [UIColor blackColor]; 
mMoviePlayer.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
mMoviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone; 

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(moviePlayBackDidFinish:) 
               name:MPMoviePlayerPlaybackDidFinishNotification 
              object:mMoviePlayer.moviePlayer]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(moviePreloadDidFinish:) 
               name:MPMoviePlayerLoadStateDidChangeNotification 
              object:mMoviePlayer.moviePlayer]; 
mPlayerPushed = YES; 
} 


- (void) play { 
NSLog(@"MovieViewController:play"); 
[mMoviePlayer.moviePlayer prepareToPlay]; 
[mMoviePlayer.moviePlayer play]; 
} 

- (void)viewWillAppear:(BOOL) animated { 
if (!mMoviePlayer) { 
    [self initPlayer]; 
} 
[super viewWillAppear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (void)didReceiveMemoryWarning { 
NSLog(@"memory error!"); 
// Releases the view if it doesn't have a superview. 
//[super didReceiveMemoryWarning]; 

// Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
} 

- (void)dealloc { 
[nextController release]; 
[movieName release]; 
[super dealloc]; 
} 

@end 

回答

0

的问题是在你的代码的第二块:

- (void) viewWillAppear:(BOOL)animated方法有以下行:

[self playSound:@"taiko_1"]; 

这使得它播放声音,每次显示视图,包括在解散不同的视图控制器后再次显示。

如果你只是想要玩一次,那么你需要到别的地方移动它像viewDidLoad

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self playSound:@"taiko_1"]; 
} 
+0

这篇帮助你吗? – lnafziger 2012-07-29 04:41:38