0

I'm using AVPlayer. It plays audio after some delay. I want to play a sound while clicking on a UIButton after that(audio), I want to play a Video. How can I do that? Can anyone help me?

-(IBAction) someMethod3:(id) sender {
 [self Playaudio];
 }

-(voidPlayaudio {

 NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"tap" ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
NSError *error;
musicplayer =  [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
musicplayer.delegate = self;
[musicplayer prepareToPlay];
[musicplayer play];
}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{

[self playVideo:indexvalue];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mainView:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

}

-(void) playVideo:(int)index {

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:[_videos objectAtIndex:index] ofType: nil];
url = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVPlayerItem *currentItem = [AVPlayerItem playerItemWithURL:url];
playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.videoGravity = AVLayerVideoGravityResize;
playerViewController.view.frame = self.view.bounds;
playerViewController.showsPlaybackControls = NO;
video = [AVPlayer playerWithPlayerItem:currentItem];
playerViewController.player = _video;
playerViewController.view.userInteractionEnabled = false;
[playerViewController.player play];
self.view.autoresizesSubviews = YES;

[self.view addSubview:playerViewController.view];
}

-(void)Mainview:(NSNotification*)notification {
UIButton * Button = [[UIButton alloc] initWithFrame: CGRectMake(10, 50, 30,20)];

 }
Marcos Crispino
  • 8,018
  • 5
  • 41
  • 59
Bharu
  • 159
  • 1
  • 3
  • 13
  • What you want to play audio or video? And add your code in question what you have tried! – Ketan Parmar Aug 09 '16 at 12:42
  • Please show your effort and add some some code of what you tried. – Teja Nandamuri Aug 09 '16 at 12:43
  • Your code to play the audio on button click looks fine and won't add delay. If you're getting a delay though, have you checked there's no initial silence gap on the audio file? If not, then something else may be holding up the main thread, which you would need to check for – Jim Tierney Aug 09 '16 at 15:15
  • everything works fine. but, after redirecting to the mainView in audioPlayerDidFinishPlaying, audio is not playing as expected. It plays after a little delay – Bharu Aug 10 '16 at 03:58

1 Answers1

2

Create new class

import AVFoundation

class Sound {

var audioPlayer = AVAudioPlayer()

init(name: String, type: String) {
    let coinSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(name, ofType: type)!)
    do {
        audioPlayer = try AVAudioPlayer(contentsOfURL: coinSound)
        audioPlayer.prepareToPlay()
    } catch let error as NSError {
        print(error.description)
    }
}

func play() {
    audioPlayer.play()
}

func stop() {
    audioPlayer.stop()
}
}

In relative viewcontroller load the sound

 let testSound = Sound(name: "alarm", type: "caf")

Play where you want to play

testSound.play()
Sofeda
  • 1,361
  • 1
  • 13
  • 23
  • To add some further info: the key is creating the AVAudioPlayer once on app (or view) initialisation and then only calling .play() each time you need it. – Ali Beadle Aug 09 '16 at 16:53