[英]How to run C# code from WinPhone 8.1 Universal app when device connect to internet
[英]Playing sounds in C# WinPhone 8.1 App - Sometimes causes exception
我一直在编写一个小应用程序,它使用C#在WinPhone 8.1上模拟架子鼓。 该应用程序运行但在触发声音时经常会导致异常:“ KiDrums.exe中发生了'System.Exception'类型的异常,但未在用户代码中处理
附加信息:灾难性故障(HRESULT的异常:0x8000FFFF(E_UNEXPECTED))
我相信是由于当我尝试在同一声音或另一声音仍在播放时触发声音而引起的。 很难说,因为我无法确定模式。 波纹管是我使用的一些代码。 我有一个背景图片,触摸时有几个椭圆在上面触发声音。
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();
}
有谁知道我是否应该使用任何额外的代码来确保其流畅运行? 我是否可以假设问题出在声音的演奏上,对吗? 感谢您提供的任何帮助。
如果只想播放“效果”,则应使用XNA Framework SoundEffect
类并调用Play()方法来播放,或者可以创建SoundEffectInstance
来停止和暂停它。 像这样:
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.