1

2 つのビューとビデオ プレーヤーとオーディオ プレーヤーがあります。最初のビューのボタンが押されたとき。その後、オーディオとビデオ プレーヤーの再生が開始されます。そして、映画の再生が停止した後。次のビューが表示されます。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 

そしてmovieplayerクラスは

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

1 に答える 1

0

問題は、コードの 2 番目のブロックにあります。

- (void) viewWillAppear:(BOOL)animatedメソッドには次の行があります。

[self playSound:@"taiko_1"];

これにより、ビューが表示されるたびにサウンドが再生されます。これには、別のビュー コントローラーを閉じた後の 2 回目の表示も含まれます。

一度だけ再生したい場合は、次のように別の場所に移動する必要がありますviewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    [self playSound:@"taiko_1"];
}
于 2012-07-27T08:28:19.193 に答える