//Action to play Audio//
-(IBAction)playAudio:(id)sender {
[self.loopPlayer play];
}
//Action to stop Audio//
-(IBAction)stopAudio:(id)sender {
if (self.loopPlayer.isPlaying) {
[self.loopPlayer stop];
self.loopPlayer.currentTime = 0;
self.loopPlayer.numberOfLoops = -1;
[self.loopPlayer prepareToPlay];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Code that gets audio file "trap synth"//
NSURL* audioFileURL = [[NSBundle mainBundle] URLForResource:@"trapsynth" withExtension:@"wav"];
self.loopPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];
}这是我和一个按钮一起使用的代码,当按钮被点击时播放声音,当按钮被释放时停止声音。我该如何为更多的按钮添加更多的声音呢?我想有更多的按钮,播放和停止不同的声音,就像这样。
property (nonatomic, strong) AVAudioPlayer *loopPlayer;此代码也在我的ViewController.h文件中。
发布于 2014-02-09 05:34:55
好的,虽然Miro提供的答案在写轨道上,但是给出的代码示例有问题。
应该是在viewDidLoad里-
- (void)viewDidLoad {
[super viewDidLoad];
NSURL* audioFileURL1 = [[NSBundle mainBundle] URLForResource:@"trapsynth" withExtension:@"wav"];
self.loopPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL1 error:nil];
NSURL* audioFileURL2 = [[NSBundle mainBundle] URLForResource:@"other_audio_file" withExtension:@"wav"];
self.loopPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL2 error:nil];
}另外,stopAudio:方法应该是
-(IBAction)stopAudio:(id)sender {
if (self.loopPlayer1.isPlaying && (sender.tag == 1)) {
[self.loopPlayer1 stop];
self.loopPlayer1.currentTime = 0;
self.loopPlayer1.numberOfLoops = -1;
[self.loopPlayer1 prepareToPlay];
}
if (self.loopPlayer2.isPlaying && (sender.tag == 2)) {
[self.loopPlayer2 stop];
self.loopPlayer2.currentTime = 0;
self.loopPlayer2.numberOfLoops = -1;
[self.loopPlayer2 prepareToPlay];
}
}最后,对于playAudio:
-(IBAction)playAudio:(id)sender {
if([sender tag] == 1){
[self.loopPlayer1 play];
}
if([sender tag] == 2){
[self.loopPlayer2 play];
}
}发布于 2014-02-09 04:06:56
如果您想同时播放不同的声音,您应该考虑创建单独的AVAudioPlayers --如果您为每个声音创建了一个不同的声音,那么您可以通过一个特定的按钮轻松地分别控制(播放/停止)每个声音。
发布于 2014-02-09 04:14:58
在最简单的层面上,您可以这样做,这允许您对所有音频使用相同的按钮处理程序。playAudio检查按下的Play按钮的标记(确保将IB中的标记值设置为1,2等)。真的只需要一个停止按钮。
您可以通过多种方式增强这一点,比如尝试以某种方式重用AVAudioPlayer,并在开始时加载音频而不是所有音频。或者将您的音频文件信息存储在一个数组中,创建一个用于管理的AVAudioPlayers数组,等等。但这是一个开始。
-(IBAction)playAudio:(id)sender {
// first, stop any already playing audio
[self stopAudio:sender];
if([sender tag] == 1){
[self.loopPlayer1 play];
} else if([sender tag] == 2){
[self.loopPlayer2 play];
}
}
-(IBAction)stopAudio:(id)sender {
if (self.loopPlayer1.isPlaying) {
[self.loopPlayer1 stop];
self.loopPlayer1.currentTime = 0;
self.loopPlayer1.numberOfLoops = -1;
[self.loopPlayer1 prepareToPlay];
} else if (self.loopPlayer2.isPlaying) {
[self.loopPlayer2 stop];
self.loopPlayer2.currentTime = 0;
self.loopPlayer2.numberOfLoops = -1;
[self.loopPlayer2 prepareToPlay];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
NSURL* audioFileURL1 = [[NSBundle mainBundle] URLForResource:@"trapsynth" withExtension:@"wav"];
self.loopPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];
NSURL* audioFileURL2 = [[NSBundle mainBundle] URLForResource:@"trapsynth" withExtension:@"wav"];
self.loopPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];
}在.h文件中;
property (nonatomic, strong) AVAudioPlayer *loopPlayer1;
property (nonatomic, strong) AVAudioPlayer *loopPlayer2;https://stackoverflow.com/questions/21655095
复制相似问题