如何在Swift中流式传输MP3网址(iOS)?

时间:2015-06-24 22:52:58

标签: swift audiounit

目前下面的代码可以传输本地mp3文件,所以如果我打电话

audio.scheduleFile(NSBundle.mainBundle().URLForResource("Moon River", withExtension: "mp3")!) 它将正确播放本地文件。现在我希望能够流式传输非本地网址。

为了让我能够流式传输MP3网址,我需要做些什么?

class Audio: NSObject {
    var graph: AUGraph
    var filePlayerAU: AudioUnit
    var filePlayerNode: AUNode
    var outputAU: AudioUnit
    var fileID: AudioFileID
    var currentFrame: Int64

    override init () {
        graph = AUGraph()
        filePlayerAU = AudioUnit()
        filePlayerNode = AUNode()
        outputAU = AudioUnit()
        fileID = AudioFileID()
        currentFrame = 0

        super.init()

        NewAUGraph(&graph)

        // Add file player node
        var cd = AudioComponentDescription(componentType: OSType(kAudioUnitType_Generator),
                                            componentSubType: OSType(kAudioUnitSubType_AudioFilePlayer),
                                            componentManufacturer: OSType(kAudioUnitManufacturer_Apple),
                                            componentFlags: 0, componentFlagsMask: 0)
        AUGraphAddNode(graph, &cd, &filePlayerNode)

        // Add output node
        var outputNode = AUNode()

        cd.componentType = OSType(kAudioUnitType_Output)
        cd.componentSubType = OSType(kAudioUnitSubType_RemoteIO)
        AUGraphAddNode(graph, &cd, &outputNode)

        // Graph must be opened before we can get node info!
        AUGraphOpen(graph)
        AUGraphNodeInfo(graph, filePlayerNode, nil, &filePlayerAU)
        AUGraphNodeInfo(graph, outputNode, nil, &outputAU)


        AUGraphConnectNodeInput(graph, filePlayerNode, 0, outputNode, 0)
        AUGraphInitialize(graph)

        registerCallbackForAU(filePlayerAU, nil)
    }

    func scheduleFile(url: NSURL) {
        AudioFileOpenURL(url, 1, 0, &fileID)

        // Step 1: schedule the file(s)
        // kAudioUnitProperty_ScheduledFileIDs takes an array of AudioFileIDs
        var filesToSchedule = [fileID]
        AudioUnitSetProperty(filePlayerAU,
                                AudioUnitPropertyID(kAudioUnitProperty_ScheduledFileIDs),
                                AudioUnitScope(kAudioUnitScope_Global), 0, filesToSchedule,
                                UInt32(sizeof(AudioFileID)))
    }

    func scheduleRegion() {
        // Step 2: Schedule the regions of the file(s) to play
        // Swift forces us to fill out the structs completely, even if they are not used
        let smpteTime = SMPTETime(mSubframes: 0, mSubframeDivisor: 0,
                                    mCounter: 0, mType: 0, mFlags: 0,
                                    mHours: 0, mMinutes: 0, mSeconds: 0, mFrames: 0)

        var timeStamp = AudioTimeStamp(mSampleTime: 0, mHostTime: 0, mRateScalar: 0,
                                        mWordClockTime: 0, mSMPTETime: smpteTime,
                                        mFlags: UInt32(kAudioTimeStampSampleTimeValid), mReserved: 0)

        var region = ScheduledAudioFileRegion()
        region.mTimeStamp = timeStamp
        region.mCompletionProc = nil
        region.mCompletionProcUserData = nil
        region.mAudioFile = fileID
        region.mLoopCount = 0
        region.mStartFrame = currentFrame
        region.mFramesToPlay = UInt32.max

        AudioUnitSetProperty(filePlayerAU,
                                AudioUnitPropertyID(kAudioUnitProperty_ScheduledFileRegion),
                                AudioUnitScope(kAudioUnitScope_Global), 0, &region,
                                UInt32(sizeof(ScheduledAudioFileRegion)))

        // Step 3: Prime the file player
        var primeFrames: UInt32 = 0
        AudioUnitSetProperty(filePlayerAU,
                                AudioUnitPropertyID(kAudioUnitProperty_ScheduledFilePrime),
                                AudioUnitScope(kAudioUnitScope_Global), 0, &primeFrames,
                                UInt32(sizeof(UInt32)))

        // Step 4: Schedule the start time (-1 = now)
        timeStamp.mSampleTime = -1
        AudioUnitSetProperty(filePlayerAU,
                                AudioUnitPropertyID(kAudioUnitProperty_ScheduleStartTimeStamp),
                                AudioUnitScope(kAudioUnitScope_Global), 0,  &timeStamp,
                                UInt32(sizeof(AudioTimeStamp)))
    }
}

1 个答案:

答案 0 :(得分:19)

如果您要从服务器播放远程文件(mp3)。我会研究使用AVPlayer()。

https://developer.apple.com/documentation/avfoundation/avplayer

这是一个可能会向您推动正确方向的片段。当然这只是一个非常基本的例子。

_handleMakeSearch(text) {
  console.log(text);
}