我正在使用AVPlayer
。一段时间后播放音频。我希望在点击UIButton
之后播放声音(音频),我想播放视频。我怎样才能做到这一点?任何人都可以帮助我吗?
-(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 :(得分:2)
创建新班级
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()