2 years ago

#64263

test-img

Harry Phan

Why Post API twice?

My idea : I get video from api and add to List video. And I Have one video Contact . After one video from List Video will run Contact Video and so on ,accompanied by push api number of runs.

My Problem is Contact video and push api of previous video to double count.

My code Check Video End :

private void MediaEnded(object sender, EventArgs e)
{
    if (currentMedia!= null)
    {
        if (currentMedia.Id != 0 && currentMedia.IsRunning == 1)
        {
            // Media mediaResponse = await Task.Run(() => client.UpdateDataAfterPlay(media.Id));
            UpdateDataAdsAfterPlay(currentMedia);
            Debug.WriteLine("Currenent MediaID : ", currentMedia.Id); 
        }

        IsPlayContactAds = !IsPlayContactAds;

        if (IsPlayContactAds)
        {
            string file = System.Windows.Forms.Application.StartupPath + string.Format("{0}.mp4", @"\contactads");
            MainSecondViewModel.ShowAds(new Uri(file, UriKind.Relative), IsMute);
            Debug.WriteLine(file);
        }
        else
        {
            int index = ListVideos.IndexOf(currentMedia);

            if (index >= ListVideos.Where(x => CheckVideoIsAllowPlay(x)).Count() - 1)
            {
                currentMedia = GetMediaFristRun();

                if (currentMedia == null)
                {
                    MessageNotificationWindow message = new MessageNotificationWindow();
                    message.ContentNotification.Text = "Chưa đăng kí dữ liệu quảng cáo";
                    message.ShowDialog();
                }
            }
            else
            {
                index++;
                currentMedia = ListVideos[index];
            }

            PlayVideoAds(currentMedia);
            Console.WriteLine(index); 
        }
    }
    else
    {
        if(MainSecondViewModel._VideoControl != null)
        {
            MainSecondViewModel._VideoControl.Stop();
        }

        MessageNotificationWindow message = new MessageNotificationWindow();
        message.ContentNotification.Text = "Chưa đăng kí dữ liệu quảng cáo";
        message.ShowDialog();
    }
}

Code update after video :

private async void UpdateDataAdsAfterPlay(MediaData media)
{
    MediaClient client = new MediaClient(this, this, this);
    Media mediaResponse = await Task.Run(() => client.UpdateDataAfterPlay(media.Id));

    if (mediaResponse != null && mediaResponse.Status == (int)HttpStatusCode.OK)
    {
        if (mediaResponse.Data.IsRunning == 0)
        {
            Application.Current.Dispatcher.Invoke((Action)delegate
            {
                int index = ListVideos.IndexOf(media);
                ListVideos.RemoveAt(index);
                ListVideos.Insert(index, mediaResponse.Data);
            });
        }
    }
    else
    {
        Application.Current.Dispatcher.Invoke((Action)delegate
        {
            int index = ListVideos.IndexOf(media);
            ListVideos.RemoveAt(index);
            ListVideos.Insert(index, mediaResponse.Data);
        });
    }
}

Code MediaClient :

public Media UpdateDataAfterPlay(long id)
{
    Console.WriteLine("Cap nhat ID Ads : " + id);
    RestRequest request = new RestRequest(string.Format(LinkCallApi.API_UPDATE_DATA_AFTER_PLAY,id.ToString()), Method.POST);
    request.AddHeader("Content-Type", "application/json");
    Debug.WriteLine(request); 
    return Get<Media>(request);
}

c#

wpf

api

media

0 Answers

Your Answer

Accepted video resources