RetSet
RetSet

Reputation: 83

MediaElement plays sound 2 times after changing source

I am working on a Windows Phone 8.1 app, and I am having an issue I cannot work out. I looked up every code on the internet I could find and I've been trying to figure out the source of the problem for hours, but with no success. Let's take a simple code:

xaml:

<Page
    x:Class="App8.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App8"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <Grid>
            <MediaElement x:Name="MediaElement1" AutoPlay="False" Visibility="Collapsed"/>
            <Button Content="Button" HorizontalAlignment="Left" Margin="154,44,0,0" VerticalAlignment="Top"
                    Click="ButtonBase_OnClick"/>
        </Grid>
    </Page>

cs:

using System;
using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace App8
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Disabled;

            MediaElement1.MediaOpened += MediaElement1_MediaOpened;
        }

        void MediaElement1_MediaOpened(object sender, RoutedEventArgs e)
        {
            MediaElement1.Play();
        }


        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            MediaElement1.Source = new Uri("ms-appx:///Assets/Audio/SampleSets/First/clap_01.mp3");
        }
    }
}

Now, when I am clicking the button, then from time to time the sound is being played twice. Sometimes is after a first click, sometimes after 10 of them. I just want to emphasize that this is just simple code to show what my problem is and I do not really want to change the Source of the MediaElement to the same again and again.

Upvotes: 2

Views: 697

Answers (1)

Romasz
Romasz

Reputation: 29792

As I've tried the problem seems to occur (sometimes) while running on Emulator.

While I was tesing it on a device, I couldn't reproduce the same behaviour.

Upvotes: 2

Related Questions