无法在xna上播放视频

时间:2012-11-26 09:09:22

标签: c#-4.0 xna-4.0

我在使用xna播放视频时出现问题,我无法使用xna播放视频。它抛出了执行。 例外情况是:System.InvalidOperationException“发生意外错误”。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace VideoGameTest1
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D videotexture;
        Video video;
        VideoPlayer videoplayer;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }


        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

           // IsMouseVisible = true;
            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);

            video = Content.Load<Video>("Wildlife");
            videoplayer = new VideoPlayer();

            // TODO: use this.Content to load your game content here
        }

        /// <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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)  
                this.Exit();

            if (videoplayer.State == MediaState.Stopped) 
            {
                videoplayer.IsLooped = true;
                videoplayer.Play(video);
            }
            // TODO: Add your update logic here

            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            if (videoplayer.State != MediaState.Stopped) 
            {
                videotexture = videoplayer.GetTexture();
            }

            Rectangle screen = new Rectangle(GraphicsDevice.Viewport.X, GraphicsDevice.Viewport.Y, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);

            spriteBatch.Begin();
            if (videotexture != null) 
            {
                spriteBatch.Draw(videotexture,screen, Color.White);
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

抛出异常:videoplayer.Play(video);

3 个答案:

答案 0 :(得分:0)

可能缺乏对编解码器的支持。 This site有一个受支持的编解码器列表,哪些适用于XNA框架。例如,如果您的视频目前在DIVX中编码,您的PC将播放它,但WP7不支持它。希望有所帮助。

答案 1 :(得分:0)

问题是,您尝试在更新循环中播放视频,并且每次(每秒30次)尝试访问从开始播放视频的更新。尝试将视频播放代码放在初始化部分。如果工作正常,请回复评论。 如果没有,那么也在评论中发表。我将进一步研究。

答案 2 :(得分:0)

谢谢大家!

我用安装新的编解码器“K-lite codecs”修复它

现在它运行了。