2010-08-27 69 views
0

我有一个根视图和一个详细视图,在详细视图中是一个带有基本播放/暂停按钮设置的AVAudioPlayer。所有的作品,但是如果我播放文件,然后单击回(popViewControllerAnimated)到根目录,该文件继续播放哪个是正确的,但如果我点击回到详细视图的视图已重置,并没有找到AVAudioPlayer。在NavigationController中查看数据/实例变量查看

详细page.h

@interface DetailPageViewController : UIViewController <AVAudioPlayerDelegate>{ 
UIButton *arrowBtn; 
UIButton *playButton; 
UIButton *pauseButton; 
AVAudioPlayer *appSoundPlayer; 
NSURL *soundFileURL; 
BOOL playing ; 
UILabel *timeLabel; 
NSTimer *timer; 
} 

@property (nonatomic, retain) IBOutlet UIButton *arrowBtn; 
@property (nonatomic, retain) IBOutlet UIButton *playButton; 
@property (nonatomic, retain) IBOutlet UIButton *pauseButton; 
@property (nonatomic, retain) AVAudioPlayer *appSoundPlayer; 
@property (nonatomic, retain) NSURL *soundFileURL; 
@property (nonatomic, retain) IBOutlet UILabel *timeLabel; 
@property (readwrite) BOOL playing; 
@property (nonatomic, retain) NSTimer *timer; 

-(IBAction)playPauseToggle:(id)sender; 
-(IBAction)returnToRoot; 

@end 

详细page.m

@synthesize arrowBtn, playButton, pauseButton, playing, appSoundPlayer, soundFileURL, timeLabel, timer; 

... 

-(IBAction)playPauseToggle:(id)sender 
{ 
if (playing == NO) 
{ 
    [appSoundPlayer play]; 
    playing = YES; 
    [playButton setHidden: YES]; 
    [pauseButton setHidden: NO]; 

} 
else { 
    [appSoundPlayer pause]; 
    playing = NO; 
    [playButton setHidden: NO]; 
    [pauseButton setHidden: YES]; 
} 
} 

-(IBAction)returnToRoot 
{ 
[self.navigationController popViewControllerAnimated:YES]; 
} 

- (void)viewDidLoad { 
[super viewDidLoad]; 

if (self.appSoundPlayer == nil) 
{ 
    playing = NO; 

    [playButton setHidden: NO]; 
    [pauseButton setHidden: YES]; 

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"soundFile" ofType:@"mp3"]; 

    NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 
    self.soundFileURL = newURL; 
    [newURL release]; 

    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil]; 
    self.appSoundPlayer = newPlayer; 
    [newPlayer release]; 

    [appSoundPlayer prepareToPlay]; 
    [appSoundPlayer setVolume: 1.0]; 
    [appSoundPlayer setDelegate: self]; 
} 

}

我如何坚持要么appSoundPlayerplaying当我再次查看视图。

回答

2

您需要在RootViewController上保留对appSoundPlayer对象的引用,并在加载它时以某种方式将其传递给您的详细视图。

其实BOOL playing没有必要,因为它是AVAudioPlayer的财产,你可以随时访问它。

RootViewController.h

@interface RootViewController : UIViewController { 
    AVAudioPlayer *_appSoundPlayer; 

// Declare the other iVars 
} 

@property (nonatomic, retain) AVAudioPlayer *appSoundPlayer; 

@end 

RootViewController.m

@synthesize appSoundPlayer = _appSoundPlayer; 

- (void)dealloc { 
    [self setAppSoundPlayer:nil]; 
    // Dealloc the other properties that you may have 

    [super dealloc]; 
} 
/* 
** Your code 
*/ 

// When presenting the detail view 
- (void)openDetailView { 
    DetailPageViewController *viewController = [[DetailPageViewController alloc] initWithParentViewController:self]; 
. 
. 
. 
    // Present the view controller 
} 

在您DetailPageViewController.h

@interface DetailPageViewController : UIViewController <AVAudioPlayerDelegate> { 
RootViewController *_parentViewController; 
    // Declare the iVars 
} 

@property (nonatomic, assign) RootViewController *parentViewController; 

- (id)initWithParentViewController:(RootViewController *)parentViewController; 

@end 

DetailPageViewController.m

@synthesize parentViewController = _parentViewController; 

- (void)dealloc { 
    // Set the delegate to nil 
    [self.parentViewController.appSoundPlayer setDelegate:nil]; 

    // Dealloc the other properties that you may have 

    [super dealloc]; 
} 

- (id)initWithParentViewController:(RootViewController *)parentViewController { 
    if (self = [super init]) { 
     self.parentViewController = parentViewController; 
    } 
    return self; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    if (self.parentViewController.appSoundPlayer == nil) { 
     [playButton setHidden: NO]; 
     [pauseButton setHidden: YES]; 

     NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"soundFile" ofType:@"mp3"]; 

     NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 
     self.soundFileURL = newURL; 
     [newURL release]; 

     AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil]; 
     [newPlayer prepareToPlay]; 
     [newPlayer setVolume: 1.0]; 
     [newPlayer setDelegate: self]; 
     self.parentViewController.appSoundPlayer = newPlayer; 
     [newPlayer release]; 
    } 
} 

我还没有测试过这个代码,但是你可以对你需要做什么有一个很好的想法。当然,还有其他方法可以做到这一点。

+0

谢谢你的答案。我开始得到它,但得到以下错误: '重复成员_parentViewController','警告:没有-initWithParentViewController:initWithNibName:捆绑:'找到方法' & '不兼容的Objective-C类型返回'结构UIViewController *',预计'结构RootViewController *' 是否有关系,我使用的是DetailPage的initWithNibName?像'DetailPageViewController * detailPage = [[DetailPageViewController alloc] initWithParentViewController:self initWithNibName:@“DetailPage”bundle:nil];'? – daidai 2010-08-27 05:33:35

+0

@daidai,你得到一个警告,因为你正在寻找的方法不存在,你必须创建方法-initWithParentViewController:initWithNibName:bundle :,基于我创建的方法-initWithParentViewController: – vfn 2010-08-27 05:58:09

+0

谢谢,固定。但它仍然抱怨'重复成员'_parentViewController',但我改变(在DetailPageViewController.h)'RootViewController * _parentViewController;'到'RootViewController * parentViewController;'它编译,但崩溃(与警告本地声明'parentViewController'隐藏实例变量和属性'parentViewController'类型不符合超类'UIViewController'属性类型。感谢您的帮助,我慢慢理解了原理。 – daidai 2010-08-27 06:25:10