在视图控制器之间移动后恢复音频播放 - iOS
resuming AVAudio playback after moving between ViewControllers - iOS
在我的项目中,我的 viewDidLoad 中播放了背景音乐,我还有另一个 viewController,当我转到 ViewController2 时,音频使用在 viewWillDisappear 中调用的 [player pause];
方法暂停。我的问题是,当我回到 ViewController1 时,音频会重新开始,而不是从停止的地方继续播放。
这是我的代码:
1) playMusic(在 ViewDidLoad 中调用):
-(void) playMusic{
NSError *error;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategorySoloAmbient error:&error];
NSString *path = [[NSBundle mainBundle]
pathForResource:kSongTitle ofType:kSongFormat];
NSURL *pathURL = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:pathURL error:&error];
[player setDelegate:self];
[player setNumberOfLoops:-1.0];
[player play];}
2) ViewWillDisappear:
-(void) viewWillDisappear:(BOOL)animated{
if (player.playing)
[player pause];
[super viewWillDisappear:YES];}
3) resumeMusic(在 viewWillAppear 中调用):
-(void)resumeMusic{
if (!player){
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:kSongfullTitle];
NSLog(@"Path to play: %@", resourcePath);
NSError *err;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:resourcePath] error:&err];
if (err)
NSLog(@"failed with reason: %@", [err localizedDescription]);
else{
player.delegate = self;
[player play];
}
} else {
NSLog(@"resuming");
[player play];
} }
嗯,player 为 nil 有点奇怪,所以首先,确保 player 定义为 strong
:
@property (strong,nonatomic) AVAudioPlayer * player;
如果是这种情况,我会建议换个角度。由于 ViewDidLoad
仅调用一次 - 当分配 viewController 时。如果你再次到达那里,就意味着整个viewController被释放了,你可以放心地重新分配player
(音频会重新开始,但你没有真正的选择)
所以我建议不要在 ViewDidLoad
中播放,而只在 ViewWillAppear
中播放(每次都会调用 - 也是第一次)
所以代码应该是:
-(void) initPlayer //called from ViewDidLoad
{
NSError *error;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategorySoloAmbient error:&error];
NSString *path = [[NSBundle mainBundle]
pathForResource:kSongTitle ofType:kSongFormat];
NSURL *pathURL = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:pathURL error:&error];
[player setDelegate:self];
[player setNumberOfLoops:-1.0];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[player play];
}
** 选项 2:**
如果您真的想让音频始终从该点恢复(无论如何)——这实际上取决于您要做什么——将 AVAudioPlayer
移至单例 (SharedInstance) 并从任何你需要的地方引用它
在我的项目中,我的 viewDidLoad 中播放了背景音乐,我还有另一个 viewController,当我转到 ViewController2 时,音频使用在 viewWillDisappear 中调用的 [player pause];
方法暂停。我的问题是,当我回到 ViewController1 时,音频会重新开始,而不是从停止的地方继续播放。
这是我的代码:
1) playMusic(在 ViewDidLoad 中调用):
-(void) playMusic{
NSError *error;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategorySoloAmbient error:&error];
NSString *path = [[NSBundle mainBundle]
pathForResource:kSongTitle ofType:kSongFormat];
NSURL *pathURL = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:pathURL error:&error];
[player setDelegate:self];
[player setNumberOfLoops:-1.0];
[player play];}
2) ViewWillDisappear:
-(void) viewWillDisappear:(BOOL)animated{
if (player.playing)
[player pause];
[super viewWillDisappear:YES];}
3) resumeMusic(在 viewWillAppear 中调用):
-(void)resumeMusic{
if (!player){
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:kSongfullTitle];
NSLog(@"Path to play: %@", resourcePath);
NSError *err;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:resourcePath] error:&err];
if (err)
NSLog(@"failed with reason: %@", [err localizedDescription]);
else{
player.delegate = self;
[player play];
}
} else {
NSLog(@"resuming");
[player play];
} }
嗯,player 为 nil 有点奇怪,所以首先,确保 player 定义为 strong
:
@property (strong,nonatomic) AVAudioPlayer * player;
如果是这种情况,我会建议换个角度。由于 ViewDidLoad
仅调用一次 - 当分配 viewController 时。如果你再次到达那里,就意味着整个viewController被释放了,你可以放心地重新分配player
(音频会重新开始,但你没有真正的选择)
所以我建议不要在 ViewDidLoad
中播放,而只在 ViewWillAppear
中播放(每次都会调用 - 也是第一次)
所以代码应该是:
-(void) initPlayer //called from ViewDidLoad
{
NSError *error;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategorySoloAmbient error:&error];
NSString *path = [[NSBundle mainBundle]
pathForResource:kSongTitle ofType:kSongFormat];
NSURL *pathURL = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:pathURL error:&error];
[player setDelegate:self];
[player setNumberOfLoops:-1.0];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[player play];
}
** 选项 2:**
如果您真的想让音频始终从该点恢复(无论如何)——这实际上取决于您要做什么——将 AVAudioPlayer
移至单例 (SharedInstance) 并从任何你需要的地方引用它