locked
[W8.1]MediaElement's KeyUp/KeyDown events RRS feed

  • Question

  • Hi all,

    I try to change Volume property of MediaElement through KeyUp and KeyDown events, but in both cases Volume becomes lower and returns back to default value 0.5 (slider from default controls shows it).

    private void Player_KeyUp(object sender, KeyRoutedEventArgs e)
            {
                Player.Volume = Player.Volume + 0.1;
            }
    
            private void Player_KeyDown(object sender, KeyRoutedEventArgs e)
            {
                Player.Volume = Player.Volume - 0.1;
            }


    • Edited by Andev Wednesday, November 4, 2015 8:34 AM
    Tuesday, October 27, 2015 11:08 AM

Answers

  • Hi Andev,

    >>but in both cases Volume becomes lower and returns back to default value 0.5 (slider from default controls shows it).

    I think it is a correct behaviour, because when we click the Key, first it will fire the KeyDown event, then the Volume will become 0.4, after it will fire the KeyUp event, the Volume will become 0.5, so the Volume becomes lower and returns back to default value 0.5. If I press the the Key for some times, you will found the Volumn will decrease for some times, because it will fire the KeyDown event for many times. Once I relase the key, it will fire the KeyUp event and the Volumn will add 0.1.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, November 5, 2015 8:47 AM

All replies

  • Try:

    private void Player_KeyUp(object sender, KeyRoutedEventArgs e)
    {
        SetValue(Player.VolumeProperty, (object)(Player.Volume + 0.1));
    }
    
    private void Player_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        SetValue(Player.VolumeProperty, (object)(Player.Volume - 0.1));
    }

    That should set the volume property directly and raise any events that should be raised which will, hopefully, eliminate the phantom return to previous value issue that you are experiencing. In theory setting the volume directly as you were trying should work correctly, but since it isn't, this may provide a workaround.


    Visual C++ MVP | http://bit.ly/bobtaco (website) | http://bit.ly/mikebmcl (Twitter: @mikebmcl)

    Wednesday, October 28, 2015 2:55 AM
  • Hello,

    VS shows error: "Member 'MediaElement.VolumeProperty' cannot be accessed with an instance reference; qualify it with a type name instead."

    If I change "Player" to "MediaElement", code is correct but volume is not changed when pressing keyup/keydown.

    Wednesday, October 28, 2015 10:38 AM
  • Hi Andev,

    Welcome to the Developing Universal Windows apps forum!

    As a friendly reminder please make sure to add the appropriate tags to the title of your post as per Guide to posting: subject line tags

    In order to change the Volume of the MediaElement through KeyUp and KeyDown events, we need to handle the TransportControls's KeyUp and KeyDown event as following:

    public MainPage()
            {
                this.InitializeComponent();
                Player.TransportControls.KeyDown += TransportControls_KeyDown;
                Player.TransportControls.KeyUp += TransportControls_KeyUp;
            }
    
            private void TransportControls_KeyUp(object sender, KeyRoutedEventArgs e)
            {
                Player.Volume = Player.Volume + 0.1;
    
            }
    
            private void TransportControls_KeyDown(object sender, KeyRoutedEventArgs e)
            {
                Player.Volume = Player.Volume - 0.1;
    
            }

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Tuesday, November 3, 2015 7:57 AM
  • I am sorry, I forgot to specify what type is W8.1, there is no such property TransportControls
    Wednesday, November 4, 2015 8:36 AM
  • Hi Andev,

    >>but in both cases Volume becomes lower and returns back to default value 0.5 (slider from default controls shows it).

    I think it is a correct behaviour, because when we click the Key, first it will fire the KeyDown event, then the Volume will become 0.4, after it will fire the KeyUp event, the Volume will become 0.5, so the Volume becomes lower and returns back to default value 0.5. If I press the the Key for some times, you will found the Volumn will decrease for some times, because it will fire the KeyDown event for many times. Once I relase the key, it will fire the KeyUp event and the Volumn will add 0.1.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, November 5, 2015 8:47 AM