Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 574
  • Last Modified:

How to play then stop a sound effect in windows phone dev

Tap the screen > play a sound effect.
Tap the screen again > stop the sound effect if it's still playing.

Sounds simple, right? Well, tapping the screen plays the sound effect each time no matter what. So, the end result is the same sound effect playing simultaneously.

I pieced this together and think i know what the problem is: Each time the Instance is Created, the SoundState defaults to Stop.

    Protected Overrides Sub OnManipulationStarted(args As ManipulationStartedEventArgs)
        PlaySound()
    End Sub

    Private Sub PlaySound()

        Dim sei As SoundEffectInstance
        Dim stream As IO.Stream = TitleContainer.OpenStream("Sounds/error.wav")
        Dim effect As SoundEffect = SoundEffect.FromStream(stream)
        sei = effect.CreateInstance()
        FrameworkDispatcher.Update()

        If sei.State = SoundState.Playing Then
            sei.Stop()
        Else
            sei.Play()
        End If

    End Sub

Open in new window


Any ideas on how to acheive the desired effect as descibed in the begging of this post?
Jason
0
jsvb1977
Asked:
jsvb1977
  • 7
  • 5
1 Solution
 
Kalpesh ChhatralaSoftware ConsultantCommented:
Seperate this PlaySound Function in Seprate Public Class and create object in Application Startup and then access this PlaySound() Method.
0
 
jsvb1977Author Commented:
Im not sure i know what you mean? Aren't I already doing this? Here is the complete code:

Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio

Partial Public Class MainPage
    Inherits PhoneApplicationPage
    ' Constructor
    Public Sub New()
        InitializeComponent()
    End Sub

    Protected Overrides Sub OnManipulationStarted(args As ManipulationStartedEventArgs)
        PlaySound()
    End Sub

    Private Sub PlaySound()

        Dim sei As SoundEffectInstance
        Dim stream As IO.Stream = TitleContainer.OpenStream("Sounds/error.wav")
        Dim effect As SoundEffect = SoundEffect.FromStream(stream)
        sei = effect.CreateInstance()
        FrameworkDispatcher.Update()

        If sei.State = SoundState.Playing Then
            sei.Stop()
        Else
            sei.Play()
        End If

    End Sub

End Class

Open in new window

0
 
Kalpesh ChhatralaSoftware ConsultantCommented:
Not in MainPage before that there is Main Function where MainPage is Called.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jsvb1977Author Commented:
Sorry. I don't see it. I checked App.xaml.vb, but no joy.
Where do you suspect the Main Function is?

I checked the My Project folder, but did not see anything in there either.

-Jason
0
 
Kalpesh ChhatralaSoftware ConsultantCommented:
are you working with Silverlight ?

in App.Xaml there is Application_Startup Method.

there is you can create one public object of your method and access from MainPage.
0
 
jsvb1977Author Commented:
This is what my app.xaml.vb looks like. I do not see an application_startup Method:

Partial Public Class App
    Inherits Application

    ''' <summary>
    ''' Provides easy access to the root frame of the Phone Application.
    ''' </summary>
    ''' <returns>The root frame of the Phone Application.</returns>
    Public Property RootFrame As PhoneApplicationFrame

    ''' <summary>
    ''' Constructor for the Application object.
    ''' </summary>
    Public Sub New()
        ' Standard Silverlight initialization
        InitializeComponent()

        ' Phone-specific initialization
        InitializePhoneApplication()

        ' Show graphics profiling information while debugging.
        If System.Diagnostics.Debugger.IsAttached Then
            ' Display the current frame rate counters.
            Application.Current.Host.Settings.EnableFrameRateCounter = True

            ' Show the areas of the app that are being redrawn in each frame.
            'Application.Current.Host.Settings.EnableRedrawRegions = True

            ' Enable non-production analysis visualization mode, 
            ' which shows areas of a page that are handed off to GPU with a colored overlay.
            'Application.Current.Host.Settings.EnableCacheVisualization = True


            ' Disable the application idle detection by setting the UserIdleDetectionMode property of the
            ' application's PhoneApplicationService object to Disabled.
            ' Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
            ' and consume battery power when the user is not using the phone.
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled
        End If

    End Sub

    ' Code to execute when the application is launching (eg, from Start)
    ' This code will not execute when the application is reactivated
    Private Sub Application_Launching(ByVal sender As Object, ByVal e As LaunchingEventArgs)
    End Sub

    ' Code to execute when the application is activated (brought to foreground)
    ' This code will not execute when the application is first launched
    Private Sub Application_Activated(ByVal sender As Object, ByVal e As ActivatedEventArgs)
    End Sub

    ' Code to execute when the application is deactivated (sent to background)
    ' This code will not execute when the application is closing
    Private Sub Application_Deactivated(ByVal sender As Object, ByVal e As DeactivatedEventArgs)
    End Sub

    ' Code to execute when the application is closing (eg, user hit Back)
    ' This code will not execute when the application is deactivated
    Private Sub Application_Closing(ByVal sender As Object, ByVal e As ClosingEventArgs)
    End Sub

    ' Code to execute if a navigation fails
    Private Sub RootFrame_NavigationFailed(ByVal sender As Object, ByVal e As NavigationFailedEventArgs)
        If Diagnostics.Debugger.IsAttached Then
            ' A navigation has failed; break into the debugger
            Diagnostics.Debugger.Break()
        End If
    End Sub

    Public Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs) Handles Me.UnhandledException

        ' Show graphics profiling information while debugging.
        If Diagnostics.Debugger.IsAttached Then
            Diagnostics.Debugger.Break()
        Else
            e.Handled = True
            MessageBox.Show(e.ExceptionObject.Message & Environment.NewLine & e.ExceptionObject.StackTrace,
                            "Error", MessageBoxButton.OK)
        End If
    End Sub

#Region "Phone application initialization"
    ' Avoid double-initialization
    Private phoneApplicationInitialized As Boolean = False

    ' Do not add any additional code to this method
    Private Sub InitializePhoneApplication()
        If phoneApplicationInitialized Then
            Return
        End If

        ' Create the frame but don't set it as RootVisual yet; this allows the splash
        ' screen to remain active until the application is ready to render.
        RootFrame = New PhoneApplicationFrame()
        AddHandler RootFrame.Navigated, AddressOf CompleteInitializePhoneApplication

        ' Handle navigation failures
        AddHandler RootFrame.NavigationFailed, AddressOf RootFrame_NavigationFailed

        ' Ensure we don't initialize again
        phoneApplicationInitialized = True
    End Sub

    ' Do not add any additional code to this method
    Private Sub CompleteInitializePhoneApplication(ByVal sender As Object, ByVal e As NavigationEventArgs)
        ' Set the root visual to allow the application to render
        If RootVisual IsNot RootFrame Then
            RootVisual = RootFrame
        End If

        ' Remove this handler since it is no longer needed
        RemoveHandler RootFrame.Navigated, AddressOf CompleteInitializePhoneApplication
    End Sub
#End Region

End Class

Open in new window

0
 
jsvb1977Author Commented:
OK, so i had to create the Startup Sub as show here:

    Private Sub App_Startup(sender As Object, e As System.Windows.StartupEventArgs) Handles MyBase.Startup
        Dim stream As IO.Stream = TitleContainer.OpenStream("Sounds/error.wav")
        Dim effect As SoundEffect = SoundEffect.FromStream(stream)
    End Sub

Open in new window


Can you help me with the implimentation now that I have my bearings straight?
In a previous post you said:


Seperate this PlaySound Function in Seprate Public Class and create object in Application Startup and then access this PlaySound() Method.

Can you elaborate a little more on this so as to nudge me in the right direction?
Specifically the  "create object in Application Startup " part of your solution.

I am unsure of how to access this once it has been created in App.xaml.vb

Jason
0
 
Kalpesh ChhatralaSoftware ConsultantCommented:
each and every time when you tap screen Application is Running or again New Application Start ?
0
 
jsvb1977Author Commented:
I will debug and check it and then get back to you.
I dont think it restarts the application. If it does, i will likely rethink how I am designing this app.
I ASSUMED that the tap will only run that peice of code WITHOUT restarting the app.

Jason
0
 
jsvb1977Author Commented:
The answer is:

When the screen is tapped, the app remains running.
I placed a breakpoint on Application Startup and the code only stopped on that breakpoint when I first run the application. After I tap the screen, the code did not stop on that breakpoint in App.xaml.vb.

Jason
0
 
Kalpesh ChhatralaSoftware ConsultantCommented:
can you send me your whole project sample ?
0
 
jsvb1977Author Commented:
Here it is...

hutmonkey.com/RandomSoundTest.zip


Let me know if you have any questions or concerns.
Jason
0
 
hjgodeCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now