locked
Why is MediaElement so difficult to use RRS feed

  • Question

  • I have a mediaelement and while I can play an MP3 file, in no way can I get the mediaelement.volume or mediaelement.islooping to work

    see code sample below, I also tried having the mediaelement direct in XAML still volume looping not working ??

       public sealed partial class MainPage : Page
        {
            private MediaElement mp_bells;
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                mp_bells = new MediaElement();
                layout_root.Children.Add(mp_bells);
                InitBells();
            }
    
    
    
            private void bells_Click(object sender, RoutedEventArgs e)
            {
                mp_bells.Volume = 0.3d;
                mp_bells.IsLooping = true;
                mp_bells.Play();
            }
    
    
    
    
    
            public async void InitBells()
            {
                var package = Windows.ApplicationModel.Package.Current;
                var installedLocation = package.InstalledLocation;
                var storageFile = await installedLocation.GetFileAsync("sounds\\bells.mp3");
                if (storageFile != null)
                {
                    var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
                    mp_bells = new MediaElement();
                    mp_bells.AutoPlay = false;
                    mp_bells.RealTimePlayback = true;
                    mp_bells.SetSource(stream, storageFile.ContentType);
                }
            }
    
        }    
      
    }


    rich

    Tuesday, March 12, 2013 4:08 PM

Answers

  • ok solved it appears you have to set the volume and islooping before you set the stream

    i.e.

           media.mp_bells.Volume = 0.5d;
                    media.mp_bells.IsLooping = true;
                    media.mp_bells.SetSource(stream, storageFile.ContentType);

    trying to set it via a button click for example

          private void Button_Click_1(object sender, RoutedEventArgs e)
          {
                media.mp_bells.Volume = 0.3d;
                media.mp_bells.IsLooping = true;
                media.mp_bells.Play();
            
          }

    does not work.


    rich

    • Marked as answer by dayjur Friday, March 15, 2013 2:27 PM
    Wednesday, March 13, 2013 9:32 PM

All replies

  • I wonder if setting RealTimePlayback = true is somehow interfering with the looping and volume? Based on the scenario you posted above, I don't think you need to set that property. I believe it is mainly used when live streaming content like a video conference.

    Tuesday, March 12, 2013 4:29 PM
  • No your right, it does not need to be set I just set it to true to experiment see if it did work, leaving it out or in has no impact on volume adjustment and looping.

    rich


    • Edited by dayjur Tuesday, March 12, 2013 5:45 PM
    Tuesday, March 12, 2013 5:43 PM
  • HI Dayjur,

    instead of using media element control, if you use microsoft media player frame work then you will see,

    there is no need to add any code for media volume or seek bar etc. feature. only you need is to set the media source to the media player

    following link wiil help u more :

    Microsoft Media Player Framework

    Best Regards,

    Atul





    Wednesday, March 13, 2013 6:53 AM
  • Can you add the
    new MediaElement();
    inside the XAML? And then try again?

    Sara Silva - Microsoft Visual C# MVP
    My blog | My Windows 8 Store Apps Samples | More Samples
    Follow me in Twitter @saramgsilva

    My Windows 8 Store Apps: Female Pill | Galinho (Tic tac Toe) | 24 | My Snake
    My Windows Phone Apps

    Wednesday, March 13, 2013 8:42 AM
  • I tried it in XAML even setting volume and islooping in XAML still does not work, regardless however you create the mediaelement these functions should work

    just as .play / .setsource / .autoplay  works, I notice that the MedieElement sample from MSFT does not contain function for volume or loop as well

    would someone from MSFT confirm if these functions acutually work and if so how ?

    My alternative is to use SharpDX and .WAVS which will allow me to play simul dynamically control vol etc ..

    but the problem is even at 11000 kHz quality they take up 2MB per sound


    rich


    • Edited by dayjur Wednesday, March 13, 2013 2:32 PM
    Wednesday, March 13, 2013 2:28 PM
  • Can you share a simple repro project on SkyDrive? I can try to have a look at it today.
    Wednesday, March 13, 2013 4:08 PM
  • ok solved it appears you have to set the volume and islooping before you set the stream

    i.e.

           media.mp_bells.Volume = 0.5d;
                    media.mp_bells.IsLooping = true;
                    media.mp_bells.SetSource(stream, storageFile.ContentType);

    trying to set it via a button click for example

          private void Button_Click_1(object sender, RoutedEventArgs e)
          {
                media.mp_bells.Volume = 0.3d;
                media.mp_bells.IsLooping = true;
                media.mp_bells.Play();
            
          }

    does not work.


    rich

    • Marked as answer by dayjur Friday, March 15, 2013 2:27 PM
    Wednesday, March 13, 2013 9:32 PM