我正在使用Windows Phone 8尝试通过创建用户控件和媒体元素来实现我的应用中的音频播放功能。
令人惊讶的是,音频在调试期间正在播放,但是当我在没有调试模式的情况下运行应用程序时,音频无法播放,我尝试了await和async方法,但那也没有用。
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();
}
我发布了我的代码,其中&amp;在调试模式下,它没有在运行模式下播放我做错了什么?
答案 0 :(得分:1)
我已成功解决了我所面临的问题,实际上当光标到达audioMediaElement.Play()时会发生什么。 Play()函数需要时间在内部执行,我做了什么我将audioMediaElement.Play()替换为audioMediaElement.AutoPlay = true;它开始工作了。 :) woooo hoooo ...:)