Ios 播放时多个音频文件重叠
我在播放声音文件时遇到问题:我有多个按钮,每个按钮都与一个声音文件关联。例如,当声音n.1播放时,我按下按钮启动声音n.2,两个声音重叠。我希望每个按钮在按下时停止另一个按钮播放的音频。这是我的.h文件和.m文件的一部分。我尝试使用“if”,但收到“使用未声明的标识符”错误。请记住,我是一个绝对的初学者,提前谢谢你Ios 播放时多个音频文件重叠,ios,avaudioplayer,Ios,Avaudioplayer,我在播放声音文件时遇到问题:我有多个按钮,每个按钮都与一个声音文件关联。例如,当声音n.1播放时,我按下按钮启动声音n.2,两个声音重叠。我希望每个按钮在按下时停止另一个按钮播放的音频。这是我的.h文件和.m文件的一部分。我尝试使用“if”,但收到“使用未声明的标识符”错误。请记住,我是一个绝对的初学者,提前谢谢你 #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface ViewC
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <AVAudioPlayerDelegate> {}
-(IBAction)playSound1;
-(IBAction)playSound2;
@end
@implementation ViewController
-(IBAction)playSound1{
NSString *path=[[NSBundle mainBundle] pathForResource:@"12-Toxicity" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];
theAudio.delegate=self;
[theAudio play];
}
@end
#导入
#进口
@接口ViewController:UIViewController{}
-(i)播放声音1;
-(i)播放声音2;
@结束
@实现视图控制器
-(iAction)播放声音1{
NSString*path=[[NSBundle mainBundle]pathForResource:@“mp3”类型的@“12毒性”;
AVAudioPlayer*theAudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path]错误:NULL];
音频代理=自身;
[音频播放];
}
@结束
此代码完成任务。。。而且,作为奖励,你的应用程序只需加载一次音乐文件
// ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <AVAudioPlayerDelegate>
@property (strong) AVAudioPlayer* sound1Player;
@property (strong) AVAudioPlayer* sound2Player;
- (IBAction)playSound1;
- (IBAction)playSound2;
@end
// ViewController.m
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
NSString *pathOne = [[NSBundle mainBundle] pathForResource:@"12-Toxicity" ofType:@"mp3"];
if (pathOne) {
self.sound1Player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathOne] error:NULL];
self.sound1Player.delegate = self;
}
NSString *pathTwo = [[NSBundle mainBundle] pathForResource:@"13-Psycho" ofType:@"mp3"];
if (pathOne) {
self.sound2Player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathTwo] error:NULL];
self.sound2Player.delegate = self;
}
}
- (IBAction)playSound1 {
if (self.sound2Player.playing)
[self.sound2Player stop];
[self.sound1Player play];
}
- (IBAction)playSound2 {
if (self.sound1Player.playing)
[self.sound1Player stop];
[self.sound2Player play];
}
@end
//ViewController.h
#进口
#进口
@界面ViewController:UIViewController
@属性(强)AVAudioPlayer*sound1Player;
@属性(强)AVAudioPlayer*sound2Player;
-(i)播放声音1;
-(i)播放声音2;
@结束
//ViewController.m
#导入“ViewController.h”
@实现视图控制器
-(无效)viewDidLoad{
NSString*pathOne=[[NSBundle mainBundle]路径资源:@“mp3”类型的“12毒性”;
if(pathOne){
self.sound1Player=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathOne]错误:NULL];
self.sound1Player.delegate=self;
}
NSString*pathTwo=[[NSBundle mainBundle]pathForResource:@“13 Psycho”类型:@“mp3”];
if(pathOne){
self.sound2Player=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathTwo]错误:NULL];
self.sound2Player.delegate=self;
}
}
-(iAction)播放声音1{
if(self.sound2Player.playing)
[self.sound2Player stop];
[self.sound1Player play];
}
-(iAction)播放声音2{
if(self.sound1Player.playing)
[self.sound1Player stop];
[self.sound2Player play];
}
@结束
您需要在属性中存储两个AVAudioPlayer
,并在playSound2
中调用[self.audio1 stop]
,反之亦然……谢谢Simon,我该如何将它们存储在.h文件中呢?我记得我做了一些有效的事情,然后我不小心删除了代码…我说不出话来…谢谢!度过一个美好的夜晚,或者在你所在时区的任何时间;)不用担心@Nicola,祝你的项目顺利