在Windows 10中,shoutcast流工作正常。
我们要做的是不断从网址中获取数据块并获取后台播放器。我们可以玩几秒钟然后玩家停下来。
这是我们尝试过的:
try {
bool initialRq = false;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(RadioUrl));
// Do not buffer the response.
HttpResponseMessage response = await httpClient.SendRequestAsync(request,
HttpCompletionOption.ResponseHeadersRead).AsTask(cts.Token);
//string text = Helpers.SerializeHeaders(response);
IOutputStream output = bufferStream.GetOutputStreamAt(0);
StringBuilder responseBody = new StringBuilder();
using(Stream responseStream = (await response.Content.ReadAsInputStreamAsync()).AsStreamForRead()) {
int read = 0;
byte[] responseBytes = new byte[1024 * 20];
byte[] strippedResponseBytes = new byte[1024 * 20];
bool headerIsDone = false;
do {
StreamReader headerReader = new StreamReader(responseStream);
int icyHeaderLength = 0;
while (!headerIsDone) {
string headerLine = headerReader.ReadLine();
icyHeaderLength += headerLine.Length;
if (headerLine.StartsWith("icy-name:")) {
StationName = headerLine.Substring(9);
icyHeaderLength += headerLine.Length;
} else if (headerLine.StartsWith("icy-genre:")) {
Genre = headerLine.Substring(10);
icyHeaderLength += headerLine.Length;
} else if (headerLine.StartsWith("icy-br:")) {
BitRate1 = short.Parse(headerLine.Substring(7));
icyHeaderLength += headerLine.Length;
} else if (headerLine.StartsWith("icy-metaint:")) {
MetaInt = int.Parse(headerLine.Substring(12));
MetadataAvailable = true;
icyHeaderLength += headerLine.Length;
} else if (headerLine.Equals("")) {
headerIsDone = true;
icyHeaderLength += headerLine.Length;
SamplingRate = samplingRateTable[0, 1];
}
}
read = await responseStream.ReadAsync(responseBytes, icyHeaderLength, responseBytes.Length - icyHeaderLength);
strippedResponseBytes = new byte[responseBytes.Length - icyHeaderLength];
if (icyHeaderLength > 0) {
strippedResponseBytes = responseBytes.ToList().GetRange(icyHeaderLength, responseBytes.Length - icyHeaderLength).ToArray();
} else {
strippedResponseBytes = responseBytes;
}
await CopyChunk(output, strippedResponseBytes); //Method for continuosly filling the bufferStream
if (bufferStream.Size >= 1024 * 20 && !initialRq) {
mediaPlayer.SetStreamSource(bufferStream);
initialRq = true;
}
} while (read != 0);
}
string responseText = responseBody.ToString();
} catch (TaskCanceledException) {
}
这里我们从无线电网址接收数据流并从数据流中剥离ICY标头并将该流提取到后台播放器。 bufferStream用于连续填充数据流。目前我正在设置源,在bufferStream填充到20kb之后。如果我增加这个尺寸,我可以听到更多时间的音乐。但是不能连续听。我做错了什么?
请帮忙。提前谢谢。