使用故事板和更改视图时,声音不会在新视图中停止播放

时间:2012-04-28 04:27:01

标签: iphone ios xcode storyboard avaudioplayer

我正在尝试做类似于此问题中所要求的内容: Playing audio with controls in iOS

我正在使用带有XCode 4.2的Storyboard。在我的初始视图页面上,我只有调用其他视图的按钮,并且没有播放声音。这些其他视图包含一个带有BarButtonItem的UINavigationController,允许您返回。其他视图页面将具有允许播放声音的按钮。每个声音都有自己的播放按钮,还有恢复,暂停和停止按钮。

所有声音都可以正常播放,但如果我点击主页面的后退按钮,声音会继续播放。所需的行为是通过后退按钮导航回主页时声音停止。

在故事板之前,我可以轻松编写后退按钮来停止音频,但故事板看起来并不那么简单。看来我必须实现prepareForSegue方法。好的,我可以设置Segue标识符可以在Attributes Inspector中设置,并参考前面的帖子,我将使用showPlayer。

但是我想我可以在我的一个声音视图中编写prepareForSegue代码,如下面这个很酷的例子所示: http://www.scott-sherwood.com/?p=219

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if([[segue identifier] isEqualToString:@"showPlayer"]){
     SoundPageController *theAudio = (SoundPageController *)[segue ViewController];
     theAudio.delegate = self;
 }
 }

在主页面ViewController.m中,我尝试添加:

  @property (strong, nonatomic) AVAudioPlayer *theAudio;

我尝试添加类似的内容:

  - (void)viewDidLoad {
[super viewDidLoad];

// this is the important part: if we already have something playing, stop it!
if (audioPlayer != nil)
{
    [audioPlayer stop];
}
// everything else should happen as normal.
audioPlayer = [[AVAudioPlayer alloc]
           initWithContentsOfURL:url
           error:&error];

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

我无法让它工作,所以我将更改恢复原状。我将在下面提供简化代码,如果您有正确的建议,请告诉我!

ViewController.h:

@interface ViewController : UIViewController {      
}

ViewController.m(非常默认):

    #import "ViewController.h"

#import "AppDelegate.h"

@implementation ViewController

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{

[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

}

SoundPage1.h:     #import

#import <AVFoundation/AVAudioPlayer.h>

@interface occupy : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer* theAudio;
}

//-(IBAction)PressBack:(id)sender; now the back button is linked up through storyboard
-(IBAction)pushButton1;
-(IBAction)pushButton2;
-(IBAction)play;
-(IBAction)stop;
-(IBAction)pause;
@end

SoundPage1.m

#import "SoundPage1.h"
#import "AppDelegate.h"
#import "ViewController.h"

@implementation SoundPage1

-(IBAction)pushButton {

NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
if(theAudio)[theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]    error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
[theAudio play];
 }

-(IBAction)pushButton2 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"mp3"];
if(theAudio)[theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
[theAudio play];
}

// Old code from XCode 3.x when creating a back button that could stop the audio was easy
//-(IBAction)PressBack:(id)sender{
//  [theAudio stop];
//  ViewController* myappname = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];  
//  [self.navigationController pushViewController:myappname animated:NO];
//  }


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

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

添加代码以播放,停止和暂停按钮。

我试着格式化这个,如果难以阅读,请提前抱歉。

感谢您的任何建议! 罗布

2 个答案:

答案 0 :(得分:3)

你应该在viewWillDisappear中停止AVAudioPlayer。

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    if(theAudio && theAudio.isPlaying) {
        [theAudio stop]; 
    }
}

答案 1 :(得分:2)

ViewDidUnload仅在设备达到内存容量结束时由程序调用,而不是在您更改视图时调用。本周才发现了这一点,同时也遇到了同样的问题。

希望能够解决问题。