c# - 使用网络服务获取随机视频并播放。C# WPF
问题描述
我需要帮助弄清楚从哪里开始使用网络服务来播放随机视频。这是我正在处理的程序指令:
对于这个问题,您将使用 Web 服务来获取要播放的随机视频。可以在以下位置找到 Web 服务:http: //pcbstuou.w27.wh-2.com/webservices/3033/api/random/video。您将需要一个按钮来从 web 服务中获取随机视频供用户按下,以及一个播放和停止按钮。播放视频时,播放按钮应兼作暂停按钮,并应更改文本以反映可用选项。
这就是我所拥有的:
namespace Problem3
{
public partial class Media
{
private bool mediaPlayerIsPlaying = false;
public mePlayer;
private void Open_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Media files (*.mp3;*.mpg;*.mpeg)|*.mp3;*.mpg;*.mpeg|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
mePlayer.Source = new Uri(openFileDialog.FileName);
}
private void Play_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = (mePlayer != null) && (mePlayer.Source != null);
}
private void Play_Executed(object sender, ExecutedRoutedEventArgs e)
{
mePlayer.Play();
mediaPlayerIsPlaying = true;
}
private void Stop_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = mediaPlayerIsPlaying;
}
private void Stop_Executed(object sender, ExecutedRoutedEventArgs e)
{
mePlayer.Stop();
mediaPlayerIsPlaying = false;
}
meplayer 出错了,我显然需要为它创建一个方法,但是该方法包括什么?另外,我不确定我是否这样做对?
这是主要的:
namespace Problem3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Search_button_Click(object sender, RoutedEventArgs e, object mediaElement)
{
//var FullVimeoUrl = "http://pcbstuou.w27.wh-2.com/webservices/3033/api/random/video ";
//mediaElement.Source = new Uri(FullVimeoUrl.ToString(), UriKind.RelativeOrAbsolute);
//mediaElement.Play();
}
private void Search_button_Click(object sender, RoutedEventArgs e)
{
var FullVimeoUrl = "http://pcbstuou.w27.wh-2.com/webservices/3033/api/random/video ";
mediaElement.Source = new Uri(FullVimeoUrl.ToString(), UriKind.RelativeOrAbsolute);
mediaElement.Play();
}
public void VideoPath(object sender, RoutedEventArgs e)
{
}
private class mediaElement
{
public static Uri Source { get; internal set; }
}
//public string Play()
//{
//}
private void Play_Click(object sender, RoutedEventArgs e)
{
//void OnMouseDownPauseMedia(object sender, MouseButtonEventArgs args)
//{
// // The Pause method pauses the media if it is currently running.
// // The Play method can be used to resume.
// mediaElement.Pause();
//}
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
}
}
我确信我自己都搞混了,因为我在这方面工作的时间太长了。我想太多了!
解决方案
请尝试此代码,使用网络服务获取随机视频并播放。C# WPF
XAML 文件:
<Window x:Class="WpfTutorialSamples.Audio_and_Video.MediaPlayerVideoControlSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MediaPlayerVideoControlSample" Height="300" Width="300">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<MediaElement Source="http://hubblesource.stsci.edu/sources/video/clips/details/images/hst_1.mpg" LoadedBehavior="Manual" Name="mePlayer" />
<StackPanel Grid.Row="1">
<Label Name="lblStatus" Content="Not playing..." HorizontalContentAlignment="Center" Margin="5" />
<WrapPanel HorizontalAlignment="Center">
<Button Name="btnPlay" Click="btnPlay_Click">Play</Button>
<Button Name="btnPause" Margin="5,0" Click="btnPause_Click">Pause</Button>
<Button Name="btnStop" Click="btnStop_Click">Stop</Button>
</WrapPanel>
</StackPanel>
</Grid>
</Window>
类文件:
using System;
using System.Windows;
using System.Windows.Threading;
namespace WpfTutorialSamples.Audio_and_Video
{
public partial class MediaPlayerVideoControlSample : Window
{
public MediaPlayerVideoControlSample()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
if(mePlayer.Source != null)
{
if(mePlayer.NaturalDuration.HasTimeSpan)
lblStatus.Content = String.Format("{0} / {1}", mePlayer.Position.ToString(@"mm\:ss"), mePlayer.NaturalDuration.TimeSpan.ToString(@"mm\:ss"));
}
else
lblStatus.Content = "No file selected...";
}
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
mePlayer.Play();
}
private void btnPause_Click(object sender, RoutedEventArgs e)
{
mePlayer.Pause();
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
mePlayer.Stop();
}
}
}
我希望这段代码对你有用。
谢谢你。
推荐阅读
- c# - C# 返回具有匹配属性的对象列表
- division - colorforth /mod 算法是如何工作的?
- java - 如何制作两个日期之间的日期列表
- swift - 为什么每次其父 View 的 @State 变量发生更改时,View 的 body 属性中的代码都不会运行?
- reactjs - 当我设置 observable 时,mobx-react 观察者不会触发
- ocaml - Ocaml 优化的明天函数
- python - 函数内部的“.append”和“+”语义差异
- plantuml - 如何在 PlantUML 的图例中定义精灵的背景颜色?
- javascript - HTML 中的成绩转换
- java - 如何在 java 中读取文本文件?