سؤال

I'm working on a Windows Form Application in which I have embedded a Windows Media Player that lets you play video files. The unique thing is that I have changed the extension of the media files (for reasons which I can't get into here). For instance, "xyz.wmv" might be called "xyz.ext". They play just fine, but before they play, I get the message:

"The file you are attempting to play has an extension that does not match the file format. Playing the file may result in unexpected behaviour. Do you want the Player to try to play the file?"

You can click yes and you can even check the box to not show that message again, but I don't want that for all the obvious reasons including the fact that it confuses users. I have looked into the .settings properties but I cannot find a way to suppress this message and more importantly other messages that might come up.

هل كانت مفيدة؟

المحلول

The .ext (for example) extension is not known to media player, hence the warning.

What you can do to change this is modify the registry and register this extension. This is described officially here: File Name Extension Registry Settings

The most simple way to do it is to create a registry key like this:

HKEY_CURRENT_USER\Software\Microsoft\MediaPlayer\Player\Extensions\.ext

And add two key values:

Runtime (DWORD): 6
Permissions (DWORD): 15 (or 0xF in hexa)

This is shown here:

enter image description here

نصائح أخرى

NOTE: this answer was originally in response to a bounty question and edit which was removed on how to do this via code. Parts of this are still relevant to the original question.

You can do this pretty straight forward IF you have admin rights as you need to edit the registry. Not sure how far you will get without admin rights and can test later, but here is how to do this via code (in a real implementation I would do this as part of a setup - or check if the keys exist each time which seems wasteful):

You need to add one key (showing two here for registering the extensions, you may need additional keys for auto-play or a setting on the player):

private void Form1_Load(object sender, EventArgs e) {
 /*This first key is not necessary - and if you will be using common 
  * extensions like mp4, skip this step altogether!! 
  */
 RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true)
                                       .OpenSubKey("Classes", true);
 key.CreateSubKey(".myExt");
 key = key.OpenSubKey(".myExt", true);
 key.SetValue("", "WMP11.AssocFile.myExt");
 key.SetValue("Content Type", "video/x-ms-wmv");
 key.SetValue("PerceivedType", "video");

 /*Here is the magic key which will make the dialog go away*/
 key = Registry.CurrentUser.OpenSubKey("Software", true)
                           .OpenSubKey("Microsoft", true)
                           .OpenSubKey("MediaPlayer", true)
                           .OpenSubKey("Player", true)
                           .OpenSubKey("Extensions", true);
 key.CreateSubKey(".myExt");
 key = key.OpenSubKey(".myExt", true);
 key.SetValue("", "");
 key.SetValue("Permissions", 0x20);

 axWindowsMediaPlayer1.URL = @"C:\Users\Public\Documents\Wildlife.myExt";
}

Media Player creates other keys when you add through its dialog, but the only one definitely needed is: HKEY_Current_User.Software.Microsoft.MediaPlayer.Player.Extensions

If you want to see all the keys Media Player adds,

  1. choose a crazy extension,
  2. click always allow when prompted and then
  3. search the registry for all the keys that get created.

The above code is tested and working for me - confirming the dialog before adding the keys and the lack of any dialog after.

This is a good generic process for programmatically adding file associations and default programs to the Windows Registry from .NET. You have to be careful about registering the extension (the first key I set above) IF the extension already exists (TEST FOR THIS). Otherwise the above code will happily overwrite your current values. All you really should need is the one added to: HKEY_Current_User.Software.Microsoft.MediaPlayer.Player.Extensions anyway. Think it through, check in advance, and test before you go crazy in the registry!!

It is also always a great idea to backup your registry before playing with it.

Final note: missed your question on how to reproduce once you have clicked always allow: just remove the entry in HKEY_Current_User.Software.Microsoft.MediaPlayer.Player.Extensions and voila!

This answer assumes you have a working knowledge of regedit.

Final note #2: Response geared to the questions in the Bounty. Other errors can be suppressed by setting telling Media Player to allow you to handle error events and then writing your custom handler. I have not done this before so cannot comment on the ease and what can/cannot be controlled through this method.

The Windows Media Player control does not raise an exception when it encounters an error such as an invalid URL. Instead, it signals an event. Your application should handle error events sent by the Player.

These can then be handled by creating / registering a MediaError event:

    private void axWindowsMediaPlayer1_MediaError(object sender, AxWMPLib._WMPOCXEvents_MediaErrorEvent e) {
           // Handle errors and profit!
    }

Then set this as the handler in the Events property window for your control - same for other events such as ErrorEvent.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top