ios5 - 如何暂停、停止和重置 AUFilePlayer?

标签 ios5 audio core-audio

我正在使用 AUFilePlayer 处理核心音频,将一些 mp3 加载到混音器单元中,一切都很好,但是我无法暂停音乐或将音乐倒回开始。我尝试启动和停止 AudioGraph,但是一旦停止播放,我就无法让它重新启动。我还尝试使用 AudioUnitSetProperty 将文件播放设置为 0

即沿着这些路线的东西:

        ScheduledAudioFileRegion rgn;
        memset (&rgn.mTimeStamp, 0, sizeof(rgn.mTimeStamp));
        rgn.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
        rgn.mTimeStamp.mSampleTime = 0;
        rgn.mCompletionProc = NULL;
        rgn.mCompletionProcUserData = NULL;
        rgn.mAudioFile = inputFile;
        rgn.mLoopCount = 1;
        rgn.mStartFrame = 0;

        rgn.mFramesToPlay = nPackets * inputFormat.mFramesPerPacket;

        AudioUnitSetProperty(fileUnit, kAudioUnitProperty_ScheduledFileRegion, 
                             kAudioUnitScope_Global, 0,&rgn, sizeof(rgn));

有什么建议?

最佳答案

如果其他人正在处理类似的问题,这让我在谷歌上搜索了几个小时,这是我发现的关于如何专门检索和设置播放头的内容。

从 AUFilePlayer 单元获取播放头:

AudioTimeStamp timestamp;
UInt32 size = sizeof(timestamp);
err = AudioUnitGetProperty(unit, kAudioUnitProperty_CurrentPlayTime, kAudioUnitScope_Global, 0, &timestamp, &size);
timestamp.mSampleTime是该文件的当前播放头。 Actor mSampleTime为浮点数或 double 数,然后除以文件的采样率以转换为秒。

为了重新启动 AUFilePlayer 的播放头,我有一个更复杂的场景,其中多个 AUFilePlayer 通过混音器,可以在不同的时间、多次和不同的循环计数进行调度。这是一个真实的场景,让它们在正确的时间重新启动需要一些代码。

每个 AUFilePlayer 有四个场景及其时间表:
  • 播放头在开头,所以可以正常安排。
  • 播放头已超过项目的持续时间,根本不需要安排。
  • 播放头在项目开始之前,因此可以向上移动开始时间。
  • 播放头在播放项目的中间,因此需要调整文件内播放的区域,并且需要单独安排剩余的循环(因此它们完整播放)。

  • 下面是一些演示这一点的代码(一些外部结构来自我自己的代码而不是 Core Audio,但机制应该很清楚):
    // Add each region
    for(int iItem = 0; iItem < schedule.items.count; iItem++) {
        AEFileScheduleItem *scheduleItem = [schedule.items objectAtIndex:iItem];
    
        // Setup the region
        ScheduledAudioFileRegion region;
        [file setRegion:&region schedule:scheduleItem];
    
        // Compute where we are at in it
        float playheadTime = schedule.playhead / file.sampleRate;
        float endOfItem = scheduleItem.startTime + (file.duration*(1+scheduleItem.loopCount));
    
        // There are four scenarios:
    
        // 1. The playhead is -1
        //    In this case, we're all done
        if(schedule.playhead == -1) {
        }
    
        // 2. The playhead is past the item start time and duration*loopCount
        //    In this case, just ignore it and move on
        else if(playheadTime > endOfItem) {
            continue;
        }
    
        // 3. The playhead is less than or equal to the start time
        //    In this case, simply subtract the playhead from the start time
        else if(playheadTime <= scheduleItem.startTime) {
            region.mTimeStamp.mSampleTime -= schedule.playhead;
        }
    
        // 4. The playhead is in the middle of the file duration*loopCount
        //    In this case, set the start time to zero, adjust the loopCount
        //    startFrame and framesToPlay
        else {
    
            // First remove the start time
            region.mStartFrame = 0;
            double offsetTime = playheadTime - scheduleItem.startTime;
    
            // Next, take out any expired loops
            int loopsExpired = floor(offsetTime/file.duration);
            int fullLoops = region.mLoopCount - loopsExpired;
            region.mLoopCount = 0;
            offsetTime -= (loopsExpired * file.duration);
    
            // Then, adjust this segment of a loop accordingly
            region.mStartFrame = offsetTime * file.sampleRate;
            region.mFramesToPlay = region.mFramesToPlay - region.mStartFrame;
    
            // Finally, schedule any remaining loops separately
            if(fullLoops > 0) {
                ScheduledAudioFileRegion loops;
                [file setRegion:&loops schedule:scheduleItem];
                loops.mStartFrame = region.mFramesToPlay;
                loops.mLoopCount = fullLoops-1;
                if(![super.errors check:AudioUnitSetProperty(unit, kAudioUnitProperty_ScheduledFileRegion, kAudioUnitScope_Global, 0, &region, sizeof(region))
                               location:@"AudioUnitSetProperty(ScheduledFileRegion)"])
                    return false;
            }
        }
    
        // Set the region
        if(![super.errors check:AudioUnitSetProperty(unit, kAudioUnitProperty_ScheduledFileRegion, kAudioUnitScope_Global, 0, &region, sizeof(region))
                       location:@"AudioUnitSetProperty(ScheduledFileRegion)"])
            return false;
    }
    

    关于ios5 - 如何暂停、停止和重置 AUFilePlayer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11533266/

    相关文章:

    ios - 在 iOS 5/5.1 上关闭 Interstitial Flurry 广告时的空白 View Controller

    iphone - 如何创建用于在两部 iPhone 之间共享视频或实时视频 View 的应用程序

    c++ - 在 Windows 中更改原始波形数据的音高

    c# - Monotouch 声音无法正常播放

    ios - Varispeed 音频单元流格式

    iphone - 如何在 JSON 中发布数组值

    iphone - UITapGestureRecognizer 不会在 UIView 子类上启动

    c++ - 检测音频设备的到达/断开

    iphone - 我可以在 iPhone 上使用 Core Audio 效果吗?

    ios - 连接节点错误 kAudioUnitErr_PropertyNotWriteable 时 AudioToolbox AUGraph 失败