2016-08-09 80 views
0

Ich benutze AVPlayer. Es spielt Audio nach einiger Verzögerung. Ich möchte einen Ton abspielen, während ich danach auf einen UIButton klicke (Audio), ich möchte ein Video abspielen. Wie kann ich das machen? Kann mir jemand helfen?How to Play Audios ohne Verzögerung mit AVPlayer

-(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)]; 

} 
+0

spielen möchten Was Sie Audio oder Video wiedergeben möchten ? Und fügen Sie Ihren Code in Frage, was Sie versucht haben! – Lion

+0

Bitte zeigen Sie Ihre Bemühungen und fügen Sie etwas Code von dem, was Sie versucht haben. –

+0

Ihr Code zum Abspielen von Audio beim Klicken auf die Schaltfläche sieht gut aus und fügt keine Verzögerung hinzu. Wenn Sie jedoch eine Verzögerung erhalten, haben Sie überprüft, ob die Audiodatei keine anfängliche Stille aufweist? Wenn nicht, dann könnte etwas anderes den Haupt-Thread aufhalten, den Sie überprüfen müssten, ob –

Antwort

2

Erstellen Sie neue Klasse

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() 
} 
} 

Relativ Viewcontroller laden der Ton

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

spielen, wo Sie

testSound.play() 
+0

Um weitere Informationen hinzuzufügen: Der Schlüssel erstellt den AVAudioPlayer einmal auf App (oder Sicht) Initialisierung und dann nur aufrufen. Play() jedes Mal, wenn Sie es brauchen. –