在iPhone中通过触摸动作连续播放音频文件

时间:2011-10-25 12:28:59

标签: iphone touch avaudioplayer fadein fadeout

当触摸在iPhone的屏幕上移动时,我正在尝试播放连续的音频剪辑。根据屏幕区域有特定的音频剪辑。当触摸改变其区域时,先前的音频淡出&新剪辑(新区域)淡入。 在- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event方法中,我正在检测区域变化&调用[self playSoundForRect]方法。这是方法:

-(void)playSoundWithPath
{
    [self fadeVolume];
    NSString *audioPath;
    switch (curRect) // curRect is the Currently entered region
    {
        case 1:
            audioPath = [[NSBundle mainBundle] pathForResource:@"clip1" ofType:@"mp3"];
            break;
        case 2:
            audioPath = [[NSBundle mainBundle] pathForResource:@"clip2" ofType:@"mp3"];
            break;
        case 3:
            audioPath = [[NSBundle mainBundle] pathForResource:@"clip3" ofType:@"mp3"];
            break;
        case 4:
            audioPath = [[NSBundle mainBundle] pathForResource:@"clip4" ofType:@"mp3"];
            break;
        default:
            break;
    }
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioPath] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
}

-(void)fadeVolume
{  
    if (theAudio.volume > 0.1)
    {
        theAudio.volume -= 0.1;
        [self performSelector:@selector(fadeVolume) withObject:nil afterDelay:0.1];           
    }
    else
    {
        [theAudio stop];
    }
}

但代码运行不正常。如何正确混合in&剪辑出来,让他们感觉顺畅连续?欢迎提出任何建议。

1 个答案:

答案 0 :(得分:0)

您可能想尝试为原始音频构建混音器,并使用RemoteIO而不是AVPlayer API来传输混音。