0

所以我创建了一个程序,当设备摇晃时会发出声音。有一个按钮,当点击该按钮时,可以随时停止播放声音(这是“重置”方法)。还有另一个按钮,按下时会显示另一个视图(这是“infoView”方法)。当我转到“infoView”然后返回第一个视图时会出现问题。摇动设备时声音仍然播放,但“重置”按钮变得无响应。这是我到目前为止所拥有的,任何帮助将不胜感激。

PS。我想知道它是否与FirstResponders有关?我仍在努力围绕 FirstResponders。

.h

#import <UIKit/UIKit.h>
#import "AVfoundation/AVfoundation.h"

@interface OldTvViewController : UIViewController{
AVAudioPlayer *audioPlayer;
}
-(IBAction)reset;
-(IBAction)infoView:(id)sender;
@end

他们

#import "OldTvViewController.h"
#import "AudioToolBox/AudioToolBox.h"
#import "infoViewController.h"

@implementation OldTvViewController

-(IBAction)infoView:(id)sender{
infoViewController *second = [[infoViewController alloc] initWithNibName:Nil bundle:Nil];
[self presentModalViewController:second animated:YES];
[second release];
}

-(BOOL) canBecomeFirstResponder{
return YES;
}

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{

if (event.subtype == UIEventSubtypeMotionShake) {

    //Play throw sound
    if(audioPlayer.playing){
        [audioPlayer stop];
    } else{
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/bomb.mp3",[[NSBundle mainBundle] resourcePath]]];
        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        audioPlayer.numberOfLoops = 0; 
        [audioPlayer play];    
    }

}

}


-(IBAction)reset{
[audioPlayer stop];
}


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

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

@end
4

1 回答 1

1

当您转到信息视图时,您可以在该类中的所有可用按钮处尝试此代码。

-(void)viewWillDisapeers
 {

  [audioPlayer stop];

 }

或添加此方法

-(IBAction)infoView:(id)sender
 {
 infoViewController *second = [[infoViewController alloc] initWithNibName:Nil bundle:Nil];
 [self presentModalViewController:second animated:YES];
 [audioPlayer stop];
 [second release];
 }
于 2012-05-04T04:22:05.007 回答