none
Need some APIs about Media Playback

    Question

  • I want to know how to make sure the system is playing music or not.

    And I want to launch the default music player if is not playing music.

    I found Windows.Media.Playback, but it seems just for UWP. 

    

    found maybe I can use SendKeys.Send("{SelectMedia}");

    Bug it seems can not take effect.


    • Edited by 王静凯 Wednesday, April 5, 2017 3:28 AM
    Monday, April 3, 2017 5:13 AM

All replies

  • Hi 王静凯,

    Thank you for posting here.

    For your question, what is your project?

    For Winform, you could download the source file from Audio and Video Player C# Winform for reference.

    For WPF, you could download the source file from Using Windows Media Player Control in WPF for reference.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 4, 2017 5:12 AM
    Moderator
  • Hi,

    I don't want to play music in my c# winform app.

    I just want to know how to launch the default music player of windows.

    Maybe I want to know the keycode of launching music player?(use SendKeys.Send())

    Tuesday, April 4, 2017 7:13 AM
  • Hi,

    you can check process of window, i think it's simple way to resolve your issue.

    example i usually call a program from window like:

    Process.Start("wmplayer.exe", "C:\\Users\\HoaND\\Music\\Playlists\\list1.wpl");
    Process.Start("wmplayer.exe", "path of WPL file");

    Tuesday, April 4, 2017 7:52 AM
  • Hi,

    I don't want to play music in my c# winform app.

    I just want to know how to launch the default music player of windows.

    Maybe I want to know the keycode of launching music player?(use SendKeys.Send())

    "Music" is vague.

    Each extension can be associated with a different executable.

    You can find them  (*.mp3, *.wav, etc...) with AssocQueryString(),

    then you can launch the executable with Process.Start

    Tuesday, April 4, 2017 8:07 AM
  • Hi,

    you can check process of window, i think it's simple way to resolve your issue.

    example i usually call a program from window like:

    Process.Start("wmplayer.exe", "C:\\Users\\HoaND\\Music\\Playlists\\list1.wpl");
    Process.Start("wmplayer.exe", "path of WPL file");

    I don't want to open a specific file, just launch the application
    Tuesday, April 4, 2017 1:42 PM
  • Hi,

    I don't want to play music in my c# winform app.

    I just want to know how to launch the default music player of windows.

    Maybe I want to know the keycode of launching music player?(use SendKeys.Send())

    "Music" is vague.

    Each extension can be associated with a different executable.

    You can find them  (*.mp3, *.wav, etc...) with AssocQueryString(),

    then you can launch the executable with Process.Start

    thx, maybe it's a good idea
    Tuesday, April 4, 2017 1:43 PM
  • No more solution?

    I found one of my keyboard press SelectMedia can launch the music player, but I use c# winform app to simulate press SelectMedia but can not launch music player.

    So I want to know why?

    Saturday, April 8, 2017 4:26 AM
  • SelectMedia from keyboard also uses AssocQueryString() + SHRegGetValueFromHKCUHKLM() with "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts"
    Saturday, April 8, 2017 6:38 AM
  • SelectMedia from keyboard also uses AssocQueryString() + SHRegGetValueFromHKCUHKLM() with "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts"
    Can you say it in more detail?
    Sunday, April 9, 2017 7:09 AM
  • For example with AssocQueryString()

    {
                    string sExtension = ".wav";
                    int nLength = 260;
                    var sbAssociatedExe = new StringBuilder(nLength);
                    int nReturn = AssocQueryString(AssocF.ASSOCF_NONE, AssocStr.ASSOCSTR_EXECUTABLE, sExtension, null, sbAssociatedExe, ref nLength);
                    if (nReturn == 0)
                    {
                        using (Process exeProcess = new Process())
                        {
                            exeProcess.StartInfo.FileName = sbAssociatedExe.ToString();
                            exeProcess.StartInfo.UseShellExecute = true;
                            exeProcess.Start();
                        }
                    }
    }

    With the declarations :

            [Flags]
            enum AssocF : uint
            {
                ASSOCF_NONE = 0,
                ASSOCF_INIT_NOREMAPCLSID = 0x1,
                ASSOCF_INIT_BYEXENAME = 0x2,
                ASSOCF_OPEN_BYEXENAME = 0x2,
                ASSOCF_INIT_DEFAULTTOSTAR = 0x4,
                ASSOCF_INIT_DEFAULTTOFOLDER = 0x8,
                ASSOCF_NOUSERSETTINGS = 0x10,
                ASSOCF_NOTRUNCATE = 0x20,
                ASSOCF_VERIFY = 0x40,
                ASSOCF_REMAPRUNDLL = 0x80,
                ASSOCF_NOFIXUPS = 0x100,
                ASSOCF_IGNOREBASECLASS = 0x200,
                ASSOCF_INIT_IGNOREUNKNOWN = 0x400,
                ASSOCF_INIT_FIXED_PROGID = 0x800,
                ASSOCF_IS_PROTOCOL = 0x1000,
                ASSOCF_INIT_FOR_FILE = 0x2000
            }
    
            enum AssocStr
            {
                ASSOCSTR_COMMAND = 1,
                ASSOCSTR_EXECUTABLE,
                ASSOCSTR_FRIENDLYDOCNAME,
                ASSOCSTR_FRIENDLYAPPNAME,
                ASSOCSTR_NOOPEN,
                ASSOCSTR_SHELLNEWVALUE,
                ASSOCSTR_DDECOMMAND,
                ASSOCSTR_DDEIFEXEC,
                ASSOCSTR_DDEAPPLICATION,
                ASSOCSTR_DDETOPIC,
                ASSOCSTR_INFOTIP,
                ASSOCSTR_QUICKTIP,
                ASSOCSTR_TILEINFO,
                ASSOCSTR_CONTENTTYPE,
                ASSOCSTR_DEFAULTICON,
                ASSOCSTR_SHELLEXTENSION,
                ASSOCSTR_DROPTARGET,
                ASSOCSTR_DELEGATEEXECUTE,
                ASSOCSTR_SUPPORTED_URI_PROTOCOLS,
                ASSOCSTR_PROGID,
                ASSOCSTR_APPID,
                ASSOCSTR_APPPUBLISHER,
                ASSOCSTR_APPICONREFERENCE,
                ASSOCSTR_MAX
            }
    
            [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
            private static extern int AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, ref int pcchOut);

    Sunday, April 9, 2017 9:03 AM
  • For example with AssocQueryString()

    {
                    string sExtension = ".wav";
                    int nLength = 260;
                    var sbAssociatedExe = new StringBuilder(nLength);
                    int nReturn = AssocQueryString(AssocF.ASSOCF_NONE, AssocStr.ASSOCSTR_EXECUTABLE, sExtension, null, sbAssociatedExe, ref nLength);
                    if (nReturn == 0)
                    {
                        using (Process exeProcess = new Process())
                        {
                            exeProcess.StartInfo.FileName = sbAssociatedExe.ToString();
                            exeProcess.StartInfo.UseShellExecute = true;
                            exeProcess.Start();
                        }
                    }
    }

    With the declarations :

            [Flags]
            enum AssocF : uint
            {
                ASSOCF_NONE = 0,
                ASSOCF_INIT_NOREMAPCLSID = 0x1,
                ASSOCF_INIT_BYEXENAME = 0x2,
                ASSOCF_OPEN_BYEXENAME = 0x2,
                ASSOCF_INIT_DEFAULTTOSTAR = 0x4,
                ASSOCF_INIT_DEFAULTTOFOLDER = 0x8,
                ASSOCF_NOUSERSETTINGS = 0x10,
                ASSOCF_NOTRUNCATE = 0x20,
                ASSOCF_VERIFY = 0x40,
                ASSOCF_REMAPRUNDLL = 0x80,
                ASSOCF_NOFIXUPS = 0x100,
                ASSOCF_IGNOREBASECLASS = 0x200,
                ASSOCF_INIT_IGNOREUNKNOWN = 0x400,
                ASSOCF_INIT_FIXED_PROGID = 0x800,
                ASSOCF_IS_PROTOCOL = 0x1000,
                ASSOCF_INIT_FOR_FILE = 0x2000
            }
    
            enum AssocStr
            {
                ASSOCSTR_COMMAND = 1,
                ASSOCSTR_EXECUTABLE,
                ASSOCSTR_FRIENDLYDOCNAME,
                ASSOCSTR_FRIENDLYAPPNAME,
                ASSOCSTR_NOOPEN,
                ASSOCSTR_SHELLNEWVALUE,
                ASSOCSTR_DDECOMMAND,
                ASSOCSTR_DDEIFEXEC,
                ASSOCSTR_DDEAPPLICATION,
                ASSOCSTR_DDETOPIC,
                ASSOCSTR_INFOTIP,
                ASSOCSTR_QUICKTIP,
                ASSOCSTR_TILEINFO,
                ASSOCSTR_CONTENTTYPE,
                ASSOCSTR_DEFAULTICON,
                ASSOCSTR_SHELLEXTENSION,
                ASSOCSTR_DROPTARGET,
                ASSOCSTR_DELEGATEEXECUTE,
                ASSOCSTR_SUPPORTED_URI_PROTOCOLS,
                ASSOCSTR_PROGID,
                ASSOCSTR_APPID,
                ASSOCSTR_APPPUBLISHER,
                ASSOCSTR_APPICONREFERENCE,
                ASSOCSTR_MAX
            }
    
            [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
            private static extern int AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, ref int pcchOut);

    thank you very much

    Sunday, April 9, 2017 3:21 PM
  • For example with AssocQueryString()

    {
                    string sExtension = ".wav";
                    int nLength = 260;
                    var sbAssociatedExe = new StringBuilder(nLength);
                    int nReturn = AssocQueryString(AssocF.ASSOCF_NONE, AssocStr.ASSOCSTR_EXECUTABLE, sExtension, null, sbAssociatedExe, ref nLength);
                    if (nReturn == 0)
                    {
                        using (Process exeProcess = new Process())
                        {
                            exeProcess.StartInfo.FileName = sbAssociatedExe.ToString();
                            exeProcess.StartInfo.UseShellExecute = true;
                            exeProcess.Start();
                        }
                    }
    }

    With the declarations :

            [Flags]
            enum AssocF : uint
            {
                ASSOCF_NONE = 0,
                ASSOCF_INIT_NOREMAPCLSID = 0x1,
                ASSOCF_INIT_BYEXENAME = 0x2,
                ASSOCF_OPEN_BYEXENAME = 0x2,
                ASSOCF_INIT_DEFAULTTOSTAR = 0x4,
                ASSOCF_INIT_DEFAULTTOFOLDER = 0x8,
                ASSOCF_NOUSERSETTINGS = 0x10,
                ASSOCF_NOTRUNCATE = 0x20,
                ASSOCF_VERIFY = 0x40,
                ASSOCF_REMAPRUNDLL = 0x80,
                ASSOCF_NOFIXUPS = 0x100,
                ASSOCF_IGNOREBASECLASS = 0x200,
                ASSOCF_INIT_IGNOREUNKNOWN = 0x400,
                ASSOCF_INIT_FIXED_PROGID = 0x800,
                ASSOCF_IS_PROTOCOL = 0x1000,
                ASSOCF_INIT_FOR_FILE = 0x2000
            }
    
            enum AssocStr
            {
                ASSOCSTR_COMMAND = 1,
                ASSOCSTR_EXECUTABLE,
                ASSOCSTR_FRIENDLYDOCNAME,
                ASSOCSTR_FRIENDLYAPPNAME,
                ASSOCSTR_NOOPEN,
                ASSOCSTR_SHELLNEWVALUE,
                ASSOCSTR_DDECOMMAND,
                ASSOCSTR_DDEIFEXEC,
                ASSOCSTR_DDEAPPLICATION,
                ASSOCSTR_DDETOPIC,
                ASSOCSTR_INFOTIP,
                ASSOCSTR_QUICKTIP,
                ASSOCSTR_TILEINFO,
                ASSOCSTR_CONTENTTYPE,
                ASSOCSTR_DEFAULTICON,
                ASSOCSTR_SHELLEXTENSION,
                ASSOCSTR_DROPTARGET,
                ASSOCSTR_DELEGATEEXECUTE,
                ASSOCSTR_SUPPORTED_URI_PROTOCOLS,
                ASSOCSTR_PROGID,
                ASSOCSTR_APPID,
                ASSOCSTR_APPPUBLISHER,
                ASSOCSTR_APPICONREFERENCE,
                ASSOCSTR_MAX
            }
    
            [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
            private static extern int AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, ref int pcchOut);

    Hi,my mp3 file is associated with an uwp app.

    So I can't get the exe path.

    but I can get the appid.

    How can I launch the app with appid?

    Sunday, April 16, 2017 7:46 AM
  • I made some tests with an UWP app (VLC) and the simplest way for any case (Win32 or UWP) seems with ShellExecuteEx()

    =>

    {
                    var shei = new SHELLEXECUTEINFO();
                    shei.cbSize = Marshal.SizeOf(shei);
                    shei.lpClass = ".mp3";
                    shei.nShow = SW_SHOWNORMAL;
                    shei.fMask = SEE_MASK_CLASSNAME;
                    bool bReturn = ShellExecuteEx(ref shei);
    }

    Declarations :

      public struct SHELLEXECUTEINFO
            {
                public int cbSize;
                public uint fMask;
                public IntPtr hwnd;
                [MarshalAs(UnmanagedType.LPStr)]
                public string lpVerb;
                [MarshalAs(UnmanagedType.LPStr)]
                public string lpFile;
                [MarshalAs(UnmanagedType.LPStr)]
                public string lpParameters;
                [MarshalAs(UnmanagedType.LPStr)]
                public string lpDirectory;
                public int nShow;
                public IntPtr hInstApp;
                public IntPtr lpIDList;
                [MarshalAs(UnmanagedType.LPStr)]
                public string lpClass;
                public IntPtr hkeyClass;
                public uint dwHotKey;
                public IntPtr hIcon;
                public IntPtr hProcess;
            }
    
            public const int SEE_MASK_CLASSNAME = 0x00000001;
    
            public const int SW_SHOWNORMAL = 1;
    
            [DllImport("shell32.dll", CharSet = CharSet.Auto, EntryPoint = "ShellExecuteExA")]
            public static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO pExecInfo)

    Sunday, April 16, 2017 10:28 AM
  • I made some tests with an UWP app (VLC) and the simplest way for any case (Win32 or UWP) seems with ShellExecuteEx()

    =>

    {
                    var shei = new SHELLEXECUTEINFO();
                    shei.cbSize = Marshal.SizeOf(shei);
                    shei.lpClass = ".mp3";
                    shei.nShow = SW_SHOWNORMAL;
                    shei.fMask = SEE_MASK_CLASSNAME;
                    bool bReturn = ShellExecuteEx(ref shei);
    }

    Declarations :

      public struct SHELLEXECUTEINFO
            {
                public int cbSize;
                public uint fMask;
                public IntPtr hwnd;
                [MarshalAs(UnmanagedType.LPStr)]
                public string lpVerb;
                [MarshalAs(UnmanagedType.LPStr)]
                public string lpFile;
                [MarshalAs(UnmanagedType.LPStr)]
                public string lpParameters;
                [MarshalAs(UnmanagedType.LPStr)]
                public string lpDirectory;
                public int nShow;
                public IntPtr hInstApp;
                public IntPtr lpIDList;
                [MarshalAs(UnmanagedType.LPStr)]
                public string lpClass;
                public IntPtr hkeyClass;
                public uint dwHotKey;
                public IntPtr hIcon;
                public IntPtr hProcess;
            }
    
            public const int SEE_MASK_CLASSNAME = 0x00000001;
    
            public const int SW_SHOWNORMAL = 1;
    
            [DllImport("shell32.dll", CharSet = CharSet.Auto, EntryPoint = "ShellExecuteExA")]
            public static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO pExecInfo)

    thx i work
    Wednesday, April 19, 2017 3:53 PM