AVAudioPlayer 正在再次播放而不被称为播放。

Posted

技术标签:

【中文标题】AVAudioPlayer 正在再次播放而不被称为播放。【英文标题】:AVAudioPlayer is playing again without being called play. 【发布时间】:2012-07-27 06:50:12 【问题描述】:

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

- (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 

电影播放器​​类是

- (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

【问题讨论】:

【参考方案1】:

问题出在您的第二个代码块中:

- (void) viewWillAppear:(BOOL)animated 方法中包含以下行:

[self playSound:@"taiko_1"];

这使它在每次显示视图时播放声音,包括在关闭不同的视图控制器后第二次显示。

如果您只想播放一次,则需要将其移动到其他位置,例如 viewDidLoad

- (void)viewDidLoad 
    [super viewDidLoad];
    [self playSound:@"taiko_1"];

【讨论】:

以上是关于AVAudioPlayer 正在再次播放而不被称为播放。的主要内容,如果未能解决你的问题,请参考以下文章

AVAudioPlayer 重置当前播放的声音并从头开始播放

使用 AVAudioPlayer 在后台播放声音而不停止 ipod 音乐

AVAudioPlayer 在我的 iPhone 上播放短音?

AVAudioPlayer 不认为它在播放时

AVAudioPlayer认为它不在播放中

如何播放与 AVAudioPlayer 重叠的相同声音?