使用后退按钮故事板播放声音。目标C.

时间:2013-05-22 08:03:27

标签: objective-c audio uinavigationcontroller avaudioplayer

在Xcode的Storyboard模式中。使用导航控制器时,会自动显示后退按钮。

通常,为了播放我的声音,我会从选择器或动作中调用我的任务。

如何使用后退按钮播放此声音?如何对此进行编码,因为后退按钮会自动显示在我的视图中?

- (void)myTask {
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/pop.wav"];
NSLog(@"Path to play: %@", resourcePath);
NSError* err;

//Initialize our player pointing to the path to our resource
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
          [NSURL fileURLWithPath:resourcePath] error:&err];

if( err ){
    //bail!
    NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
    //set our delegate and begin playback
    player.delegate = self;
    [player play];
}
}

此致

布雷克

1 个答案:

答案 0 :(得分:1)

您可以在- (void)viewWillDisappear:(BOOL)animated { // in here }

中播放