Windows Phone application crashes when pausing the current song -
my goal pausing playing song when amplitude of microphone exceeds value.
apps exit when amplitude increase value.
why that?
how fix this?
[what did played song in music,
opened app , press button , acted sound exceed value.
app exit suddenly]
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 simulate xna game studio game loop (microphone 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); (int = 0; < buffer.length; += 2) { //the value of sample amplitude of signal short sample = bitconverter.toint16(new byte[2] { buffer[i], buffer[i + 1] }, 0); //getting absolut value if (sample < 0) sample *= (-1); //showing output if(sample>1000) pause_music(); } } void pause_music() { if (mediaplayer.state == mediastate.playing) { frameworkdispatcher.update(); mediaplayer.pause(); } } }
}
it crashing because it's going stackoverflow!
you should not call frameworkdispatcher.update()
within pause_music()
method! result in call microphone_bufferready
, pause_music
etc etc , stack overflow served.
just remove line , remember call microphone.stop()
, work :)
Comments
Post a Comment