Surface Pro 7 deal! Save big at Amazon right now
- 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.11-07-2013 12:14 PMLike 0 - 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?11-08-2013 08:30 AMLike 0 - 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.11-08-2013 01:30 PMLike 0 - 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");
}
}
11-09-2013 01:46 PMLike 0
- Forum
- Developers
- Developers Corner
How to add Stop Button in Soundboard app using Blend
« How to resolve the host / machine name with an IP address on windows phone
|
Bug or Something Wrong With My Developer Preview GDR3 Update? »
Similar Threads
-
No rotation lock in my Nokia Lumia 520?
By Kerry Andrews in forum Windows PhonesReplies: 5Last Post: 11-12-2013, 11:37 AM -
NFC in 8x
By shilpicin in forum Windows PhonesReplies: 2Last Post: 11-12-2013, 10:37 AM -
Apps/Games downloaded over WiFI
By amitmehra057 in forum Windows PhonesReplies: 3Last Post: 11-08-2013, 10:45 AM -
Flashing GDR 3 to at&t locked 1020? - Is it possible?
By montsa007 in forum Windows PhonesReplies: 6Last Post: 11-07-2013, 10:08 AM -
Tiny Death Star (nimblebit) in Marketplace
By uopjo6 in forum General Phone DiscussionReplies: 1Last Post: 11-07-2013, 08:19 AM
LINK TO POST COPIED TO CLIPBOARD