Accessing the resources in the published application
I followed a tutorial on Video Player.
It is nice but I am imagining a different situation.
I need to embed some videos in the Resources file and dynamically change the videos depending on the user input. I have managed this running inside Visual Studio 2010. using the switch statements. But I have to specify the path of that video in the resources file. for eg :
case 1 : video = new Video("..//..//Resources//The video name");
But when I publish this application using the click once wizard, the final application ends up in an exception :
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).............
It seems that the video file was not included in the application. But when i saw the installer folder, a clear look showed that there is a file as named VideoPlayer.exe.deploy which is of 59MB normally these files without any resources are light weighted...
It surely contains the Video file. But I cannot access that file.
How Do I Access It?
Please help me out...
Thanks in advance :-)
As asked by some users, I am posting the entire code in C#.NET :
The Designer.cs File :
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 Syst开发者_JS百科em.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;
}
}
And the other .cs file is :
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();
}
}
}
}
And I thought a screenshot may be of some information :
It works fine in Debugging mode..... But once I install it using the click once installer, It crashes into an Exception. How do I manage the same using the Resources in the DEPLOY file?
You are referencing the files in your VS solution, which works fine, when debugging the application, since the paths are valid from your project's \bin\debug folder. If you've set the build-action for the resources to "Embedded Resource", you can access those files using:
typeof(Form1).Assembly.GetManifestResourceStream("PlayVideo.Resources.Video1.dat");
MSDN: http://msdn.microsoft.com/en-us/library/xc4235zt.aspx
Update I've confirmed that the Video class won't accept a Stream and that the res://-Protocol cannot be used either. I would suggest, that you don't embed the videos into the executable, but place them into the Application directory. You can change the build-action of the video-files to none and change the "Copy to Output Directory" property to "Copy If Newer". Once the application is built, all videos are put in the output directory, which you can deploy along with the executable. You should then use your initial approach, but use a path like "The video name.dat" rather than "..//..//Resources/The video name.dat".
精彩评论