Ios 如何使用AVPlayer毫不延迟地播放音频
我使用的是Ios 如何使用AVPlayer毫不延迟地播放音频,ios,objective-c,avplayer,avplayerviewcontroller,Ios,Objective C,Avplayer,Avplayerviewcontroller,我使用的是AVPlayer。它在延迟一段时间后播放音频。我想在单击ui按钮时播放声音,然后(音频),我想播放视频。我该怎么做?有人能帮我吗 -(IBAction) someMethod3:(id) sender { [self Playaudio]; } -(voidPlayaudio { NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"tap" ofType: @"mp3"]; NSURL *fil
AVPlayer
。它在延迟一段时间后播放音频。我想在单击ui按钮时播放声音,然后(音频),我想播放视频。我该怎么做?有人能帮我吗
-(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)];
}
创建新类
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()
}
}
在相对viewcontroller中加载声音
let testSound = Sound(name: "alarm", type: "caf")
在你想玩的地方玩
testSound.play()
创建新类
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()
}
}
在相对viewcontroller中加载声音
let testSound = Sound(name: "alarm", type: "caf")
在你想玩的地方玩
testSound.play()
您想播放什么音频或视频?并添加您尝试过的有问题的代码!请展示您的努力,并添加一些您尝试的代码。单击按钮播放音频的代码看起来很好,不会增加延迟。如果您遇到延迟,您是否检查过音频文件上没有初始静音间隔?如果没有,那么可能有其他东西挡住了主线程,您需要检查是否一切正常。但是,在AudioPlayerDidFinishPlay中重定向到主视图后,音频未按预期播放。它在稍微延迟后播放您想要播放的音频或视频是什么?并添加您尝试过的有问题的代码!请展示您的努力,并添加一些您尝试的代码。单击按钮播放音频的代码看起来很好,不会增加延迟。如果您遇到延迟,您是否检查过音频文件上没有初始静音间隔?如果没有,那么可能有其他东西挡住了主线程,您需要检查是否一切正常。但是,在AudioPlayerDidFinishPlay中重定向到主视图后,音频未按预期播放。它在稍微延迟后播放添加一些进一步的信息:关键是在应用程序(或视图)初始化时创建一次AVAudioPlayer,然后每次需要时只调用.play()。要添加一些进一步的信息:关键是在应用程序(或视图)初始化时创建一次AVAudioPlayer,然后每次需要时只调用.play()。