c# - Slider progress bar with MediaElement Windows Phone 8.1 -
i working within windows phone 8.1 (non silverlight) application. have mediaelement audioplayer
plays audio files. have slider audioplayerseek
want 'hooked' mediaelement, moves audio playing.
i have gone through this tutorial word word, still can't work. when play audio (clicking on item in listview), audio plays, slider doesn't move!
private dispatchertimer _timer; private bool _sliderpressed = false; // constructor public mainpage() { initializecomponent(); navigationcachemode = navigationcachemode.required; audioplayerseek.valuechanged += audioplayerseek_valuechanged; initializeaudiorecording(); datacontext = app.viewmodel; } // called when select audio file in listview private async void selector_onselectionchanged(object sender, selectionchangedeventargs e) { // cast xaml listview var listview = sender listview; // verify have listview if (listview == null) { return; } var listvmitem = listview.selecteditem recordfilevm; if (listvmitem != null) { var file2 = await _finalstoragefolder.getfileasync(listvmitem.filename); var stream = (await file2.openreadasync()).asstream().asrandomaccessstream(); audioplayer.setsource(stream, file2.contenttype); timespan recordingtime = audioplayer.naturalduration.timespan; audioplayerseek.maximum = recordingtime.totalseconds; audioplayerseek.smallchange = 1; audioplayerseek.largechange = math.min(10, recordingtime.seconds / 10); audioplayer.mediaopened += audioplayer_mediaopened; audioplayer.currentstatechanged += audioplayer_currentstatechanged; audioplayer.play(); } // clear selection listview.selecteditem = null; } private double sliderfrequency(timespan timevalue) { double stepfrequency = -1; double absvalue = (int)math.round(timevalue.totalseconds, midpointrounding.awayfromzero); stepfrequency = (int)(math.round(absvalue / 100)); if (timevalue.totalminutes >= 10 && timevalue.totalminutes < 30) { stepfrequency = 10; } else if (timevalue.totalminutes >= 30 && timevalue.totalminutes < 60) { stepfrequency = 30; } else if (timevalue.totalhours >= 1) { stepfrequency = 60; } if (stepfrequency == 0) stepfrequency += 1; if (stepfrequency == 1) { stepfrequency = absvalue / 100; } return stepfrequency; } void audioplayerseek_valuechanged(object sender, rangebasevaluechangedeventargs e) { if (!_sliderpressed) { audioplayer.position = timespan.fromseconds(e.newvalue); } } private void setuptimer() { _timer = new dispatchertimer(); _timer.interval = timespan.fromseconds(audioplayerseek.stepfrequency); starttimer(); } private void _timer_tick(object sender, object e) { if (!_sliderpressed) { audioplayerseek.value = audioplayer.position.totalseconds; } } private void starttimer() { _timer.tick += _timer_tick; _timer.start(); }
i believe relevant code. can please tell me how can synchronize slider moves playing media (audio).
thank you.
you setting things in wrong order. try changing this:
if (listvmitem != null) { var file2 = await _finalstoragefolder.getfileasync(listvmitem.filename); var stream = (await file2.openreadasync()).asstream().asrandomaccessstream(); audioplayer.mediaopened += audioplayer_mediaopened; audioplayer.currentstatechanged += audioplayer_currentstatechanged; audioplayer.setsource(stream, file2.contenttype); audioplayer.play(); }
and move these calls mediaopened handler:
timespan recordingtime = audioplayer.naturalduration.timespan; audioplayerseek.maximum = recordingtime.totalseconds; audioplayerseek.smallchange = 1; audioplayerseek.largechange = math.min(10, recordingtime.seconds / 10);
Comments
Post a Comment