2011-07-02 7 views
0

私はa tutorial on Video Playerに続きます。公開アプリケーションのリソースへのアクセス

いいですが、私は別の状況を想像しています。

私は埋め込みのリソースファイルにいくつかのビデオを必要とし、動的にビデオをユーザーの入力に応じて変更する必要があります。 私はこれをVisual Studio 2010で実行し、switch文を使用して管理しています。 しかし、そのビデオのパスをリソースファイルに指定する必要があります。

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

しかし、私は、ウィザードたらクリックを使用して、このアプリケーションを公開する場合、最終的なアプリケーションが例外で終わる:例えばのため

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

ビデオファイルがアプリケーションに含まれていなかったようです。 しかし、私はインストーラのフォルダを見たときに、明確な外観は、59MBののVideoPlayer.exe.deployという名前のファイルがあることを示しました。これらのファイルはリソースがなくても軽量です...

必ずビデオファイル。しかし、私はそのファイルにアクセスすることはできません。

どうすればアクセスできますか?一部のユーザーによって求められたよう

...

:-)事前に

おかげで私を助けてください、私は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

これは、デバッグモードで正常に動作します.....しかし、これは、 Exceptionにクラッシュします。 DEPLOYファイル内のリソースを使用して同じものをどのように管理しますか?

+0

スラッシュとバックスラッシュの混乱はありませんか? – svick

+0

いいえ...実際に私が投稿したコードは、Visual Studioの下でソリューションを「デバッグ」しているときに完璧に動作します。注:実際には、リソースフォルダはデバッグフォルダよりも2レベル高いです。したがって、スラッシュ。しかし、私は一度クリックしてアプリケーションを使用してアプリケーションをインストールすると、私は言及した例外を私に与えます。 Windowsマシンにアプリケーションをインストールすると、作業アセンブリが異なるためだと思います。また、インストールされているファイルシステムも異なります。誰でもこのことで私を助けることができます?????????してください...... – killerCoder

答えて

2

パスはプロジェクトの\ bin \ debugフォルダから有効なので、アプリケーションをデバッグするときに正常に動作するVSソリューションのファイルを参照しています。あなたは、「埋め込まれたリソース」へのリソースのビルドアクションを設定している場合は、使用してそれらのファイルにアクセスすることができます。

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

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

更新 私はビデオいることが確認されましたクラスはストリームを受け入れず、res:// - プロトコルも使用できません。実行可能ファイルにビデオを埋め込むのではなく、アプリケーションディレクトリに配置することをお勧めします。ビデオファイルのビルドアクションをnoneに変更し、 "Copy to Output Directory"プロパティを "Copy If Newer"に変更することができます。アプリケーションがビルドされると、すべてのビデオが出力ディレクトリに置かれます。このディレクトリは、実行可能ファイルとともにデプロイできます。最初のアプローチを使用する必要がありますが、 "..//////Resources/The video name.dat"ではなく "Video name.dat"のようなパスを使用してください。

+0

OK ... @J Tihon ....あなたの素晴らしい助けをありがとう:-) – killerCoder