我想将iOS设备用作源客户端,以将音频发送到icecast服务器。我已经在icecast上成功设置了localhost服务器。现在,当我向服务器发送以下请求时,它会创建mountpoint,但仅针对8 sec的第一个audioaudio的总长度为25 sec。我知道这是因为我没有分块发送数据。以下是我的要求:

let requestString = "http://localhost:8000/"
let url = URL(string: requestString)
let mountPointName = "myMountPoint"

var request = URLRequest(url: url!)
request.httpMethod = "SOURCE /\(mountPointName) ICE/1.0"
request.addValue("SOURCE /mp3test ICE/1.0", forHTTPHeaderField: "SOURCE")
request.addValue("audio/ogg", forHTTPHeaderField: "Content-Type")
request.setValue("Basic c291cmNlOmhhY2ttZQ==", forHTTPHeaderField: "Authorization")
request.setValue("Server name", forHTTPHeaderField: "ice-name")
request.setValue("https://www.google.com", forHTTPHeaderField: "ice-url")
request.setValue("RockTest", forHTTPHeaderField: "ice-genre")
request.setValue("128", forHTTPHeaderField: "ice-bitrate")
request.setValue("0", forHTTPHeaderField: "ice-private")
request.setValue("1", forHTTPHeaderField: "ice-public")
request.setValue("Server description", forHTTPHeaderField: "ice-description")
request.setValue("samplerate=44100;bitrate=128;channels=2", forHTTPHeaderField: "ice-audio-info")
self.recurseivelySendRequest(request)


我的递归函数:

func recurseivelySendRequest(_ _request: URLRequest) {
    var request = _request

    do {
        let fileURL = Bundle.main.url(forResource: "mount", withExtension: "ogg")  let data = try Data(contentsOf: fileURL!)
        let inputStream = InputStream(data: data)
        request.httpBodyStream = inputStream
    } catch let error {
        print(error)
    }

   // let dataTask = URLSession.shared.uploadTask(withStreamedRequest: request)
   // dataTask.resume()

   let dataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in

   if (error != nil) {
       print(error ?? "error = unknown")
       return
   }

   print(response ?? "response = unknown")

   do {
      let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
      print(json)
   } catch let error {
      print(error)
      let str = String.init(data: data!, encoding: String.Encoding.utf8)
      print(str ?? "str = nil")
   }

   self.recurseivelySendRequest(request)
}
        dataTask.resume()
}


上面代码的问题是它也只发送audio的一部分,如果我不递归发送请求,则mountpoint不再存在,似乎正在替换未添加的数据。所以我要创建一次mountpoint,然后将音频数据分块写入。

我也关注Icecast 2: protocol description, streaming to it using

最佳答案

我走错了路,终于在github上找到了iOS-Icecast-Client。它可以帮助我与icecast-server建立连接,并将录音机音频分块发送到服务器。正如我已经提到的,我能够在icecast-server上设置localhost

AudioProcessor.m文件中下载上述库后,有一行

ShoutOutputStream_Init("192.x.x.x", 8000, "/stream", "source", "hackme", 0);


其中192.x.x.x是您的wifi ip,因为您无法在设备中使用localhost进行连接。另外,请确保您的系统和设备应该在同一wifi连接上才能访问网络。

8000是您在icecast.xml中定义的端口。

/stream是在icecast.xml中定义的安装点名称。

在这里,我花了几个小时来获取usernamepassword,它们也在authenticationicecast.xml标记中定义。我使用默认的sourcehackme

0vorbis(0)还是.mp3(1)文件。

终于,我开始工作了。非常感谢gstream79为我们提供了一个如此出色的库。

关于ios - 使用Icecast将iOS设备设置为源客户端,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50056884/

10-09 01:26