0

我正在创建一个设备充当服务器并且其他设备加入的应用程序,我设法创建服务器,但我找不到流式传输音频的方法(设备本地歌曲)。

脚步:

  1. 我用 GCDWebServer 创建服务器

  2. 从 MPMediaItem 获取 NSData

  3. 在服务器默认获取请求中设置 songData

这是我的代码:

class DjSocketServer : NSObject,GCDWebServerDelegate
{
var myLocalServer = GCDWebServer()
var isRunning = false
var serverType = ServerType.isDj


func startServer(with type:ServerType)
{
    serverType = type
    GCDWebServer.setLogLevel(4)
    myLocalServer = GCDWebServer()

    myLocalServer?.delegate = self

    myLocalServer?.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, processBlock: {request in

        //Default Response to GET request
        if let requestUrl = request?.url.absoluteString
        {
            if requestUrl.contains(".mp3")
            {
                return GCDWebServerDataResponse(data: playerInfo.songPlayedData as Data!, contentType: "audio/mp3")
            }
        }

        let para:NSMutableDictionary = NSMutableDictionary()
        para.setValue(DjInformation.party_name, forKey: "djName")
        para.setValue(Common.DJ_Connected_Users.count, forKey: "listener")
        para.setValue(DjInformation.dj_ip, forKey: "djIp")
        para.setValue("http://\(DjInformation.dj_ip):\(AppConfig.Dj_Port)/atrist_poster", forKey: "djImg")
        para.setValue(DjInformation.artist_name, forKey: "artist")

        let jsonData = try! JSONSerialization.data(withJSONObject: para, options: JSONSerialization.WritingOptions.prettyPrinted)
        return GCDWebServerDataResponse(data: jsonData, contentType: "application/json")
    })

    //myLocalServer?.start(withPort: UInt(ServerPort.DjPort.rawValue), bonjourName: UIDevice.current.name)
    do
    {
    try myLocalServer?.start(options: [GCDWebServerOption_AutomaticallySuspendInBackground:false,GCDWebServerOption_Port:UInt(ServerPort.DjPort.rawValue),GCDWebServerOption_BonjourName:UIDevice.current.name])
        print("\n\n\n\n\n\nSuccess Tag:Started ----- \n\n\n\n\n\n")
    }catch
    {
        print("Error Tag : Starting MainDj Server")
    }
}

func webServerDidStart(_ server: GCDWebServer!) {
    print("Visit \(server.serverURL) in your web browser")
    isRunning = true
}

func webServerDidCompleteBonjourRegistration(_ server: GCDWebServer!) {
    print(" CompleteBonjourRegistration ")
}

func webServerDidStop(_ server: GCDWebServer!) {
    print("\n\n\n\n\n\n\nServer Stoped MainDj\n\n\n\n\n\n\n")
    isRunning = false
}
}

我将 MPMediaItem 传递给此函数并转换为 NSData

class func getSongRawData(item: MPMediaItem,success: @escaping (NSData) -> Void,fail: @escaping (NSError) -> Void) -> Void {

    if let exporter = createSongExporter(item: item) {
        exporter.exportAsynchronously(completionHandler: { () -> Void in
            switch exporter.status {
            case .completed:
                let rawData = NSData(contentsOf: exporter.outputURL!)!
                songUrl_selected = (exporter.outputURL?.absoluteString)!
                //print("UURL : ",exporter.outputURL?.absoluteString ?? "None")
                success(rawData)
            default:
                let e = NSError(
                    domain: "jp.co.faithcreates.Meowziq",
                    code: MusicManagerError.RawDataAccessFailed.rawValue,
                    userInfo: [
                        "exporterStatus": exporter.status.rawValue
                    ])
                fail(e)
            }

            self.removeExportedFile(url: exporter.outputURL! as NSURL)
        })
    } else {
        let e = NSError(
            domain: "jp.co.faithcreates.Meowziq",
            code: MusicManagerError.RawDataAccessFailed.rawValue,
            userInfo: nil
        )
        fail(e)
    }
}

private class func createSongExporter(item: MPMediaItem) -> AVAssetExportSession? {
    if let url = item.assetURL {
        let asset = AVURLAsset(url: url, options: nil)
        let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A)

        exporter?.outputFileType = "com.apple.m4a-audio";

        let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let fileName = createExportFileName()
        exporter?.outputURL = NSURL(fileURLWithPath: docDir).appendingPathComponent(fileName)

        return exporter
    } else {
        return nil
    }
}

private class func createExportFileName() -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyyMMddhhmmss"
    let dateString = dateFormatter.string(from: NSDate() as Date)
    return String(format: "%@.m4a", dateString)
    //return String(format: "%@.m4a", "tmp")
}
4

1 回答 1

1

将 NSData 保存为文档目录中的 mp3 文件,然后使用 GCDWebServerFileResponse 如下所示

let fileResponse = GCDWebServerFileResponse(file: song_filePath) 
return fileResponse
于 2017-05-10T10:56:03.287 回答