从PC实时流媒体音乐到Windows Phone
本文关键字:Windows Phone 音乐 流媒体 PC 实时 | 更新日期: 2023-09-27 17:53:17
我正在尝试开发一个Windows应用程序(WPF),它可以通过TCP连接(本地WiFi)在系统上播放任何内容。在接收端,我有Windows Phone 8应用程序,可以实时播放音频。
我试过了:
-
连接成功,数据传输正常。
void sMixListner_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e) { try { if (writer != null) { SocketHandler.SocketManager.sendDataToClients(e.Buffer); } } catch { } } public void sendDataToClients(byte[] music) { foreach (Socket client in clients) { try { client.Send(music); } catch { } } }
以上代码传输从WasapiLoopbackCapture
类接收到的任何数据。
-
在手机上,我能够正确地接收所有字节[]数据,我正在将其写入MemoryStream。MemoryStream stream=new MemoryStream();公共无效ReceiveMessage(){
var responseListener = new SocketAsyncEventArgs(); responseListener.Completed += responseListener_Completed; var responseBuffer = new byte[MAX_BUFFER_SIZE]; responseListener.SetBuffer(responseBuffer, 0, MAX_BUFFER_SIZE); _socket.ReceiveAsync(responseListener); } void responseListener_Completed(object sender, SocketAsyncEventArgs e) { stream.Write(e.Buffer, 0, e.BytesTransferred); if (!stopReceiving) ReceiveMessage(); }
收到几秒钟后,我试图播放它:
public void play()
{
SoundEffect soundE = new SoundEffect(stream.ToArray(), 48000, AudioChannels.Mono);
soundE.CreateInstance();
FrameworkDispatcher.Update();
isPlaying = true;
soundE.Play();
}
我听到的都是噪音。我试着用以下方式写一个WaveHeader到流(来自诺基亚开发人员网站):
public void WriteWavHeader(Stream stream, int sampleRate)
{
const int bitsPerSample = 32;
const int bytesPerSample = bitsPerSample / 8;
var encoding = System.Text.Encoding.UTF8;
// ChunkID Contains the letters "RIFF" in ASCII form (0x52494646 big-endian form).
stream.Write(encoding.GetBytes("RIFF"), 0, 4);
// NOTE this will be filled in later
stream.Write(BitConverter.GetBytes(0), 0, 4);
// Format Contains the letters "WAVE"(0x57415645 big-endian form).
stream.Write(encoding.GetBytes("WAVE"), 0, 4);
// Subchunk1ID Contains the letters "fmt " (0x666d7420 big-endian form).
stream.Write(encoding.GetBytes("fmt "), 0, 4);
// Subchunk1Size 16 for PCM. This is the size of therest of the Subchunk which follows this number.
stream.Write(BitConverter.GetBytes(16), 0, 4);
// AudioFormat PCM = 1 (i.e. Linear quantization) Values other than 1 indicate some form of compression.
stream.Write(BitConverter.GetBytes((short)1), 0, 2);
// NumChannels Mono = 1, Stereo = 2, etc.
stream.Write(BitConverter.GetBytes((short)1), 0, 2);
// SampleRate 8000, 44100, etc.
stream.Write(BitConverter.GetBytes(sampleRate), 0, 4);
// ByteRate = SampleRate * NumChannels * BitsPerSample/8
stream.Write(BitConverter.GetBytes(sampleRate * bytesPerSample), 0, 4);
// BlockAlign NumChannels * BitsPerSample/8 The number of bytes for one sample including all channels.
stream.Write(BitConverter.GetBytes((short)(bytesPerSample)), 0, 2);
// BitsPerSample 8 bits = 8, 16 bits = 16, etc.
stream.Write(BitConverter.GetBytes((short)(bitsPerSample)), 0, 2);
// Subchunk2ID Contains the letters "data" (0x64617461 big-endian form).
stream.Write(encoding.GetBytes("data"), 0, 4);
// NOTE to be filled in later
stream.Write(BitConverter.GetBytes(0), 0, 4);
}
录音部分是正确的,因为如果我用WavFileWriter
写一个。wav文件,声音在播放时很好。
WASAPI捕获是32位IEEE浮点数,而不是32位PCM,因此WAV头中的AudioFormat是错误的。IEEE是浮动的,但在您的情况下,我建议在通过网络发送音频之前转换为16位,以节省一点带宽。