简体   繁体   中英

Playing sounds in C# WinPhone 8.1 App - Sometimes causes exception

I've been writing a little App that simulates a drumkit on a WinPhone 8.1 using C#. The App runs but frequently when triggering a sound it causes an exception: "An exception of type 'System.Exception' occurred in KiDrums.exe but was not handled in user code

Additional information: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))"

I believe it is caused if I try to trigger a sound either when that same sound or another is still playing. It's hard to tell as I've not been able to determine a pattern. Bellow is some of the code I've used. I have a background image over which several elipses trigger sounds when touched.

XAML:

<Grid>
    <Image x:Name="KiDrums" HorizontalAlignment="Left" Height="620" Margin="10,10,-2,0" VerticalAlignment="Top" Width="392" Source="Assets/KiDrums.jpg" IsDoubleTapEnabled="False" ScrollViewer.VerticalScrollBarVisibility="Disabled" IsHitTestVisible="False" IsTapEnabled="False" ManipulationMode="None"/>
    <Ellipse x:Name="RedDrum" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="196" Margin="142,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="248" Opacity="0" Tapped="RedDrum_Tapped" Holding="RedDrum_Holding" DoubleTapped="RedDrum_DoubleTapped"/>
    <MediaElement x:Name="Snare" Source="Assets/Snare.wav" AutoPlay="False" Visibility="Collapsed"/>
    <MediaElement x:Name="SnareRollLong" Source="Assets/SnareRollLong.wav" AutoPlay="False" Visibility="Collapsed"/>
    <MediaElement x:Name="SnareXtraRoll" Source="Assets/SnareXtraRoll.wav" AutoPlay="False" Visibility="Collapsed"/>

CS:

 private void RedDrum_Tapped(object sender, TappedRoutedEventArgs e)
    {
        // Single Snare Hit : Snare.wav
        Snare.Play();
    }

    private void RedDrum_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
    {
        // Snare Roll : SnareRollLong.wav
        SnareRollLong.Play();
    }

    private void RedDrum_Holding(object sender, HoldingRoutedEventArgs e)
    {
        // Snare Extra Long Roll : SnareXtraRoll.wav
        SnareXtraRoll.Play();
    }

Does anyone know if I should use any extra code to ensure that it plays smoothly? Am I right in assuming that the problem lies in the playing of sounds? Thanks for any helpyou may provide.

If you want to play just an "effect" then you should use XNA Framework SoundEffect class and call Play() method to just play or you can create a SoundEffectInstance to stop and pause it. Like this:

public class SoundEffectHelper : IDisposable
{
    public TimeSpan Duration { get; private set; }
    private SoundEffectInstance soundEffect;

    public SoundEffectHelper(string path)
    {
        using (Stream stream = TitleContainer.OpenStream(path))
        {
            SoundEffect effect = SoundEffect.FromStream(stream);
            this.Duration = effect.Duration;
            this.soundEffect = effect.CreateInstance();
            FrameworkDispatcher.Update();
        }
    }

        public void Play()
    {
        this.soundEffect.Play();
    }

    public void Stop()
    {
        this.soundEffect.Stop(true);
    }

    public void Pause()
    {
        this.soundEffect.Pause();
    }

    public void Resume()
    {
        this.soundEffect.Resume();
    }

    public void Dispose()
    {
        this.soundEffect.Dispose();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM