[C#] Problem mit absoluten Pfaden

krizzelfix

Commander
Registriert
Sep. 2005
Beiträge
2.626
Hallo Community,

ich habe ein Problem mit der winmm.dll. Mit der dll ist es möglich verschiedene Musik und Videoformate abzuspielen.
Habe erst ein kleines Konsolen Programm geschrieben und getest.
Habe jetzt ein Form mit eingebaut und suche die MP3's mit einem FileChooser aus. Wenn ich jetzt den Pfad an die DLL übergebe, wird kein Sound abgespielt. Bei einem relative Pfad geht es aber.

Kennt jemand das Problem oder weiss wie man es umgeht? Habe es schon mit dem @-Zeichen vor dem String probiert. Leider ohne erfolg.

Grüße

tewes
 
Wie machst du den Aufruf bzw. wie sehen die Parameter aus?

Ich hatte das mal probiert, da ich ein kleines Tool brauchte, das unsichtbar wav-Dateien abspielt. So in etwa sieht das aus:
Code:
        [System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        private static extern bool PlaySound(string szSound, System.IntPtr hMod, PlaySoundFlags flags);

        private enum PlaySoundFlags : int
        {
            SYNC = 0x0000,/* play synchronously (default) */
            ASYNC = 0x0001, /* play asynchronously */
            NODEFAULT = 0x0002, /* silence (!default) if sound not found */
            MEMORY = 0x0004, /* pszSound points to a memory file */
            LOOP = 0x0008, /* loop the sound until next sndPlaySound */
            NOSTOP = 0x0010, /* don't stop any currently playing sound */
            NOWAIT = 0x00002000, /* don't wait if the driver is busy */
            ALIAS = 0x00010000,/* name is a registry alias */
            ALIAS_ID = 0x00110000, /* alias is a pre d ID */
            FILENAME = 0x00020000, /* name is file name */
            RESOURCE = 0x00040004, /* name is resource name or atom */
            PURGE = 0x0040,  /* purge non-static events for task */
            APPLICATION = 0x0080 /* look for application specific association */
        }

        ...
           PlaySound(@"C:\WINDOWS\Media\Windows XP-Startvorgang.wav", new System.IntPtr(), PlaySoundFlags.ASYNC|PlaySoundFlags.NOWAIT|PlaySoundFlags.FILENAME);
        ...
Das Ur-Beispiel stammt aus dem MSDN. Den PlaySoundFlags-enum habe ich von The Code Project.

Ach ja, bei mir läuft das unter XP...
 
Hallo Gobble-G,

ich mache das momentan so:
Code:
// Öffnen der Datei
OpenFileDialog Selecter = new OpenFileDialog();
            Selecter.Multiselect = false;
            DialogResult result = Selecter.ShowDialog();

            try
            {
                if (result == DialogResult.OK)
                {
                    MCI.FileName = Selecter.FileName;
                }
                else
                {
                    //relativer Pfad:
                    //MCI.FileName = "test.mp3";

                    //absoluter Pfad:
                    //MCI.FileName = @"C:\Users\xxxx\Documents\Visual Studio 2005\Projects\WinXMS\WinXMS\bin\Debug\test.mp3";
                    MCI.FileName = @"C:\Users\xxxx\Desktop\WinXMS\WinXMS\bin\Debug\test.mp3";
                }
                label1.Text = MCI.FileName;
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show("Die Datei \"" + MCI.FileName + "\" wurde nicht gefunden.",
                                ex.Message,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error,
                                MessageBoxDefaultButton.Button1);
            }

...        #region DllImport ...
        [DllImport("winmm.dll")]
        private static extern int mciSendString(string @cmd, StringBuilder ret, int retLen, IntPtr hwnd);
        [DllImport("winmm.dll")]
        private static extern int mciGetErrorString(int errCode, StringBuilder errText, int errLen);
        #endregion

        private bool _isOpen = false;
        private string _FileName = "";
        private const int lenght = 256;
        private StringBuilder buffer = new StringBuilder(256);

        public void Play()
        {
            Command("play " + _FileName);
        }
        private int Command(string CMD)
        {
            return mciSendString(@CMD, null, 0, IntPtr.Zero);
        }
        public string FileName
        {
            set
            {
                if (File.Exists(value))
                {
                    _FileName = value;
                }
                else
                {
                    throw new FileNotFoundException("The File \"" + value + "\" was not found.");
                }
            }
            get
            {
                return _FileName;
            }
        }
Habs bei mir unter Vista und XP getestet...aber hat nichts funktioniert
Grüße

tewes

//Edit: Hab das problem gelöst. Wenn ich um die Variable _FileName noch Anführungszeichen setzte geht alles.
 
Zuletzt bearbeitet:
Zurück
Oben