2012-12-21 17 views
0

有誰知道如何在使用可恢復的上傳時將視頻添加到播放列表?使用可恢復的上傳將視頻添加到播放列表

讓我說清楚。以下是我的代碼。

Video newVideo = new Video(); 
newVideo.Title = fileName.Split(".".ToCharArray())[0]; 
newVideo.Tags.Add(new MediaCategory("Nonprofit", YouTubeNameTable.CategorySchema)); 
newVideo.Description = DateTime.Now.ToShortDateString(); 
newVideo.YouTubeEntry.Private = false; 
ResumableUploader m_ResumableUploader = null; 
Authenticator YouTubeAuthenticator; 

m_ResumableUploader = new ResumableUploader(100); //chunksize 1 MB 
m_ResumableUploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(m_ResumableUploader_AsyncOperationCompleted); 
m_ResumableUploader.AsyncOperationProgress += new AsyncOperationProgressEventHandler(m_ResumableUploader_AsyncOperationProgress); 

YouTubeAuthenticator = new ClientLoginAuthenticator("YouTubeUploader", ServiceNames.YouTube, ConfigurationManager.AppSettings["USERNAME"].ToString(), ConfigurationManager.AppSettings["PASSWORD"].ToString()); 

YouTubeAuthenticator.DeveloperKey = ConfigurationManager.AppSettings["DEVELOPER_KEY"].ToString(); 
string contentType = MediaFileSource.GetContentTypeForFileName(fileName); 
newVideo.MediaSource = new MediaFileSource(filePath, contentType); 

AtomLink link = new AtomLink("http://uploads.gdata.youtube.com/resumable/feeds/api/users/<username>/uploads"); 
link.Rel = ResumableUploader.CreateMediaRelation; 
newVideo.YouTubeEntry.Links.Add(link); 

System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); 

m_ResumableUploader.InsertAsync(YouTubeAuthenticator, newVideo.YouTubeEntry, new object()); 

我想直接上傳視頻到播放列表。

我正在看這段代碼,我很難連接這兩個。需要幫忙。

https://developers.google.com/youtube/2.0/developers_guide_dotnet

添加視頻到播放列表

您可以通過使用PlayListMember對象的視頻添加到播放列表。以下代碼創建一個具有已知ID值的PlayListMember對象,然後將其添加到播放列表對象(p)中。由於該請求沒有指定視頻在播放列表中出現的位置,因此新視頻將添加到播放列表的末尾。

// For Playlist object p 
PlayListMember pm = new PlayListMember(); 
// Insert <id> or <videoid> for video here 
pm.Id = VIDEOID; 
request.AddToPlaylist(p, pm); 

更新1 - 添加到播放列表時,我得到一個 「不支持的URI格式」 的錯誤。

YouTubeRequestSettings ys = new YouTubeRequestSettings("YouTubeUploader", 
ConfigurationManager.AppSettings["DEVELOPER_KEY"].ToString()); 
YouTubeRequest ytr = new YouTubeRequest(ys); 
Video v = ytr.ParseVideo(e.ResponseStream);     
PlayListMember pm = new PlayListMember(); 
Feed<Playlist> userPlaylists = ytr.GetPlaylistsFeed(ytr.Credentials.Username); 
foreach (Playlist p in userPlaylists.Entries) 
{ 
    fs.WriteLine(p.Title); 
    if (p.Title == "Test 2") 
    { 
      pm.Id = v.VideoId; 
      ytr.AddToPlaylist(p, pm); 

      fs.WriteLine("Added To Playlist "); 
    } 
} 
+0

可能重複(http://stackoverflow.com/questions/13976749/youtube-api-添加-a-video-to-a-playlist-using-resumable-upload) –

+0

是自己的問題的重複,但是這一次是有據可查的。我想我們應該欣賞現在的努力 – Steve

+0

好的。現在我能得到答覆嗎? –

回答

1

我想通了。 YouTube上的代碼有錯誤。它告訴你將PlayListMember id設置爲VIDEOID。

// For Playlist object p 
PlayListMember pm = new PlayListMember(); 
// Insert <id> or <videoid> for video here 
pm.Id = VIDEOID; 
request.AddToPlaylist(p, pm); 

這是不正確的。您需要設置VIDEOID爲[YouTube的API - 使用可恢復上傳添加視頻到播放列表]的PlayListMember VideoID的

// For Playlist object p 
PlayListMember pm = new PlayListMember(); 
// Insert <id> or <videoid> for video here 
pm.VideoId = VIDEOID; 
request.AddToPlaylist(p, pm); 
+0

感謝您的支持,並對開發人員指南中的錯誤信息感到抱歉。我提出了一個更新的要求,以便其他人不會在未來受到誤導。 –

0

這兩個操作鏈接的是視頻ID。您需要從第一部分(上載)獲取視頻ID並將其提供給第二部分(播放列表插入)。因此,問題歸結爲在使用.NET客戶端庫執行可恢復上載時,如何獲得新視頻的視頻ID。

我覺得這個示例代碼與專門處理:http://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/YouTubeUploader/YouTubeUploader/insertandretry.cs#169

+0

嘗試添加到現有播放列表時,出現不受支持的URI格式錯誤。參見上面的更新。 –

+0

您是否已完成健康檢查以確保v.VideoId設置正確? –

+0

是的。 videoid正在設置 –

相關問題