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);

4

3 回答 3

0

谢谢你们!

我通过安装新的编解码器“K-lite 编解码器”来修复它

现在它运行了。

于 2012-11-29T08:14:52.717 回答
0

可能是缺乏对编解码器的支持。该站点有一个受支持的编解码器列表以及哪些适用于 XNA 框架。例如,如果您的视频当前以 DIVX 编码,您的 PC 将播放它,但 WP7 不支持它。希望有帮助。

于 2012-11-26T09:32:16.803 回答
0

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

于 2012-11-27T16:37:17.927 回答