2011-07-02 82 views
0

我跟着a tutorial on Video Player訪問已發佈應用程序中的資源

這很好,但我想象的是一種不同的情況。

我需要嵌入資源文件中的一些視頻,並根據用戶輸入動態更改視頻。 我使用switch語句在Visual Studio 2010中管理了這個運行。 但我必須在資源文件中指定該視頻的路徑。對於如:

System.NullReferenceException: Object reference not set to an instance of an object. 
    at VideoPlayer.Form1.button2_Click(Object sender, EventArgs e) 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e)............. 

看來,視頻文件沒有包含在應用程序:

case 1 : video = new Video("..//..//Resources//The video name"); 

但是當我發佈使用點擊一次嚮導此應用程序,最終應用在異常結束。 但是,當我看到安裝程序文件夾,清晰的外觀顯示,有一個文件名爲VideoPlayer.exe.deploy它通常是59MB這些文件沒有任何資源是輕的加權...

它肯定包含視頻文件。但我無法訪問該文件。

如何訪問?

請幫我...

在此先感謝:-)

至於問一些用戶,我張貼在C#.NET整個代碼:

的設計師。 CS文件:

namespace PlayVideo 
{ 
    partial class Form1 
    { 
     // <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.viewport = new System.Windows.Forms.Panel(); 
      this.button1 = new System.Windows.Forms.Button(); 
      this.button2 = new System.Windows.Forms.Button(); 
      this.button3 = new System.Windows.Forms.Button(); 
      this.label1 = new System.Windows.Forms.Label(); 
      this.comboBox1 = new System.Windows.Forms.ComboBox(); 
      this.SuspendLayout(); 
      // 
      // viewport 
      // 
      this.viewport.Location = new System.Drawing.Point(12, 39); 
      this.viewport.Name = "viewport"; 
      this.viewport.Size = new System.Drawing.Size(391, 368); 
      this.viewport.TabIndex = 0; 
      // 
      // button1 
      // 
      this.button1.Location = new System.Drawing.Point(55, 418); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(79, 27); 
      this.button1.TabIndex = 0; 
      this.button1.Text = "Play"; 
      this.button1.UseVisualStyleBackColor = true; 
      this.button1.Click += new System.EventHandler(this.button1_Click); 
      // 
      // button2 
      // 
      this.button2.Location = new System.Drawing.Point(167, 418); 
      this.button2.Name = "button2"; 
      this.button2.Size = new System.Drawing.Size(79, 27); 
      this.button2.TabIndex = 1; 
      this.button2.Text = "Pause"; 
      this.button2.UseVisualStyleBackColor = true; 
      this.button2.Click += new System.EventHandler(this.button2_Click); 
      // 
      // button3 
      // 
      this.button3.Location = new System.Drawing.Point(279, 418); 
      this.button3.Name = "button3"; 
      this.button3.Size = new System.Drawing.Size(79, 27); 
      this.button3.TabIndex = 2; 
      this.button3.Text = "Stop"; 
      this.button3.UseVisualStyleBackColor = true; 
      this.button3.Click += new System.EventHandler(this.button3_Click); 
      // 
      // label1 
      // 
      this.label1.AutoSize = true; 
      this.label1.Location = new System.Drawing.Point(12, 15); 
      this.label1.Name = "label1"; 
      this.label1.Size = new System.Drawing.Size(108, 13); 
      this.label1.TabIndex = 3; 
      this.label1.Text = "Load Video number : "; 
      // 
      // comboBox1 
      // 
      this.comboBox1.FormattingEnabled = true; 
      this.comboBox1.Items.AddRange(new object[] { 
      "Video number 1", 
      "Video number 2", 
      "Video number 3"}); 
      this.comboBox1.Location = new System.Drawing.Point(132, 12); 
      this.comboBox1.Name = "comboBox1"; 
      this.comboBox1.Size = new System.Drawing.Size(121, 21); 
      this.comboBox1.TabIndex = 4; 
      this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(415, 457); 
      this.Controls.Add(this.comboBox1); 
      this.Controls.Add(this.label1); 
      this.Controls.Add(this.button3); 
      this.Controls.Add(this.button2); 
      this.Controls.Add(this.button1); 
      this.Controls.Add(this.viewport); 
      this.Name = "Form1"; 
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
      this.Text = "Video Player"; 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.Panel viewport; 
     private System.Windows.Forms.Button button1; 
     private System.Windows.Forms.Button button2; 
     private System.Windows.Forms.Button button3; 
     private System.Windows.Forms.Label label1; 
     private System.Windows.Forms.ComboBox comboBox1; 
    } 
} 

而另一個.cs文件是:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using Microsoft.DirectX.AudioVideoPlayback; 

namespace PlayVideo 
{ 
    public partial class Form1 : Form 
    { 
     private Video video; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      switch (comboBox1.SelectedIndex) 
      { 
       case 0: video = new Video("..//..//Resources//Video1.mp4"); 
         break; 
       case 1: video = new Video("..//..//Resources//Video2.DAT"); 
         break; 
       case 2: video = new Video("..//..//Resources//Video3.DAT"); 
         break; 
      } 
      int width = viewport.Width; 
      int height = viewport.Height; 
      // set the panel as the video object’s owner 
      video.Owner = viewport; 

      // stop the video 
      video.Stop(); 

      // resize the video to the size original size of the panel 
      viewport.Size = new Size(width, height); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (video.State != StateFlags.Running) 
      { 
       video.Play(); 
      } 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      if (video.State == StateFlags.Running) 
      { 
       video.Pause(); 
      } 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      if (video.State != StateFlags.Stopped) 
      { 
       video.Stop(); 
      } 
     } 
    } 
} 

而且我認爲一個屏幕截圖可能是一些信息:

enter image description here

它工作正常,在調試模式下.....但是,一旦我使用點擊一次安裝程序安裝它,它崩潰成例外。如何使用DEPLOY文件中的資源管理相同的內容?

+0

難道你只是混淆了斜槓和反斜槓嗎? – svick

+0

不...實際上我發佈的代碼在Visual Studio下「調試」解決方案時運行得非常好。注意:實際上我有比調試文件夾高兩級的Resources文件夾。因此,斜槓。但是,當我使用Click Once Application安裝應用程序時,它給了我提到的例外。我認爲這是因爲一旦我們在我們的Windows機器上安裝了應用程序,工作組件就不一樣了。另外安裝的文件系統也不同。任何人都可以幫助我在這個?????????請...... – killerCoder

回答

2

在調試應用程序時,您正在引用VS解決方案中的文件,因爲這些路徑在項目的\ bin \ debug文件夾中有效。如果您已設置集結行動的資源爲「嵌入的資源」,您可以使用訪問這些文件:

typeof(Form1).Assembly.GetManifestResourceStream("PlayVideo.Resources.Video1.dat"); 

MSDN:http://msdn.microsoft.com/en-us/library/xc4235zt.aspx

更新 我已經證實視頻類不會接受流,並且res:// - 協議也不能使用。我建議你不要將視頻嵌入到可執行文件中,而是將它們放到應用程序目錄中。您可以將視頻文件的構建操作更改爲無,並將「複製到輸出目錄」屬性更改爲「複製如果更新」。構建應用程序後,所有視頻都將放入輸出目錄中,您可以將其與可執行文件一起部署。然後,您應該使用初始方法,但使用「The video name.dat」之類的路徑,而不是「..//..//Resources/The video name.dat」。

+0

好的...... @J Tihon ....謝謝你的好幫助:-) – killerCoder

相關問題