OpenFileDialog not compatible with DirectX, alternatives?

Started by
4 comments, last by theStormWeaver 17 years, 2 months ago
I use C#. I wanted to grant my users the option to select any mp3 on their system and use it as the bg music in my game, so I thought "This'll be easy, give my Game class constructor a string parameter that will be supplied by an OpenFileDialog, and make sure to provide a default mp3 file ofcourse." Except that OpenFileDialog's require the keyword [STAThread] to be placed at the begining of your main class(in my case the frmMain class). This is completely in-compatible with DirectX. I get these errors about some kind of managed code in 3 or 4 random places in the code, one of which is where I instantiate my custom objects(paddle, ball, and an array of blocks). I really like the idea of users being able to play to their favorite songs, do any of you know of any possible alternaties? Remember, the files must be restricted to the mp3 data type because thats the only kind of file DirectSound.AudioVideoPlayback will accept. Thanks for any help in advance.
Advertisement
A cursory Google search doesn't reveal any information about an incompatibility between STA Thread and DirectX.. Perhaps you can give more details about the compile-time errors you're having?

The simplest solution would be to simply prompt the user (say, in a simple form with a text box and a label) to enter the path to the MP3 they wish to play. Or for simplicity, require that the mp3 be in a specific directory and prompt the user to enter the filename.

Alternatively you could try to hack OpenFileDialog and see if you can make your own implementation that doesn't require you to use STA Thread.. Only advice I can give you here is that OpenFileDialog is not natively implemented in .NET, it makes use of a Win32 API called GetOpenFileName.

In any case that seems cumbersome and I would focus on getting your program to compile with [STAThread] and use the OpenFileDialog as you originally intended.
I have used Managed DirectX with the [STAThread] attribute many many (..many) times [grin] so there are no issues between it and Managed DirectX. So, as steelBovine has said, provide the errors and also any necessary code within [source][/source] tags.

Regards,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
This is the error message(which, by the way, only appears if I choose an mp3 that isn't the one I packaged with the game, and isn't the message I remember getting two months ago.)

ArgumentException was Unhandled

Parameter is not Valid

The code I got it in is here:
bmpRedBlock = new Bitmap("redBlock.bmp");


I included the majority of my frmMain code. I left out the form objects (the first elipse) and the auto-generated InitializeComponent(the second elipse).

public class frmMain : Form {	...        OpenFileDialog ofd;        string soundPath;	...        [STAThread]        public static void Main()        {            frmMain main = new frmMain();            Application.Run(main);        }//end Main methodpublic frmMain()        {            InitializeComponent();            lstLevels.SelectedItem = "1";            ofd = new OpenFileDialog();            ofd.Title = "Select an MP3 file";            ofd.Filter = "MP3 (*.mp3)|*.mp3";            soundPath = Path.Combine(Application.StartupPath, "bgSound.mp3");            txtBgSoundFileName.Text = soundPath;        }//end frmMain method        private void btnStart_Click(object sender, EventArgs e)        {            int selectedLevel;            selectedLevel = int.Parse(lstLevels.SelectedItem.ToString());            theMenuScreen = this;            GameEngine game = new GameEngine(selectedLevel, soundPath);            game.Text = "BreakOut 2007";            game.Show();        }        private void btnClose_Click(object sender, EventArgs e)        {            Close();        }// end btnClose_Click        public void setLabel(string text)        {            lblOutcome.Text = text;        }//end setLabel        private void btnBrowse_Click(object sender, EventArgs e)        {            if (ofd.ShowDialog() == DialogResult.OK)            {                soundPath = ofd.FileName;                txtBgSoundFileName.Text = ofd.FileName;            }//end if        }//end btnBrowse_Click    }//end class frmMain
It does not seem your error has anything to do with STAThread or OpenFileDialog.

The Bitmap constructor overload Bitmap(string filename), which you are trying to use, will throw a System.ArgumentException iff the stream does not contain image data or is a null reference. So you need to take a look at redBlock.bmp.

I am currently building my first game with C# and XNA (Tetris clone, w00t) and have used OpenFileDialog successfully to get an mp3 filename to play in the background with DirectSound.AudioVideoPlayback. If you would like I can send you my code via PM.
That doesn't make any sense. My program works (nearly) flawlessly without this OpenFileDialog feature. I don't even get this problem if I leave the default as is and press start.

Okay, it does compile, maybe I forgot to mention that. If I press Browse, and pick another mp3, the file path appears in the text box as it should. Then I press Start and the music begins, but the program crashes right after and gives me that error.

BUT, if I don't press Browse (just leave it at the default) and just press Start it works fine.

Whats up with that?

P.S. Mine is BreakOut :)

This topic is closed to new replies.

Advertisement