自定义 Segue 不播放声音
Custom Segue doesn't Play Sound
我做了一个自定义转场,我希望它在改变视图时播放声音。不幸的是,事实并非如此。当我在 View Controller 中使用方法 playSound 时,它工作正常。但我不想在我需要的每个视图控制器中导出它。我如何改进我的代码以及为什么 segue 不想播放声音?
#import "CustomSegue.h"
@import AVFoundation;
@interface CustomSegue()
@property (nonatomic, strong) AVAudioPlayer *player;
@end
@implementation CustomSegue
@synthesize player;
- (void) perform
{
UIViewController *src = (UIViewController *) self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;
[UIView transitionWithView:src.navigationController.view duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[src.navigationController pushViewController:dst animated:NO];
}
completion:NULL];
[self playSound];
}
- (void) playSound
{
int r = arc4random_uniform(7) + 1;
NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%i", r] ofType:@"caf"]];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[self.player prepareToPlay];
[self.player play];
}
我打赌你的自定义 segue 被释放了(ARC) before the sound has a chance to play, and your audio player is released with it. The Life Cycle of a Segue 是这样的,它在 -perform
执行后立即被释放。
您需要做的是让音频播放器保持更长时间。一种方法是从一个对象添加对 AVAudioPlayer 的(强)引用,该对象至少在声音播放期间不会被释放。例如,将播放器放在您的应用委托中,只要应用处于 运行.
,它就会一直存在
尝试将 player
属性 和 -playsound
方法移动到您的应用委托中。 属性 应该类似于:
@property (strong, nonatomic) AVAudioPlayer *player;
然后,在这个 segue 中,代替 [self playsound];
调用应用程序委托,例如:
MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate playsound];
我做了一个自定义转场,我希望它在改变视图时播放声音。不幸的是,事实并非如此。当我在 View Controller 中使用方法 playSound 时,它工作正常。但我不想在我需要的每个视图控制器中导出它。我如何改进我的代码以及为什么 segue 不想播放声音?
#import "CustomSegue.h"
@import AVFoundation;
@interface CustomSegue()
@property (nonatomic, strong) AVAudioPlayer *player;
@end
@implementation CustomSegue
@synthesize player;
- (void) perform
{
UIViewController *src = (UIViewController *) self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;
[UIView transitionWithView:src.navigationController.view duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[src.navigationController pushViewController:dst animated:NO];
}
completion:NULL];
[self playSound];
}
- (void) playSound
{
int r = arc4random_uniform(7) + 1;
NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%i", r] ofType:@"caf"]];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[self.player prepareToPlay];
[self.player play];
}
我打赌你的自定义 segue 被释放了(ARC) before the sound has a chance to play, and your audio player is released with it. The Life Cycle of a Segue 是这样的,它在 -perform
执行后立即被释放。
您需要做的是让音频播放器保持更长时间。一种方法是从一个对象添加对 AVAudioPlayer 的(强)引用,该对象至少在声音播放期间不会被释放。例如,将播放器放在您的应用委托中,只要应用处于 运行.
,它就会一直存在尝试将 player
属性 和 -playsound
方法移动到您的应用委托中。 属性 应该类似于:
@property (strong, nonatomic) AVAudioPlayer *player;
然后,在这个 segue 中,代替 [self playsound];
调用应用程序委托,例如:
MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate playsound];