I'm making a C# Windows Forms program in Visual Studio which keeps crashing whenever I set the URL to a windows media player axWindowsMediaPLayer object.
There are two form windows: MainWindow and VideoForm. There are buttons on MainWindow to select a file via a fileDialog, and a button to open the VideoForm window which has the media player in it. I created a custom Video.cs class to capture the video file information. It also handles the fileDialog. However, when I set the URL to the media player object, the program crashes with code 3221225477 (0xc0000005) 'Access violation'. So, the URL of the media player in the VideoForm window is being set from the button click event in the MainWindow. I'm not sure if this is what's causing the access violation. I have run Visual Studio as an administrator to ensure that it should have access to the file. I have checked the file path and it is correct. I've tried with and without the preceding @.
This is the line that causes the crash:
VideoWindow.MediaPlayer.URL = @auditVideo.Path;
The relevant code is below:
MainWindow.cs:
Video auditVideo = new Video();
private void ButtonImportVideo_Click(object sender, EventArgs e)
{
auditVideo.InitializeFile(openFileDialogVideo);
textBoxVideoFile.Text = auditVideo.Name;
}
private void ButtonPlayVideo_Click(object sender, EventArgs e)
{
VideoForm VideoWindow = new VideoForm();
try
{
VideoWindow.MediaPlayer.URL = @auditVideo.Path; // This is what causes the crash
}
catch(Exception ex)
{
MessageBox.Show("could not load the file" + ex.Message);
}
Console.WriteLine("VideoWindow.MediaPlayer.URL is {0}", @VideoWindow.MediaPlayer.URL);
VideoWindow.Show();
}
Video.cs class:
namespace AVCAudit
{
internal class Video
{
internal OpenFileDialog OpenFileDialog { get; private set; } //This is an AutoProperty which generates the private field behind the scenes
internal string Path { get; private set; } //set should be private for Path and Name since they refer to the actual file on disk which doesn't change
internal string Name { get; private set; }
internal void InitializeFile(OpenFileDialog fileDialogArg)
{
OpenFileDialog = fileDialogArg;
OpenFileDialog.InitialDirectory = @"C:\Users\schaney\Desktop\Neology\AVC Audit Project\avc_audit\Recordings";
OpenFileDialog.Title = "Open audit video file";
OpenFileDialog.Filter = "(mp3,wav,mp4,mov,wmv,mpg)|*.mp3;*.wav;*.mp4;*.mov;*.wmv;*.mpg|all files|*.*";
if (OpenFileDialog.ShowDialog() == DialogResult.OK)
{
Path = fileDialogArg.FileName;
Name = fileDialogArg.SafeFileName;
}
}
}
}
VideoForm.cs
namespace AVCAudit
{
public partial class VideoForm : Form
{
internal String test { get; set; }
public VideoForm() //constructor for VideoForm class. The rest of the partial class is defined in VideoForm.Designer.cs
{
InitializeComponent(); //so the constructor for this class just runs the InitializeComponent method defined in the Designer.cs file
}
private void MediaPlayer_Enter(object sender, EventArgs e)
{
}
}
}