dimanche 1 février 2015

Custom Segue doesn't Play Sound

I made a custom segue and I want it to play sound when changing views. Unfortunately, it doesn't. Method playSound works fine when I use it in View Controller. But i don't want to export it in every View Controller i need. How can i improve my code and why segue doesn't want to play sound?



#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];
}

Aucun commentaire:

Enregistrer un commentaire