Search code examples
wpfmediaelement

WPF MediaElement, change audio track


I'm writing a Media Player using WPF and the MediaElement. I was playing a video i have that has several audio tracks. But they are both playing at the same time :(

How do i just play one of them ? is it at all possible ?


Solution

  • I think this problem is in the audio and video DirectShow codecs you use. Can you play the video correct in the Windows Media Player? If no, please re-install the appropriated codecs.

    Some months ago I have completely prototyped the requirements to switch audio tracks in order to show the video-help in our multi-language software. My experience shows some alternatives:

    • Use the WMP-control via WinForms host, and then change the language like:

      var control = ((IWMPControls3)wmpControl.Ctlcontrols);
      control.currentAudioLanguage = 9; // english

    The Complete list of the LCID you can find here. To list all audio tracks in the video you can use something like:

    wmpControl.PlayStateChange += (o, args) => {
      if(args.newState == 3) {
        var control = ((IWMPControls3)wmpControl.Ctlcontrols);
        for(int i = 1; i <= control.audioLanguageCount; i++) {
          MessageBox.Show(control.getAudioLanguageID(i) + ": " + control.getAudioLanguageDescription(i));
        }
      }
    };
    
    • Use the separate audio tracks and play it synchronously to the video using the MediaPlayer. The main problem of this method is, that you must synchronize audio and video playback, start, stop, seeking,.. manually.

    I hope this helps you to choose your solution.