Ive tried it with Autoplay false as well. The issue is, it doesnt play the media when I press the button, so no sound at all. Here is an updated code
App.xaml
<Application
x:Class="PhoneApp3.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns
hone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
<!--Application Resources-->
<Application.Resources>
<MediaElement x:Name="SoundPlayer" AutoPlay="True" MediaEnded="SoundPlayer_MediaEnded" MediaFailed="SoundPlayer_MediaFailed" Volume="1" />
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell
honeApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>
App.xaml.cs
using System;
using System.Diagnostics;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp8.Resources;
namespace PhoneApp3
{
public partial class App : Application
{
public void PlaySound(string SoundPath)
{
// Get sound player
MediaElement SoundPlayer = null;
if (PhoneApp8.App.Current.Resources.Contains("SoundPlayer"))
{
SoundPlayer = PhoneApp8.App.Current.Resources["SoundPlayer"] as MediaElement;
}
if (SoundPlayer == null)
{
return;
}
SoundPlayer.Source = new Uri(SoundPath, UriKind.RelativeOrAbsolute);
SoundPlayer.Position = new TimeSpan(0);
}
Mainpage.xaml
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
</Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="42,67,0,0" Grid.Row="1" VerticalAlignment="Top" Width="186" Height="101"
Click="Button_Click">
</Button>
</Grid>
</phone
honeApplicationPage>
Mainpage.xaml.cs
namespace PhoneApp3
{
public partial class MainPage : PhoneApplicationPage
{
public void PlaySound(string SoundPath)
{ }
public MainPage()
{
InitializeComponent();}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
private void PlaySound(object sender, RoutedEventArgs e)
{
PlaySound("Run.mp3");
}
}