private void ReceiveMessage(object stream) { latestRequest.done = true; NetworkStream nStream = stream as NetworkStream; Message msg; while (!disposed) { if (!latestRequest.done && (DateTime.Now - latestRequest.requestTime).TotalMilliseconds > 3000) { requestMp3(latestRequest.mp3Name); } msg = mm.ReceiveMessage(); if (msg == null) { if (!tcpClient.Connected) { break; } SendMessage(new Message(MessageCode.test)); continue; } ReceivedEventArgs rea = new ReceivedEventArgs(msg); OnReceivedMessage?.Invoke(this, rea); if (rea.done) { continue; } switch (msg.msgCode) { case MessageCode.mp3Frame: int index = (int)msg.data[0]; byte[] frame = msg.data[1] as byte[]; putMp3Frame(index, frame); break; case MessageCode.mp3Info: Mp3Info mp3Info = msg.data[0] as Mp3Info; if (!latestRequest.done && mp3Info.name == latestRequest.mp3Name) { frameList = new byte[mp3Info.totalFrames][]; totalMilliseconds = mp3Info.totalMilliseconds; sampleRate = mp3Info.sampleRate; setPosition(0); latestRequest.done = true; if (playAfterGetInfo) { play(); } } break; } } Dispose(); Console.WriteLine("done"); }
private void responseMp3(string fileName) { stopStream = true; lock (streamingLock) { long len = setMp3File(fileName); var mp3Info = new Mp3Info(); mp3Info.name = fileName; mp3Info.totalFrames = mp3FileReader.totalFrames; mp3Info.totalMilliseconds = mp3FileReader.totalSeconds * 1000; mp3Info.sampleRate = mp3FileReader.getSampleRate(); mm.SendMessage(new Message(MessageCode.mp3Info, mp3Info)); Monitor.PulseAll(streamingLock); } stopStream = false; }