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,&outputNode) // Graph must be opened before we can get node info! AUGraphOpen(graph) AUGraphNodeInfo(graph,filePlayerNode,nil,&filePlayerAU) AUGraphNodeInfo(graph,outputNode,&outputAU) AUGraphConnectNodeinput(graph,0) AUGraphInitialize(graph) registerCallbackForAU(filePlayerAU,nil) } func schedulefile(url: NSURL) { AudiofileOpenURL(url,1,&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),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),®ion,UInt32(sizeof(ScheduledAudiofileRegion))) // Step 3: Prime the file player var primeFrames: UInt32 = 0 AudioUnitSetProperty(filePlayerAU,AudioUnitPropertyID(kAudioUnitProperty_ScheduledfilePrime),&primeFrames,UInt32(sizeof(UInt32))) // Step 4: Schedule the start time (-1 = Now) timeStamp.mSampleTime = -1 AudioUnitSetProperty(filePlayerAU,AudioUnitPropertyID(kAudioUnitProperty_ScheduleStartTimeStamp),&timeStamp,UInt32(sizeof(AudioTimeStamp))) }}解决方法 如果您要从服务器播放远程文件(mp3).我会研究使用AVPlayer().
https://developer.apple.com/documentation/avfoundation/avplayer
这是一个片段,可能会推动你朝着正确的方向前进.当然,这只是一个非常基本的例子.
func playRemotefile() { let fileUrl = "http://www.foo.com/bar.mp3" let url = NSURL(string: fileUrl) var myPlayer = AVPlayer(URL: url) myPlayer.play()}总结
以上是内存溢出为你收集整理的如何在Swift中流式传输MP3网址(iOS)?全部内容,希望文章能够帮你解决如何在Swift中流式传输MP3网址(iOS)?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)