xaml - Silverlight - Adding MediaElement via C# - short time to manage Play() method -
i made simple app in windows phone silverlight 8.1. looks cell phone keypad lcd-looks screen shows number of current tapped button , plays tone sound of number.
i've been searching while found nothing specific.
i have problem play(); method because execute while i'm debugging in realtime after app-deploy device hear nothing...
i researched many topics , can tell depends on
mediaelementobject.currentstate;
sometimes it's on "closed" state , on "opening"(and hear sound while debugging).
c#
private void keypadbuttonclick(object sender, routedeventargs e) { button currentbutton = sender button; mediaelement keysound = null; if (currentbutton != null) { string buttoncontentvalue = currentbutton.content.tostring(); keysound = new mediaelement(); contentpanel.children.add(keysound); playtargetkeypadsound(buttoncontentvalue, keysound); refreshnumber(buttoncontentvalue); } if (keysound != null) { contentpanel.children.remove(keysound); } } private void playtargetkeypadsound(string buttoncontentvalue, mediaelement keysound) { string path = "/assets/sounds/keynumber_" + buttoncontentvalue + ".wav"; keysound.currentstatechanged += keysoundoncurrentstatechanged; keysound.source = new uri(path, urikind.relativeorabsolute); keysound.volume = 1.0; keysound.autoplay = false; } private void keysoundoncurrentstatechanged(object sender, routedeventargs routedeventargs) { mediaelement sound = sender mediaelement; if (sound != null) sound.play(); } private void refreshnumber(string buttoncontentvalue) { numbertextblock.text = buttoncontentvalue; }
xaml
<grid x:name="contentpanel" grid.row="2" margin="8,0,8,12"> <grid.rowdefinitions> <rowdefinition height="*" /> <rowdefinition height="*" /> <rowdefinition height="*" /> <rowdefinition height="*" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="*" /> <columndefinition width="*" /> </grid.columndefinitions> <!-- first row --> <button click="keypadbuttonclick" content="1" style="{staticresource keypadbuttonstyle}" /> <button click="keypadbuttonclick" content="2" style="{staticresource keypadbuttonstyle}" grid.column="1" /> <button click="keypadbuttonclick" content="3" style="{staticresource keypadbuttonstyle}" grid.column="2" /> <!-- second row --> <button click="keypadbuttonclick" content="4" style="{staticresource keypadbuttonstyle}" grid.row="1" /> <button click="keypadbuttonclick" content="5" style="{staticresource keypadbuttonstyle}" grid.row="1" grid.column="1" /> <button click="keypadbuttonclick" content="6" style="{staticresource keypadbuttonstyle}" grid.row="1" grid.column="2" /> <!-- third row --> <button click="keypadbuttonclick" content="7" style="{staticresource keypadbuttonstyle}" grid.row="2" /> <button click="keypadbuttonclick" content="8" style="{staticresource keypadbuttonstyle}" grid.row="2" grid.column="1" /> <button click="keypadbuttonclick" content="9" style="{staticresource keypadbuttonstyle}" grid.row="2" grid.column="2" /> <!-- fourth row --> <button click="keypadbuttonclick" content="*" style="{staticresource keypadbuttonstyle}" grid.row="3" /> <button click="keypadbuttonclick" content="0" style="{staticresource keypadbuttonstyle}" grid.row="3" grid.column="1" /> <button click="keypadbuttonclick" content="#" style="{staticresource keypadbuttonstyle}" grid.row="3" grid.column="2" /> </grid>
many helping
ok.
i solved it...
c#
private void keypadbuttonclick(object sender, routedeventargs e) { button currentbutton = sender button; if (currentbutton != null) { string buttoncontentvalue = currentbutton.content.tostring(); playtargetkeypadsound(buttoncontentvalue); refreshnumber(buttoncontentvalue); } } private void playtargetkeypadsound(string buttoncontentvalue) { string path = "/assets/sounds/keynumber_" + buttoncontentvalue + ".wav"; keysoundmediaelement.mediaopened += keysoundmediaelementonmediaopened; keysoundmediaelement.source = new uri(path, urikind.relativeorabsolute); } private void keysoundmediaelementonmediaopened(object sender, routedeventargs routedeventargs) { mediaelement sound = sender mediaelement; if (sound != null) sound.play(); } private void refreshnumber(string buttoncontentvalue) { numbertextblock.text = buttoncontentvalue; }
xaml
<!-- fourth row --> <button click="keypadbuttonclick" content="*" style="{staticresource keypadbuttonstyle}" grid.row="3" /> <button click="keypadbuttonclick" content="0" style="{staticresource keypadbuttonstyle}" grid.row="3" grid.column="1" /> <button click="keypadbuttonclick" content="#" style="{staticresource keypadbuttonstyle}" grid.row="3" grid.column="2" /> <mediaelement x:name="keysoundmediaelement" volume="1" autoplay="false" />
Comments
Post a Comment