首先,我获取所有YouTube视频的列表,并将列表添加到listBox:
static List<string> videosList = new List<string>();
static List<string> videosUrl = new List<string>();
public async void RetrieveUploadsList()
{
UserCredentials();
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
var channelsListRequest = youtubeService.Channels.List("contentDetails");
channelsListRequest.Mine = true;
var channelsListResponse = await channelsListRequest.ExecuteAsync();
foreach (var channel in channelsListResponse.Items)
{
var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
Console.WriteLine("Videos in list {0}", uploadsListId);
var nextPageToken = "";
while (nextPageToken != null)
{
var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet");
playlistItemsListRequest.PlaylistId = uploadsListId;
playlistItemsListRequest.MaxResults = 50;
playlistItemsListRequest.PageToken = nextPageToken;
var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();
foreach (var playlistItem in playlistItemsListResponse.Items)
{
videosList.Add(playlistItem.Snippet.Title + " " + playlistItem.Snippet.ResourceId.VideoId);
videosUrl.Add("http://www.youtube.com/v/" + playlistItem.Snippet.ResourceId.VideoId);
listBox1.Items.Add(playlistItem.Snippet.Title + " " + playlistItem.Snippet.PublishedAt);
}
nextPageToken = playlistItemsListResponse.NextPageToken;
}
}
if (this.listBox1.Items.Count > 80)
{
this.listBox1.SelectedIndex = 80;
axWindowsMediaPlayer1.URL = videosUrl[80];
}
}
然后listBox1选择索引更改事件:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = videosUrl[listBox1.SelectedIndex];
}
最后Windows媒体播放器控制播放状态更改事件:
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
e.newState = 2;
if (e.newState == 3)
{
axWindowsMediaPlayer1.fullScreen = true;
}
}
问题是,在我点击任何内容开始播放列表中的任何视频之前,它正在进入PlayStateChange事件并尝试执行fullScreen = true似乎正在尝试自动开始播放视频。
所以我补充说:
e.newState = 2;
代码2表示暂停。但现在即使我点击播放按钮,它也会使这行e.newState = 2;它会被暂停。
我找不到办法让它不要自动开始播放或认为它。
您可以在此链接中看到e.newState的所有代码
代码1 =已停止
代码2 =暂停
代码3 =正在播放