1. DaveGx's Avatar
    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 11:14 AM
  2. DaveGx's Avatar
    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 07:30 AM
  3. Korhaan's Avatar
    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 12:30 PM
  4. DaveGx's Avatar
    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 12:46 PM
  5. Korhaan's Avatar
    Nothing is in your event handler for the button click event, try:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
    PlaySound("Run.mp3");
    }
    11-09-2013 02:22 PM

Similar Threads

  1. No rotation lock in my Nokia Lumia 520?
    By Kerry Andrews in forum Nokia Lumia 520
    Replies: 5
    Last Post: 11-12-2013, 10:37 AM
  2. NFC in 8x
    By shilpicin in forum HTC 8X
    Replies: 2
    Last Post: 11-12-2013, 09:37 AM
  3. Apps/Games downloaded over WiFI
    By amitmehra057 in forum Nokia Lumia 620
    Replies: 3
    Last Post: 11-08-2013, 09:45 AM
  4. Flashing GDR 3 to at&t locked 1020? - Is it possible?
    By montsa007 in forum Nokia Lumia 1020
    Replies: 6
    Last Post: 11-07-2013, 09:08 AM
  5. Tiny Death Star (nimblebit) in Marketplace
    By uopjo6 in forum Applications
    Replies: 1
    Last Post: 11-07-2013, 07:19 AM
LINK TO POST COPIED TO CLIPBOARD