我正在使用Vlc.DotNet.Core进行多播视频流传输。另外,我想拍摄快照。为了进行测试,我正在开发一个简单的程序,该程序每2秒调用一次快照功能。但是快照功能不起作用,调用TakeSnapshot
函数时,它没有调用OnMediaPlayerSnapshotTaken
事件。
除此之外,我对Vlc.DotNet.Forms
库进行了快照测试,并且该库运行良好。
我正在处理的简单代码如下:
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Vlc.DotNet.Core;
namespace SnapshotTest
{
class Program
{
static void Main(string[] args)
{
VlcStreamerSnapshotText myObject = new VlcStreamerSnapshotText();
myObject.streamFile();
}
}
class VlcStreamerSnapshotText
{
System.Timers.Timer aTimer = new System.Timers.Timer();
static Vlc.DotNet.Core.VlcMediaPlayer mediaPlayer;
public bool streamFile(string source = "")
{
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 2000;
aTimer.Enabled = true;
var currentDirectory = Directory.GetCurrentDirectory();
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "x86" : "x64"));
var options = new string[]
{
"--no-snapshot-preview",
"--no-audio"
//"--no-video"
// VLC options can be given here. Please refer to the VLC command line documentation.
};
mediaPlayer = new Vlc.DotNet.Core.VlcMediaPlayer(new DirectoryInfo(libDirectory.FullName), options);
string[] mediaOptions = new string[]
{
":sout=#duplicate{dst=rtp{sdp=rtsp://127.0.0.1:1000/},dst=display}",
":sout-all",
":sout-keep"
};
//string[] mediaOptions = new string[] { string.Concat("--sout=#duplicate{dst=rtp{sdp=rtsp://", IpAddress, ":", "1000", "/}} --sout-all --sout-keep") };
mediaPlayer.SetMedia(new Uri("http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_h264.mov"), mediaOptions);
mediaPlayer.SnapshotTaken += OnMediaPlayerSnapshotTaken;
bool playFinished = false;
mediaPlayer.PositionChanged += (sender, e) =>
{
Console.Write("\r" + Math.Floor(e.NewPosition * 100) + "%");
};
mediaPlayer.EncounteredError += (sender, e) =>
{
Console.Error.Write("An error occurred");
playFinished = true;
};
mediaPlayer.EndReached += (sender, e) =>
{
playFinished = true;
};
Task.Factory.StartNew(() => mediaPlayer.Play());
//mediaPlayer.Play();
// Ugly, sorry
while (!playFinished)
{
Thread.Sleep(TimeSpan.FromMilliseconds(500));
}
return true;
}
ManualResetEvent m_mreSnapshot = new ManualResetEvent(false);
private readonly object m_snapLock = new object();
private bool m_SnapshotIsTaken = false;
public bool GetSnapshot()
{
m_SnapshotIsTaken = false;
if (mediaPlayer == null)
{
return false;
}
string currentTime = DateTime.Now.ToString("yyyyMMdd_HHmmss");
string uniqueId = generateID(string.Empty);
string snapName = string.Format("{0}_{1}.png", currentTime, uniqueId);
string tempFilePathCand = Path.GetTempPath();
if (!tempFilePathCand.EndsWith("\\"))
tempFilePathCand = tempFilePathCand + "\\" + snapName;
else
tempFilePathCand = tempFilePathCand + snapName;
FileInfo snapPathInfo = new FileInfo(tempFilePathCand);
//mediaPlayer.Manager.TakeSnapshot();
mediaPlayer.TakeSnapshot(snapPathInfo);
m_mreSnapshot.WaitOne(TimeSpan.FromSeconds(2));
if (m_SnapshotIsTaken)
return true;
return false;
}
private string generateID(string tip)
{
return string.Format(@"{0}_{1}", tip, Guid.NewGuid().ToString("N"));
}
void OnMediaPlayerSnapshotTaken(object sender, VlcMediaPlayerSnapshotTakenEventArgs e)
{
mediaPlayer.SnapshotTaken -= OnMediaPlayerSnapshotTaken;
m_SnapshotIsTaken = true;
mediaPlayer.SnapshotTaken += OnMediaPlayerSnapshotTaken;
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
GetSnapshot();
}
}
}
那么,我想念什么?
答案 0 :(得分:0)
可能是Vlc.DotNet中的错误:https://github.com/ZeBobo5/Vlc.DotNet/issues/447 您正在使用Vlc.DotNet v3吗?