我想在第一包数据到来时启动我的 mediaElement(MediaElement 类的实例)。所以我就这样写了。
private void SocketOnMessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
if (isPaused) return;
if (!isStarted)
{
mediaElement.Play();
isStarted = true;
}
var r = args.GetDataReader();
var l = r.UnconsumedBufferLength;
var buff = new byte[l];
r.ReadBytes(buff);
if (System.Text.Encoding.UTF8.GetString(buff, 0, buff.Length) != "stop")
{
AudioSteam.AddBytes(buff);
}
else
{
mediaElement.Pause();
isStarted = false;
Debug.WriteLine("stop");
}
buff = null;
}
当然,我为 mediaElement (MediaStreamSource) 设置了源。
var socket = new DatagramSocket();
socket.MessageReceived += SocketOnMessageReceived;
await socket.BindServiceNameAsync("4444");
HostName multicast = new HostName("230.0.0.1");
socket.JoinMulticastGroup(multicast);
isPaused = true;
isStarted = false;
AudioSteam = new Sine440AudioStreamSource(44100);
mdiaElement.SetSource(AudioSteam);
起初我System.IO.FileNotFoundException
在System.UnauthorizedAccessException
mediaElement.Start(); 我不知道为什么。
任何建议将不胜感激。