我有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
和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 :(得分:0)
问题在于你的第二段代码:
- (void) viewWillAppear:(BOOL)animated
方法中包含以下行:
[self playSound:@"taiko_1"];
这使得它在每次显示视图时播放声音,包括在解除其他视图控制器后第二次显示。
如果您只想播放一次,那么您需要将其移至其他地方,例如viewDidLoad
:
- (void)viewDidLoad {
[super viewDidLoad];
[self playSound:@"taiko_1"];
}