我正在尝试同时播放2种不同的声音,这在将所有代码放在2个不同的方法中时效果很好。相反,我想创建一个包含声音播放器代码的方法,并以歌曲名称作为参数从另一个方法调用该方法。这个......几乎可以。
按下按钮1 - >声音播放。好!!! 按下按钮2 - > sound1停止并且sound2开始。不好!!!
如何发出声音,让他/她的手指远离声音。
这是代码。这可能很简单,但我现在已经停留了一段时间。
.
.
.
-(IBAction) playSound1
{
NSString *nameOfSong = @"song1";
[self nowPlay:nameOfSong];
}
-(IBAction) playSound2
{
NSString *nameOfSong = @"song2";
[self nowPlay:nameOfSong];
}
-(void) nowReallyPlay:(NSString*) whatsTheName
{
NSURL *playSong = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:whatsTheName ofType:@"mp3"]];
self.soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:playSong error:nil];
soundPlayer.delegate = self;
soundPlayer.volume= 0.5;
soundPlayer.numberOfLoops = 0;
[soundPlayer play];
}
.
.
.
答案 0 :(得分:0)
我通过定义2个不同的AudioPlayers并使用if语句选择要播放哪个来解决它。
它有效,但在我的诚实意见中仍然不是很性感。对于我的程序(适用于我的小女孩的应用程序),它会做得很好,但如果有人想要25个声音播放怎么办?你需要创建25个AVPlayers吗?
还有另一种(更性感)的方式来克服这个障碍吗?
.
.
-(IBAction) playSound1
{
NSString *nameOfSong = @"song1";
NSString *playThis = @"first_song";
[self nowPlay:nameOfSong:playThis];
}
-(IBAction) playSound2
{
NSString *nameOfSong = @"song2";
NSString *playThis = @"second_song";
[self nowPlay:nameOfSong:playThis];
}
-(void) nowReallyPlay:(NSString*) whatsTheName : (NSString*) playWhat
{
if (playWhat == @"first_song")
{
NSURL *playSong = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:whatsTheName ofType:@"mp3"]];
self.soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:playSong error:nil];
sound1Player.delegate = self;
sound1Player.volume= 0.5;
sound1Player.numberOfLoops = 0;
[sound1Player play];
} else if (playWhat == @"second_song"") {
NSURL *playSong = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:whatsTheName ofType:@"mp3"]];
self.sound2Player = [[AVAudioPlayer alloc] initWithContentsOfURL:playSong error:nil];
sound2Player.delegate = self;
sound2Player.volume= 0.5;
sound2Player.numberOfLoops = 0;
[sound2Player play];
}
}
.
.