为什么切换视图时AVAudioPlayer stop方法不起作用

时间:2011-07-15 23:18:12

标签: iphone ios xcode avaudioplayer

我有一个iphone应用程序,可以显示图像并播放从主菜单中选择的音频。

用户单击按钮以选择他们想要的图像/音频组合。用动画切换视图的代码工作正常。

在新视图中显示图像,播放,暂停,清理和停止音频的所有代码也可以正常工作。

然而,当用户点击主菜单按钮时,我希望播放音频停止。我正在使用viewWillDisappear:(BOOL)动画来调用stop方法:

-(void)viewWillDisappear:(BOOL)animated {
audioPlayer.stop;
[super viewWillDisappear: animated];}

当用户切换回主菜单视图时,此代码不会停止声音。有一个更好的方法吗?我做错了吗?

以下是上面代码段所在的整个类的代码:

#import "twelthPoem.h"

UIImageView *largeImageView;

@implementation twelthPoem

-(void)resetControls
{
audioPlayer.currentTime = 0;
scrubber.value = 0;
[playButton setImage:[UIImage imageNamed:@"play_HL.png"] 
    forState:UIControlStateNormal];
}

-(void)play:(id)sender {


if (! audioPlayer.playing)  {
audioPlayer.play;
[playButton setImage:[UIImage imageNamed:@"pauseHL.png"] forState:UIControlStateNormal];
}
else {
audioPlayer.pause;
[playButton setImage:[UIImage imageNamed:@"play_HL.png"]          forState:UIControlStateNormal];
}

[self becomeFirstResponder];
}

-(void)stop:(id)sender {

audioPlayer.stop;
[self resetControls];
}

-(void)changeVolume:(id)sender {

audioPlayer.volume = volume.value;
[self becomeFirstResponder];
}

-(void)scrub:(id)sender {

if (audioPlayer.playing) {

audioPlayer.pause;
audioPlayer.currentTime = scrubber.value;
audioPlayer.play;
}
else
audioPlayer.currentTime = scrubber.value;
[self becomeFirstResponder];
}

-(void)createControls {

//play/pause button
playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton setFrame:CGRectMake(60,405,80,20)];
[playButton setImage:[UIImage imageNamed:@"play_HL.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(play:) 
 forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];

//stop button
stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
[stopButton setFrame:CGRectMake(180,405,80,20)];
[stopButton setImage:[UIImage imageNamed:@"stopHL.png"] forState:UIControlStateNormal];
[stopButton addTarget:self action:@selector(stop:) 
 forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stopButton];

//volume control
volume = [[UISlider alloc] initWithFrame:CGRectMake(10,445,145,20)];
[volume addTarget:self action:@selector(changeVolume:) 
 forControlEvents:UIControlEventValueChanged];
volume.minimumValue = 0.0;
volume.maximumValue = 1.0;
volume.value = audioPlayer.volume;
volume.continuous = YES;
[self.view addSubview:volume];

//scrubber control
scrubber = [[UISlider alloc] initWithFrame:CGRectMake(165,445,145,20)];
[scrubber addTarget:self action:@selector(scrub:) 
   forControlEvents:UIControlEventValueChanged];
scrubber.minimumValue = 0.0;
scrubber.maximumValue = audioPlayer.duration;
scrubber.value = audioPlayer.currentTime;
scrubber.continuous = NO;
[self.view addSubview:scrubber];

}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {

switch (event.subtype)
{
case UIEventSubtypeRemoteControlTogglePlayPause:
    [self play:nil];
    break;

case UIEventSubtypeRemoteControlNextTrack:
    //do nothing
    break;

case UIEventSubtypeRemoteControlPreviousTrack:
    //do nothing
    break; 
}
}

- (BOOL)canBecomeFirstResponder {

return YES;
}

-(void)viewDidLoad {    

//for scrolling the image

[super viewDidLoad];

CGRect scrollFrame = CGRectMake(0,40,320,350);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 1.5;
scrollView.delegate = self;


UIImage *bigImage = [UIImage imageNamed:@"birches.png"];



largeImageView = [[UIImageView alloc] initWithImage:bigImage];


[scrollView addSubview:largeImageView];
scrollView.contentSize = largeImageView.frame.size; //important!



[self.view addSubview:scrollView];

[scrollView release];


//for playing the recording

NSString *filePath = [[[NSBundle mainBundle] resourcePath] 
          stringByAppendingPathComponent:@"birches_final_mp3.mp3"]; 
NSURL *fileURL = [NSURL fileURLWithPath:filePath];

NSError *error = nil;

OSStatus status = AudioSessionInitialize(NULL, NULL, NULL, NULL);    
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
status = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, 
                  sizeof (sessionCategory), 
                  &sessionCategory);
AudioSessionSetActive(YES);

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL 
                         error:&error];

      if (error )
    NSLog(@"An error occurred: %@",error);
        else
    {
    audioPlayer.volume = 0.3;
    [audioPlayer prepareToPlay];

    [self createControls];  
    } 

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return largeImageView;
}

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player 
{
    interrupted = audioPlayer.playing;
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player 
{
    if (interrupted)
    audioPlayer.play;
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player 
               successfully:(BOOL)flag 
{
    [self resetControls];
}




-(void)viewWillDisappear:(BOOL)animated {
    audioPlayer.stop;
    [super viewWillDisappear: animated];    

}

- (void)dealloc {

    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [scrubber release];
    [volume release];
    [audioPlayer release];

    [super dealloc];
}

@end

2 个答案:

答案 0 :(得分:2)

使用以下语法调用objective-c中的方法。

[audioPlayer stop];

audioPlayer.stop无效。

其他地方也一样。

答案 1 :(得分:0)

audioPlayer.stop主要不会起作用,因为它需要一个表达式,例如audioPlayer.stop = //expression,停止为bool,因此您可以说audioPlayer.stop = YES;[audioPlayer stop];