MediaElement仅在调试模式下播放音频,而在调试模式下不播放音频 [英] MediaElement is playing audio only in debug mode, its not playing audio without debug mode
问题描述
我正在Windows Phone 8上尝试通过创建用户控件和媒体元素在我的应用程序中实现音频播放功能.
I am working on windows phone 8 trying to implement the audio playing feature in my app by creating the user control and media element.
令人惊讶的是,音频在调试过程中正在播放,但是由于我在没有调试模式的情况下运行该应用程序而无法播放音频,因此我尝试了等待和异步方法,但这也无法正常工作.
Surprisingly audio is playing during debugging but as i run the app without debug mode audio does not play, i tried the await and async methodology but thats also not working.
MainPage.xaml.cs
private async void AudioControl(string audioId, string audio_url)
{
customControlAudioPlayer.media_Id = audioId;
customAudioPlayer.media_Url = audio_url;
//Following function is dedicated to show control and play the audio.
await customAudioPlayer.RenderView();
gridContainsMediaElement.Visibility = Visibility.Visible;
}
MainPage.xaml
xmlns:MyControls="clr-namespace:dbok.Controls"
<Grid Name="gridContainsMediaElement"
VerticalAlignment="Top"
Background="Black" Visibility="Collapsed">
<MyControls:AudioPlayerControl
x:Name="customControlAudioPlayer"/>
<!--CloseClick="audioPlayerControl_CloseClick"-->
</Grid>
CustomControlPlayer.xaml
<Grid Background="{StaticResource PhoneChromeBrush}" >
<MediaElement x:Name="audioMediaElement" AutoPlay="False"
Grid.Row="0"
MediaOpened="audioMediaElement_MediaOpened"
MediaEnded="audioMediaElement_MediaEnded"
MediaFailed="audioMediaElement_MediaFailed"
HorizontalAlignment="Center" Height="05"
VerticalAlignment="Center"/>
<Slider Visibility="Visible" Margin="50,0,50,0"
x:Name="mediaPositionSlider" Background="#f1592a"
Minimum="0" Grid.Row="0"
/>
</StackPanel>
</Grid>
CustomControlPlayer.xaml.cs
public string media_Id;
public string media_Url;
public async Task RenderView()
{
Uri file_url;
if (Uri.TryCreate(media_Url, UriKind.Absolute, out file_url))
{
myElement.Source = file_url;
myElement.Play();
//I tried by check the media element state but its executing and playing only in debug mode. it was not working for me without debug i made it commented.
//if (myElement.CurrentState != MediaElementState.Playing)
//{
// myElement.Play();
//}
mediaPositionSlider.Value = 0;
UpdateDuration(0);
}
//-----------
private void UpdateDuration(double _myValue)
{
TimeSpan t = TimeSpan.FromSeconds(_myValue);
string time_slot = string.Format("{0:D2}:{1:D2}:{2:D2}",
t.Hours,
t.Minutes,
t.Seconds);
PlayStatusLabel.Text = time_slot.ToString();
}
我已经发布了我的代码,其中&我做错了什么,它不在调试模式下在运行模式下播放?
I have post my code, where & what i am doing wrong by which its not playing in run mode inplace of debug mode ?
推荐答案
我已经成功解决了我所面临的问题,实际上当光标到达audioMediaElement.Play()时会发生什么? Play()函数需要内部执行的时间,我将audioMediaElement.Play()替换为audioMediaElement.AutoPlay = true;它开始工作. :) woooo hoooo ...:)
I have successfully solved the issue i was facing, actually what happening when cursor reach on audioMediaElement.Play(); the Play() function takes time to execute internally, what i did i replace audioMediaElement.Play() to audioMediaElement.AutoPlay = true; and it started working. :) woooo hoooo... :)
这篇关于MediaElement仅在调试模式下播放音频,而在调试模式下不播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!