How to add Stop Button in Soundboard app using Blend

DaveGx

New member
Nov 5, 2011
756
0
0
Visit site
Im a beginner in developing which is why im using Blend a lot so far. Im creating a soundboard app, have no problems getting buttons to play sounds I want, pretty easy to do. However, some clips are long, and Id like to add one Stop button that will stop any audio that would currently be playing. How do I do that?

Ive searched on line, and there is a pallet Triggers, which apparently isnt in my version of Blend (5) or not for use for Windows Phone apps.

Any help would be appreciated.
 

DaveGx

New member
Nov 5, 2011
756
0
0
Visit site
Here is what I have so far. Ive modified what Im trying to do a bit. I got a code from someone for a sound player. The soundplayer is created as a resource.
Here is my app.xaml and cs<Application:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
x:Class="PhoneApp3.App">

<!--Application Resources-->
<Application.Resources>
<MediaElement x:Name="SoundPlayer" AutoPlay="True" Volume="1" />
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:phoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>


app cs:

namespace PhoneApp3
{
public partial class App : Application
{
public void PlaySound(string SoundPath)
{
// Get sound player
MediaElement SoundPlayer = null;
if (PhoneApp3.App.Current.Resources.Contains("SoundPlayer"))
{
SoundPlayer = PhoneApp3.App.Current.Resources["SoundPlayer"] as MediaElement;
}

if (SoundPlayer == null)
{
return;
}

SoundPlayer.Source = new Uri(SoundPath, UriKind.RelativeOrAbsolute);
SoundPlayer.Position = new TimeSpan(0);
}
public void StopSound()
{
// Get sound player
MediaElement SoundPlayer = null;
if (PhoneApp3.App.Current.Resources.Contains("SoundPlayer"))
{
SoundPlayer = PhoneApp3.App.Current.Resources["SoundPlayer"] as MediaElement;
}

if (SoundPlayer == null)
{
return;
}

SoundPlayer.Stop();
}


Now the Mainpage is where I would have my buttons and whatnot. Here is what I have:


<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,9,0,40"/>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"/>
<Image Source="title1.png" Stretch="Fill"/>
<Image Margin="0,0,0,-17" Source="/WP2.png" Stretch="Fill" Grid.Row="1"/>

<Button Content="Button" HorizontalAlignment="Left" Margin="42,67,0,0" Grid.Row="1" VerticalAlignment="Top" Width="186" Height="101"
Click="PlaySound" Source="run.mp3">

</Button>

</Grid>




So obviously its not working. I dont know how to tie a button and sound to play via the sound player in the resource. Any ideas?
 

Korhaan

New member
Dec 30, 2011
24
0
0
Visit site
I think what you are seeing is due to the sound being initiated via "AutoPlay". Here are two things you can try to see if it fixes it.


1) In your StopSound method, after the SoundPlayer.Stop(); call. Add a new line clearing out the MediaElement's Source. SoundPlayer.Source = null;
2) Alternatively think there may be some issue with the state of the media element when autoplaying. So in your MediaElement XAML, update the AutoPlay attribute and turn it off. AutoPlay="False". Then in your PlaySound method, after the SoundPlayer.Source = new Uri(SoundPath, UriKind.RelativeOrAbsolute); line, add the line SoundPlayer.Play(); to initiate playback.
 

DaveGx

New member
Nov 5, 2011
756
0
0
Visit site
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:phone="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:phoneApplicationService
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:phoneApplicationPage>

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");

}
}

 

Korhaan

New member
Dec 30, 2011
24
0
0
Visit site
Nothing is in your event handler for the button click event, try:

private void Button_Click(object sender, RoutedEventArgs e)
{
PlaySound("Run.mp3");
}
 

Members online

Forum statistics

Threads
323,302
Messages
2,243,601
Members
428,055
Latest member
DrPendragon