2014-05-16 75 views
1

我在添加多個視頻的過程中遇到了很大的困難。在顯示菜單之前,第一個視頻完全播放(introVid)。一旦我按下開始,而不是顯示第二個視頻(在level0加載之前),我得到一個錯誤,說vidTexture2是NULL。添加多個視頻

有沒有人有任何想法如何添加多個視頻?這裏是視頻管理類的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using TileEngine; 
using Microsoft.Xna.Framework.Media; 
using System.Diagnostics; 

namespace TimerGame.VideoManagers 
{ 
    public class Video1Manager 
    { 

     #region Declarations 


     public Video introVid, cineVid; 
     public VideoPlayer vidPlayer, vidPlayer2; 
     public Texture2D vidTexture, vidTexture2; 
     public Rectangle vidRectangle, vidRectangle2; 


     #endregion 

     #region Initialization 


     public void Initialize(ContentManager Content) 
     { 
      try 
      { 
       vidPlayer = new VideoPlayer(); 
       vidPlayer2 = new VideoPlayer(); 

       introVid = Content.Load<Video>(@"Videos\TimerIntro"); 
       vidRectangle = new Rectangle(-225, 0, 
              1250, 600); 
       cineVid = Content.Load<Video>(@"Videos\Cinematic1"); 
       vidRectangle2 = new Rectangle(0, 0, 800, 600); 

      } 
      catch { Debug.Write("Video Manager Failed"); } 
     } 

     public void PlayIntro() 
     { 
      try { vidPlayer.Play(introVid); } 
      catch { Debug.Write("IntroVid Failed"); } 
     } 

     public void PlayCine1() 
     { 
      try { vidPlayer2.Play(cineVid); } 
      catch { Debug.Write("Cinematic1 Failed"); } 
     } 


     #endregion 

    } 
} 

和的Game1:

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; 
using TileEngine; 
using TimerGame.MenuButtons; 
using TimerGame.LevelManagers; 
using TimerGame.VideoManagers; 

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

     Player player; 
     SpriteFont pericles10; 

     //MENU 

     ... 

     Video1Manager videoManager; 

     //Levels 

     int currentLevel = -1; 

     int state = 0; 


     ... 


     protected override void Initialize() 
     { 

      videoManager = new Video1Manager(); 

      ... 
     } 


     protected override void LoadContent() 
     { 
      spriteBatch = new SpriteBatch(GraphicsDevice); 


     ... 

      //Video 

      videoManager.Initialize(Content); 

      if (state == 0) 
       videoManager.PlayIntro(); 


      ... 


     } 

     protected override void Update(GameTime gameTime) 
     { 

      ... 

      switch (state) 
      { 
       case 0: 
        if (keyState.IsKeyDown(Keys.Escape)) 
        { 
         videoManager.vidPlayer.Stop(); 
         state = 1; 
        } 
        if (videoManager.vidPlayer.State == MediaState.Stopped) 
        { 
         state = 1; 
         MediaPlayer.Play(menuMusic); 
        } 

        break; 

       case 1: 

        ... 

         #region Playing 

         case GameState.Playing: 
          IsMouseVisible = false; 

          ... 
           switch (currentLevel) 
           { 
            case 0: 

             videoManager.PlayCine1(); 

             if (videoManager.vidPlayer2.State == MediaState.Stopped) 
             { 
              currentLevel++; 
              Level0Manager.LoadLevel(); 
             } 

             break; 

            case 1: 

             player.Update(gameTime); 
             Level0Manager.Update(gameTime); 
             if (Level0Manager.LevelComplete) 
             { 
              currentLevel++; 
             } 

             break; 

            ...some code 

         #endregion 
        } 

        break; 

      } 

      base.Update(gameTime); 
     } 


     protected override void Draw(GameTime gameTime) 
     { 

      GraphicsDevice.Clear(Color.Black); 


      videoManager.vidTexture = videoManager.vidPlayer.GetTexture(); //Video TEXTURE    


      spriteBatch.Begin(
       SpriteSortMode.BackToFront, 
       BlendState.AlphaBlend);    

      switch (state) 
      { 
       case 0: 
        spriteBatch.Draw(videoManager.vidTexture, videoManager.vidRectangle, Color.White); 

        break; 

       case 1: 

        switch (gameState) 
        { 

         #region Menu Draw 

         ... 

          break; 

         #endregion 

         #region Playing Draw 

         case GameState.Playing: 

          MediaPlayer.Stop(); 
          //spriteBatch.DrawString(pericles10, "12:20", new Vector2(100, 20), Color.Red); 
          TileMap.Draw(spriteBatch); 

          switch (currentLevel) 
          { 
           case 0: 
            videoManager.vidTexture2 = videoManager.vidPlayer2.GetTexture();          
            spriteBatch.Draw(videoManager.vidTexture2, videoManager.vidRectangle2, Color.White); 

            break; 

           case 1: 
            player.Draw(spriteBatch); 
            Level0Manager.Draw(spriteBatch); 
            break; 

           ....other code 

          break; 

         #endregion 

        } 

        break; 
      } 

      spriteBatch.End(); 


      base.Draw(gameTime); 

     } 
    } 
} 
+0

只是一個提示:如果你簡化代碼和問題,那麼你可能會得到更多的答案。 – FunctionR

+0

你說 - 當我做第二個'Texture2D vidTexture2'時,當第二個視頻應該播放時,它總是把我帶出遊戲,並說'vidTexture2是NULL'。我甚至沒有在代碼中看到'Texture2D vidTexture2'。 xD – Wallstrider

+0

@FunctionR我看到你的建議,但是,爲什麼從標題中刪除XNA 4.0? – Chipnstein3D

回答

0

你必須檢查的質地是不是空的,我建議你檢查MSDN下面鏈接。從MSDN

代碼片段:

// Draw the video, if we have a texture to draw. 
if (videoTexture != null) 
{ 
    spriteBatch.Begin(); 
    spriteBatch.Draw(videoTexture, screen, Color.White); 
    spriteBatch.End(); 
} 

來源:http://msdn.microsoft.com/en-us/library/dd904198.aspx