【发布时间】:2014-04-12 20:51:51
【问题描述】:
我的目标是在麦克风的振幅超过某个值时暂停当前播放的歌曲。
但是当幅度增加该值时,应用程序会突然退出。
这是为什么呢?
如何解决这个问题?
[我所做的是我在音乐中播放了一首歌,
打开此应用程序并按下按钮并发出超过值的声音。
然后应用突然退出]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using U.Resources;
using Microsoft.Xna.Framework.Audio;
using System.Windows.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
namespace U
{
public partial class MainPage : PhoneApplicationPage
{
//global variables
Microphone microphone = Microphone.Default;
byte[] buffer;
// Constructor
public MainPage()
{
InitializeComponent();
// Timer to simulate the XNA Game Studio game loop (Microphone is from XNA Game Studio)
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(33);
dt.Tick += delegate { try { FrameworkDispatcher.Update(); } catch { } };
dt.Start();
microphone.BufferReady += new EventHandler<EventArgs>(microphone_BufferReady);
}
private void buttonStart_Click(object sender, RoutedEventArgs e)
{
microphone.BufferDuration = TimeSpan.FromMilliseconds(100);
buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
microphone.Start();
}
void microphone_BufferReady(object sender, EventArgs e)
{
microphone.GetData(buffer);
for (int i = 0; i < buffer.Length; i += 2)
{
//The value of sample is the amplitude of the signal
short sample = BitConverter.ToInt16(new byte[2] { buffer[i], buffer[i + 1] }, 0);
//getting the absolut value
if (sample < 0) sample *= (-1);
//showing the output
if(sample>1000) pause_music();
}
}
void pause_music()
{
if (MediaPlayer.State == MediaState.Playing)
{
FrameworkDispatcher.Update();
MediaPlayer.Pause();
}
}
}
}
【问题讨论】:
-
我在 lumia 设备上进行了测试。正如我提到的,它退出了应用程序。我无法在模拟器上调试它,因为我无法在模拟器上播放音乐。还有其他方法可以调试吗?
-
请贴出应用崩溃时遇到的异常
-
在真机而非模拟器上运行时如何查看异常?如果我可以在模拟器上调试它,我很高兴。
-
你是如何部署应用程序的?
-
在 Visual Studio 上。它不会在模拟器上崩溃,因为我没有在上面播放任何音乐(似乎无法在模拟器上播放音乐)
标签: windows windows-phone-7 windows-phone-8 xna microphone