اليكم طريقة أضافة الفيديو الى XNA وهي الميزة الجديدة في XNA 3.1
وبأمكانك ان تشغل وتسجل لعبتك المفضلة عن طريق هذه الخاصية الرائعة وأمكانك أن تشاهد الفيديو HD بدقة 1280*720
تم تحديث الكود
واليك الطريقة البرمجية لذلك :
وبأمكانك ان تشغل وتسجل لعبتك المفضلة عن طريق هذه الخاصية الرائعة وأمكانك أن تشاهد الفيديو HD بدقة 1280*720
تم تحديث الكود
واليك الطريقة البرمجية لذلك :
namespace VideoPlayerAction
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// Create the variables needed to hold and play back my video.
// انشاء متغيرات الفيديو هنا
Video PlayvideoFile;
VideoPlayer videoPlayer;
private SpriteFont font;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
1
//اضافة تحميل الفيديو لتجهيزه في XNA
videoPlayer = new VideoPlayer();
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
1
// كتابة تعريف الفيديو في الملف المرفق مع اللعبة يكون من نوعية WMV & AVI &WMA
PlayvideoFile = Content.Load<Video >(@"open2");
font = Content.Load<SpriteFont>("Fonts/font");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// الخروج من اللعبة
if (newState.IsKeyDown(Keys.Q ))
this.Exit();
//كتابة متحكمات اللعبة في تشغيل الفيديو او تحريك المجسمات مع تسجيل اللعبة في مراحلها
KeyboardState newState = Keyboard.GetState();
//للضغط على بي لتشغيل الفيديو
if (newState.IsKeyDown(Keys.P ))
{
videoPlayer.Play(myVideoFile);
}
//للضغط على حرف ل لعمل اعادة تشغيل دوماً
if (newState.IsKeyDown(Keys.L ))
{
player.IsLooped = true;
player.Play(video);
}
// للضغط على حرف الأم للأسكات الصوت او عدمه
if (newState.IsKeyDown(Keys.M ))
{
videoPlayer.IsMuted();
}
// للضغط على حرف في لزيادة الصوت او نقصانه
if (newState.IsKeyDown(Keys.V ))
{
videoPlayer.Volume += 10;
}
// للضغط على حرف الأس لأيقاف الفيديو
if (newState.IsKeyDown(Keys.S ))
{
player.Pause();
}
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// هنا يتم عمل رسم او أظهار الفيديو في تكبيره على حجم الشاشة
// في تشغيل واغلاق الفيديو GetTexture يتم اتصال الامر
if (player.State != MediaState.Stopped)
videoTexture = player.GetTexture();
// يتم هنا رسم المتغير على طول وعرض الشاشة
Rectangle screen = new Rectangle(GraphicsDevice.Viewport.X,
GraphicsDevice.Viewport.Y,
GraphicsDevice.Viewport.Width,
GraphicsDevice.Viewport.Height);
// يتم هنا برمجة اصدار الفيديو في الصورة على الشاشة بلون ابيض اذا كنت تريد اللون المناسب عند البداية التشغيل
if (videoTexture != null)
{
spriteBatch.Begin();
spriteBatch.Draw(videoTexture, screen, Color.White);
spriteBatch.End();
}
base.Draw(gameTime);
}
}
}
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// Create the variables needed to hold and play back my video.
// انشاء متغيرات الفيديو هنا
Video PlayvideoFile;
VideoPlayer videoPlayer;
private SpriteFont font;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
1
//اضافة تحميل الفيديو لتجهيزه في XNA
videoPlayer = new VideoPlayer();
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
1
// كتابة تعريف الفيديو في الملف المرفق مع اللعبة يكون من نوعية WMV & AVI &WMA
PlayvideoFile = Content.Load<Video >(@"open2");
font = Content.Load<SpriteFont>("Fonts/font");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// الخروج من اللعبة
if (newState.IsKeyDown(Keys.Q ))
this.Exit();
//كتابة متحكمات اللعبة في تشغيل الفيديو او تحريك المجسمات مع تسجيل اللعبة في مراحلها
KeyboardState newState = Keyboard.GetState();
//للضغط على بي لتشغيل الفيديو
if (newState.IsKeyDown(Keys.P ))
{
videoPlayer.Play(myVideoFile);
}
//للضغط على حرف ل لعمل اعادة تشغيل دوماً
if (newState.IsKeyDown(Keys.L ))
{
player.IsLooped = true;
player.Play(video);
}
// للضغط على حرف الأم للأسكات الصوت او عدمه
if (newState.IsKeyDown(Keys.M ))
{
videoPlayer.IsMuted();
}
// للضغط على حرف في لزيادة الصوت او نقصانه
if (newState.IsKeyDown(Keys.V ))
{
videoPlayer.Volume += 10;
}
// للضغط على حرف الأس لأيقاف الفيديو
if (newState.IsKeyDown(Keys.S ))
{
player.Pause();
}
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// هنا يتم عمل رسم او أظهار الفيديو في تكبيره على حجم الشاشة
// في تشغيل واغلاق الفيديو GetTexture يتم اتصال الامر
if (player.State != MediaState.Stopped)
videoTexture = player.GetTexture();
// يتم هنا رسم المتغير على طول وعرض الشاشة
Rectangle screen = new Rectangle(GraphicsDevice.Viewport.X,
GraphicsDevice.Viewport.Y,
GraphicsDevice.Viewport.Width,
GraphicsDevice.Viewport.Height);
// يتم هنا برمجة اصدار الفيديو في الصورة على الشاشة بلون ابيض اذا كنت تريد اللون المناسب عند البداية التشغيل
if (videoTexture != null)
{
spriteBatch.Begin();
spriteBatch.Draw(videoTexture, screen, Color.White);
spriteBatch.End();
}
base.Draw(gameTime);
}
}
}
ارجو ان يكون الدرس شرحه سهل , اذا تريد ان تجربه فأفعل ذلك الآن
تعليق