Iphone 使用故事板和更改视图时,声音不会在新视图中停止播放
我正在尝试做一些类似于此问题中要求的事情: 我正在使用XCode 4.2的故事板。在我的初始视图页面上,我只有调用其他视图的按钮,并且没有声音在那里播放。这些其他视图包含一个UINavigationController,它带有一个按钮,允许您返回。其他视图页面将有允许播放声音的按钮。每个声音都有自己的播放按钮,还有恢复、暂停和停止按钮 所有的声音都播放得很好,但是如果我点击主页的后退按钮,声音就会继续播放。当通过“后退”按钮导航回主页面时,所需的行为是声音停止 在故事板出现之前,我可以很容易地编写一个后退按钮来停止音频,但是故事板看起来并没有那么简单。看来我必须实现prepareforsgue方法。好的,我可以设置Segue标识符,可以在属性检查器中设置,回到前面的文章,我将使用showPlayer 但我想我可以在我的一个声音视图中编写prepareForSegue,如下面这个很酷的示例所示: 在主页ViewController.m中,我尝试添加:Iphone 使用故事板和更改视图时,声音不会在新视图中停止播放,iphone,ios,xcode,storyboard,avaudioplayer,Iphone,Ios,Xcode,Storyboard,Avaudioplayer,我正在尝试做一些类似于此问题中要求的事情: 我正在使用XCode 4.2的故事板。在我的初始视图页面上,我只有调用其他视图的按钮,并且没有声音在那里播放。这些其他视图包含一个UINavigationController,它带有一个按钮,允许您返回。其他视图页面将有允许播放声音的按钮。每个声音都有自己的播放按钮,还有恢复、暂停和停止按钮 所有的声音都播放得很好,但是如果我点击主页的后退按钮,声音就会继续播放。当通过“后退”按钮导航回主页面时,所需的行为是声音停止 在故事板出现之前,我可以很容易地
@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(几乎是默认值):
SoundPage1.h:
#进口
#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
添加播放、停止和暂停按钮的代码
我试图格式化此文件,如果难以阅读,请提前道歉
谢谢你的建议!
Rob您应该停止视图中的AVAudioPlayer将消失
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if(theAudio && theAudio.isPlaying) {
[theAudio stop];
}
}
您应该停止ViewWill中的AVAudioPlayer
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if(theAudio && theAudio.isPlaying) {
[theAudio stop];
}
}
ViewDidUnload仅在设备达到内存容量极限时由程序调用,而不是在您更改视图时调用。这周我自己才发现了这一点,同时也在努力解决同样的问题
希望这能解决问题。只有当设备达到内存容量极限时,程序才会调用ViewDidUnload,而不是当您更改视图时。这周我自己才发现了这一点,同时也在努力解决同样的问题
希望这能把事情弄清楚。哇。。。我刚发现这有多容易。阻止声音在视图上播放的解决方案是通过VIEWWILLENGINE!!!-(void)视图将消失:(BOOL)动画{[self.navigationController setNavigationBarHidden:无动画:动画];[super view将消失:动画];[theAudio stop];}效果非常好!谢谢大家准备好了!罗布哇。。。我刚发现这有多容易。阻止声音在视图上播放的解决方案是通过VIEWWILLENGINE!!!-(void)视图将消失:(BOOL)动画{[self.navigationController setNavigationBarHidden:无动画:动画];[super view将消失:动画];[theAudio stop];}效果非常好!谢谢大家准备好了!罗布+1。奇怪的是,两个viewDidUnload都不能使用,没有视图控制器可以重用+1。奇怪的是,两个viewDidUnload都不能使用,视图控制器都不能重用
#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;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if(theAudio && theAudio.isPlaying) {
[theAudio stop];
}
}