【问题标题】:Use other method to play sound simultaneously使用其他方法同时播放声音
【发布时间】:2012-09-23 21:06:27
【问题描述】:

我正在尝试同时播放 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];
}
.
.
.

【问题讨论】:

    标签: objective-c methods avaudioplayer argument-passing


    【解决方案1】:

    我通过定义 2 个不同的 AudioPlayers 并使用 if 语句选择播放哪一个来解决这个问题。

    它有效,但在我看来仍然不是很性感。对于我的程序(我的小女孩的应用程序)它会做得很好,但是如果一个人想要 25 个声音播放呢?您是否必须创建 25 个 AVPlayer?

    还有其他(更性感的)方法可以克服这个障碍吗?

    .
    .
    -(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];
        }
    }
    .
    .
    

    【讨论】:

    • 不是磨练中的响应频率。必须触发外面的某个人来回复某事……或者至少叫我驴。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多