我正试图让我的Parrot AR Drone 2.0在Windows机器上运行。
我有一个简单的C#应用程序来控制它,但现在我希望视频流在我的应用程序中。
如果我执行ffplay tcp://192.168.1.1:5555
,它会连接到视频流并显示一个包含视频的窗口。
如何在我的应用程序中获取此视频?比如,一个简单的"框架"或"图像"充满了这些内容?
我从来没有用C#做过那么多的工作,所以任何帮助都会很棒。
您可以启动ffplay
进程,然后PInvoke SetParent
将播放器窗口放置在表单中,MoveWindow
将其定位。
为此,您需要定义以下内容。
[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
然后你可以像这样使用两种原生方法。
// start ffplay
var ffplay = new Process
{
StartInfo =
{
FileName = "ffplay",
Arguments = "tcp://192.168.1.1:5555",
// hides the command window
CreateNoWindow = true,
// redirect input, output, and error streams..
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false
}
};
ffplay.EnableRaisingEvents = true;
ffplay.OutputDataReceived += (o, e) => Debug.WriteLine(e.Data ?? "NULL", "ffplay");
ffplay.ErrorDataReceived += (o, e) => Debug.WriteLine(e.Data ?? "NULL", "ffplay");
ffplay.Exited += (o, e) => Debug.WriteLine("Exited", "ffplay");
ffplay.Start();
Thread.Sleep(200); // you need to wait/check the process started, then...
// child, new parent
// make 'this' the parent of ffmpeg (presuming you are in scope of a Form or Control)
SetParent(ffplay.MainWindowHandle, this.Handle);
// window, x, y, width, height, repaint
// move the ffplayer window to the top-left corner and set the size to 320x280
MoveWindow(ffplay.MainWindowHandle, 0, 0, 320, 280, true);
ffplay
进程的标准输出,即您通常在命令窗口中看到的文本,是通过ErrorDataReceived
处理的。在传递给ffplay的参数中,将-loglevel
设置为类似fatal
的值,可以减少引发的事件数量,并且只允许处理实际故障。
您尝试过使用媒体播放器进行流媒体播放吗?只需在表单上添加工具箱中的控件,然后将以下代码添加到表单中即可。cs
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = "your URL";
}
}
以下链接中的详细信息
http://msdn.microsoft.com/en-us/library/bb383953%28v=vs.90%29.aspx
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Drawing.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using Microsoft.Win32;
using System.Windows.Forms.VisualStyles;
namespace FfplayTest
{
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
public Form1()
{
InitializeComponent();
Application.EnableVisualStyles();
this.DoubleBuffered = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public Process ffplay = new Process();
private void xxxFFplay()
{
ffplay.StartInfo.FileName = "ffplay.exe";
string _argString = "-fflags nobuffer "rtsp://admin:admin@192.168.0.163/live0.264" -x 640 -y 480";
string _newArgString = _argString.Replace("","", ";");
ffplay.StartInfo.Arguments = _newArgString;
ffplay.StartInfo.CreateNoWindow = true;
ffplay.StartInfo.RedirectStandardOutput = true;
ffplay.StartInfo.UseShellExecute = false;
ffplay.EnableRaisingEvents = true;
ffplay.OutputDataReceived += (o, e) => Debug.WriteLine(e.Data ?? "NULL", "ffplay");
ffplay.ErrorDataReceived += (o, e) => Debug.WriteLine(e.Data ?? "NULL", "ffplay");
ffplay.Exited += (o, e) => Debug.WriteLine("Exited", "ffplay");
ffplay.Start();
IntPtr intPtr = ffplay.MainWindowHandle;
Thread.Sleep(200); // you need to wait/check the process started, then...
// child, new parent
// make 'this' the parent of ffmpeg (presuming you are in scope of a Form or Control)
SetParent(ffplay.MainWindowHandle, this.Handle);
// window, x, y, width, height, repaint
// move the ffplayer window to the top-left corner and set the size to 320x280
MoveWindow(ffplay.MainWindowHandle, 0, 0, 320, 280, true);
}
private void button1_Click(object sender, EventArgs e)
{
xxxFFplay();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
try { ffplay.Kill(); }
catch { }
}
}
}
我的代码是这样的,它如何加载ffplay,但不移动到面板或不将其定位到给定的