Playing a sound from a generated buffer in a Windows phone app -
Playing a sound from a generated buffer in a Windows phone app -
i new in windows phone sdk. can't find illustration of playing sound generated buffer in windows phone app. help pls.
i found example:
byte] buffer = new byte[44100 * 2 * 5]; float t = 0; (int = 0; < 44100 * 2 * 5; += 2) { short val = (short)(math.sin(t * 2 * math.pi * 440) * short.maxvalue); buffer[i] = (byte)(val & 0xff); buffer[i + 1] = (byte)(val >> 8); t += 1 / 44100.0f; } sf = new soundeffect(buffer, 44100, audiochannels.mono); // play. sf.play();
but it's crash error first chance exception of type 'system.invalidoperationexception' occurred in microsoft.xna.framework.ni.dll exception of type 'system.invalidoperationexception' occurred in microsoft.xna.framework.ni.dll not handled in user code
you need phone call frameworkdispatcher.update
.
(see exception: frameworkdispatcher.update has not been called. regular frameworkdispatcher.update calls necessary fire , forget sound effects , framework events function correctly. see http://go.microsoft.com/fwlink/?linkid=193853 details.)
set timer in constructor:
var dt = new dispatchertimer(); dt.interval = timespan.frommilliseconds(33); dt.tick += new eventhandler(tick); dt.start();
and tick event handler:
void tick(object sender, eventargs e) { seek { frameworkdispatcher.update(); } grab (exception ex) { if (debugger.isattached) { debugger.break(); } } }
should work illustration play button in app. have added 1 line code: var soundinstance = sf.createinstance();
private void appbarplaybutton_click(object sender, eventargs e) { byte[] buffer = new byte[44100 * 2 * 5]; float t = 0; (int = 0; < 44100 * 2 * 5; += 2) { short val = (short)(math.sin(t * 2 * math.pi * 440) * short.maxvalue); buffer[i] = (byte)(val & 0xff); buffer[i + 1] = (byte)(val >> 8); t += 1 / 44100.0f; } var sf = new soundeffect(buffer, 44100, audiochannels.mono); var soundinstance = sf.createinstance(); // play. sf.play(); }
windows windows-phone-8 sdk
Comments
Post a Comment