1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "components/ui_video.h"
17 #include "securec.h"
18 
19 #ifndef VERSION_LITE
20 namespace OHOS {
UIVideo()21 UIVideo::UIVideo()
22 {
23     SetTouchable(true);
24     SetOnTouchListener(this);
25 }
26 
~UIVideo()27 UIVideo::~UIVideo()
28 {
29     if (videoPlayer_ != nullptr) {
30         videoPlayer_->Stop();
31         videoPlayer_->Reset();
32         videoPlayer_->Release();
33     }
34     if (sliderAnimator_ != nullptr) {
35         delete sliderAnimator_;
36         sliderAnimator_ = nullptr;
37     }
38     if (surfaceView_ != nullptr) {
39         Remove(surfaceView_);
40         delete surfaceView_;
41         surfaceView_ = nullptr;
42     }
43     DeleteController();
44 }
45 
SetSrc(const char * source)46 bool UIVideo::SetSrc(const char* source)
47 {
48     if (source == nullptr) {
49         return false;
50     }
51 
52     InitVideo();
53     src_ = source;
54     std::string uri(source);
55     std::map<std::string, std::string> header;
56     Source videoSource(uri, header);
57     if (videoPlayer_ != nullptr) {
58         int32_t ret = videoPlayer_->SetSource(videoSource);
59         if (ret == 0) {
60             return true;
61         }
62     }
63     return false;
64 }
65 
Prepare()66 bool UIVideo::Prepare()
67 {
68     if (src_ == nullptr) {
69         return false;
70     }
71     if (videoPlayer_ == nullptr) {
72         return false;
73     }
74 
75     int32_t ret = videoPlayer_->Prepare();
76     if (ret != 0) {
77         return false;
78     }
79     SetVolume(DEFAULT_VOLUME, DEFAULT_VOLUME);
80     videoPlayer_->GetDuration(duration_);
81     if (totalTimeLabel_ != nullptr) {
82         char timer[10] = {0}; // 10:timer length
83         if (!GetTimerFromMSecond(duration_, timer, sizeof(timer))) {
84             return false;
85         }
86         totalTimeLabel_->SetText(&timer[0]);
87     }
88 
89     if (titleLabel_ != nullptr) {
90         const char* fileName = nullptr;
91         fileName = strrchr(src_, '/');
92         if (fileName != nullptr) {
93             titleLabel_->SetText(fileName + 1);
94         }
95     }
96     if (sliderAnimatorCallback_) {
97         sliderAnimatorCallback_->SetDuration(duration_);
98     }
99     SetSurfaceInfo();
100 
101     playerListener_ = std::make_shared<PlayerListener>();
102     playerListener_->SetVideoPlayer(this);
103     videoPlayer_->SetPlayerCallback(playerListener_);
104     return true;
105 }
106 
Play()107 bool UIVideo::Play()
108 {
109     if (videoPlayer_ != nullptr) {
110         int32_t ret = videoPlayer_->Play();
111         if (ret == 0) {
112             if (sliderAnimator_ != nullptr) {
113                 sliderAnimator_->Start();
114             }
115             if (pauseButton_ != nullptr) {
116                 pauseButton_->SetVisible(false);
117             }
118             if (playButton_ != nullptr) {
119                 playButton_->SetState(true);
120             }
121             if (surfaceView_ != nullptr) {
122                 surfaceView_->SetVisible(true);
123             }
124             if (videoPlayerListener_ != nullptr) {
125                 videoPlayerListener_->OnPlaybackPlay();
126             }
127             return true;
128         }
129     }
130     return false;
131 }
132 
IsPlaying()133 bool UIVideo::IsPlaying()
134 {
135     if (videoPlayer_ != nullptr) {
136         return videoPlayer_->IsPlaying();
137     }
138     return false;
139 }
140 
Pause()141 bool UIVideo::Pause()
142 {
143     if (videoPlayer_ != nullptr) {
144         int32_t ret = videoPlayer_->Pause();
145         if (ret == 0) {
146             if (sliderAnimator_ != nullptr) {
147                 sliderAnimator_->Pause();
148             }
149             if (playButton_ != nullptr) {
150                 playButton_->SetState(false);
151             }
152             if (pauseButton_ != nullptr) {
153                 pauseButton_->SetVisible(true);
154             }
155             if (videoPlayerListener_ != nullptr) {
156                 videoPlayerListener_->OnPlaybackPause();
157             }
158             return true;
159         }
160     }
161     return false;
162 }
163 
Stop()164 bool UIVideo::Stop()
165 {
166     if (videoPlayer_ != nullptr) {
167         int32_t ret = videoPlayer_->Stop();
168         if (ret == 0) {
169             if (sliderAnimator_ != nullptr) {
170                 sliderAnimator_->Stop();
171             }
172             if (playSlider_ != nullptr) {
173                 playSlider_->SetValue(0);
174             }
175             if (currentTimeLabel_ != nullptr) {
176                 currentTimeLabel_->SetText("00:00:00");
177             }
178             if (totalTimeLabel_ != nullptr) {
179                 totalTimeLabel_->SetText("00:00:00");
180             }
181             if (playButton_ != nullptr) {
182                 playButton_->SetState(false);
183             }
184             if (pauseButton_ != nullptr) {
185                 pauseButton_->SetVisible(true);
186             }
187             if (surfaceView_ != nullptr) {
188                 surfaceView_->SetVisible(false);
189             }
190             if (videoPlayerListener_ != nullptr) {
191                 videoPlayerListener_->OnPlaybackStop();
192             }
193             return true;
194         }
195     }
196     return false;
197 }
198 
Rewind(int64_t mSeconds)199 bool UIVideo::Rewind(int64_t mSeconds)
200 {
201     if (videoPlayer_ != nullptr) {
202         int32_t ret = videoPlayer_->Rewind(mSeconds, PLAYER_SEEK_PREVIOUS_SYNC);
203         if (ret == 0) {
204             return true;
205         }
206     }
207     return false;
208 }
209 
SetVolume(float leftVolume,float rightVolume)210 bool UIVideo::SetVolume(float leftVolume, float rightVolume)
211 {
212     if (videoPlayer_ != nullptr) {
213         int32_t ret = videoPlayer_->SetVolume(leftVolume * MAX_VOLUME, rightVolume * MAX_VOLUME);
214         if (ret == 0) {
215             leftVolumeValue_ = leftVolume;
216             rightVolumeValue_ = rightVolume;
217             if (volumeButton_ != nullptr) {
218                 if ((leftVolumeValue_ == 0) && (rightVolumeValue_ == 0)) {
219                     volumeButton_->SetState(true);
220                 } else {
221                     volumeButton_->SetState(false);
222                 }
223             }
224             return true;
225         }
226     }
227     return false;
228 }
229 
IsSingleLooping()230 bool UIVideo::IsSingleLooping()
231 {
232     if (videoPlayer_ != nullptr) {
233         return videoPlayer_->IsSingleLooping();
234     }
235     return false;
236 }
237 
GetCurrentTime(int64_t & time)238 bool UIVideo::GetCurrentTime(int64_t& time)
239 {
240     if (videoPlayer_ != nullptr) {
241         int32_t ret = videoPlayer_->GetCurrentTime(time);
242         if (ret == 0) {
243             return true;
244         }
245     }
246     return false;
247 }
248 
GetDuration(int64_t & durationMs)249 bool UIVideo::GetDuration(int64_t& durationMs)
250 {
251     if (videoPlayer_ != nullptr) {
252         int32_t ret = videoPlayer_->GetDuration(durationMs);
253         if (ret == 0) {
254             return true;
255         }
256     }
257     return false;
258 }
259 
SetVideoPlayerListener(VideoPlayerListener * listener)260 void UIVideo::SetVideoPlayerListener(VideoPlayerListener* listener)
261 {
262     videoPlayerListener_ = listener;
263 
264     if (playerListener_ != nullptr) {
265         playerListener_->SetVideoPlayerListerner(videoPlayerListener_);
266     }
267 }
268 
Reset()269 bool UIVideo::Reset()
270 {
271     if (videoPlayer_ != nullptr) {
272         if (sliderAnimator_ != nullptr) {
273             sliderAnimator_->Stop();
274         }
275         if (playSlider_ != nullptr) {
276             playSlider_->SetValue(0);
277         }
278         if (currentTimeLabel_ != nullptr) {
279             currentTimeLabel_->SetText("00:00");
280         }
281         if (totalTimeLabel_ != nullptr) {
282             totalTimeLabel_->SetText("00:00");
283         }
284         if (pauseButton_ != nullptr) {
285             pauseButton_->SetVisible(true);
286         }
287         if (playButton_ != nullptr) {
288             playButton_->SetState(false);
289         }
290         int32_t ret = videoPlayer_->Reset();
291         if (ret == 0) {
292             videoPlayer_->Release();
293             return true;
294         }
295     }
296     return false;
297 }
298 
EnableSingleLooping(bool loop)299 bool UIVideo::EnableSingleLooping(bool loop)
300 {
301     if (videoPlayer_ != nullptr) {
302         int32_t ret = videoPlayer_->EnableSingleLooping(loop);
303         if (ret == 0) {
304             return true;
305         }
306     }
307     return false;
308 }
309 
ShowController(bool show)310 void UIVideo::ShowController(bool show)
311 {
312     if (controllerGroup_ != nullptr) {
313         if (controllerGroup_->IsVisible() != show) {
314             controllerGroup_->SetVisible(show);
315             Invalidate();
316         }
317     }
318 }
319 
InitVideo()320 void UIVideo::InitVideo()
321 {
322     if (videoPlayer_ == nullptr) {
323         videoPlayer_ = std::make_shared<Player>();
324     }
325 
326     InitControllerLabel();
327     InitControllerButton();
328     InitControllerSlider();
329 
330     if (surfaceView_ == nullptr) {
331         surfaceView_ = new UISurfaceView();
332         if (surfaceView_ == nullptr) {
333             GRAPHIC_LOGE("new UISurfaceView fail");
334             return;
335         }
336         surfaceView_->SetPosition(0, 0);
337         surfaceView_->SetWidth(DEFAULT_VIEW_WIDTH);
338         surfaceView_->SetHeight(DEFAULT_VIEW_HEIGHT);
339         Add(surfaceView_);
340     }
341     if (controllerGroup_ == nullptr) {
342         controllerGroup_ = new UIViewGroup();
343         if (controllerGroup_ == nullptr) {
344             GRAPHIC_LOGE("new UIViewGroup fail");
345             return;
346         }
347         controllerGroup_->SetPosition(0, 0, DEFAULT_VIEW_WIDTH, DEFAULT_VIEW_HEIGHT);
348         controllerGroup_->SetStyle(STYLE_BACKGROUND_OPA, 0);
349         controllerGroup_->Add(titleLabel_);
350         controllerGroup_->Add(totalTimeLabel_);
351         controllerGroup_->Add(currentTimeLabel_);
352         controllerGroup_->Add(playSlider_);
353         controllerGroup_->Add(volumeButton_);
354         controllerGroup_->Add(playButton_);
355         controllerGroup_->Add(pauseButton_);
356         Add(controllerGroup_);
357     }
358     if (sliderAnimatorCallback_ == nullptr) {
359         sliderAnimatorCallback_ = new SliderAnimatorCallback(this, playSlider_, currentTimeLabel_);
360         if (sliderAnimatorCallback_ == nullptr) {
361             GRAPHIC_LOGE("new SliderAnimatorCallback fail");
362             return;
363         }
364         sliderAnimatorCallback_->SetPlayButton(playButton_);
365     }
366     if (sliderAnimator_ == nullptr) {
367         sliderAnimator_ = new Animator(sliderAnimatorCallback_, this, 0, true);
368         if (sliderAnimator_ == nullptr) {
369             GRAPHIC_LOGE("new Animator fail");
370             return;
371         }
372         sliderAnimatorCallback_->SetSliderAnimator(sliderAnimator_);
373     }
374 }
375 
InitControllerLabel()376 void UIVideo::InitControllerLabel()
377 {
378     if (titleLabel_ == nullptr) {
379         titleLabel_ = new UILabel();
380         if (titleLabel_ == nullptr) {
381             GRAPHIC_LOGE("new UILabel fail");
382             return;
383         }
384         titleLabel_->SetPosition(322, 28, 316, 29); // 322:pos x, 28:pos y, 316:width, 29:height
385         titleLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER,
386                               UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
387         titleLabel_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20); // 20:font size
388         titleLabel_->SetText("title title");
389         titleLabel_->SetTextColor(Color::White());
390     }
391 
392     if (totalTimeLabel_ == nullptr) {
393         totalTimeLabel_ = new UILabel();
394         if (totalTimeLabel_ == nullptr) {
395             GRAPHIC_LOGE("new UILabel fail");
396             return;
397         }
398         // 580:pos x, 56:pos y offset
399         totalTimeLabel_->SetPosition(580, DEFAULT_VIEW_HEIGHT - 56, TIME_LABEL_WIDTH, TIME_LABEL_HEIGHT);
400         totalTimeLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_RIGHT,
401                                   UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
402         totalTimeLabel_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
403         totalTimeLabel_->SetTextColor(Color::White());
404         totalTimeLabel_->SetText("00:00:00");
405     }
406 
407     if (currentTimeLabel_ == nullptr) {
408         currentTimeLabel_ = new UILabel();
409         if (currentTimeLabel_ == nullptr) {
410             GRAPHIC_LOGE("new UILabel fail");
411             return;
412         }
413         // 80:pos x, 56:pos y offset
414         currentTimeLabel_->SetPosition(80, DEFAULT_VIEW_HEIGHT - 56, TIME_LABEL_WIDTH, TIME_LABEL_HEIGHT);
415         currentTimeLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT,
416                                     UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
417         currentTimeLabel_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
418         currentTimeLabel_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
419         currentTimeLabel_->SetText("00:00:00");
420         currentTimeLabel_->SetTextColor(Color::White());
421     }
422 }
423 
InitControllerButton()424 void UIVideo::InitControllerButton()
425 {
426     if (playButton_ == nullptr) {
427         playButton_ = new UIToggleButton();
428         if (playButton_ == nullptr) {
429             GRAPHIC_LOGE("new UIToggleButton fail");
430             return;
431         }
432         // 24:pox x, 88:pos y offset
433         playButton_->SetPosition(24, DEFAULT_VIEW_HEIGHT - 88, TOGGLE_BUTTON_WIDTH, TOGGLE_BUTTON_HEIGHT);
434         playButton_->SetImages(MEDIA_IMAGE_PAUSE, MEDIA_IMAGE_PLAY);
435         playButton_->SetState(true);
436         playButton_->SetOnClickListener(this);
437     }
438 
439     if (pauseButton_ == nullptr) {
440         pauseButton_ = new UIToggleButton();
441         if (pauseButton_ == nullptr) {
442             GRAPHIC_LOGE("new UIToggleButton fail");
443             return;
444         }
445         // 448:pox x, 208:pos y 2:double width 2:double height
446         pauseButton_->SetPosition(448, 208, 2 * TOGGLE_BUTTON_WIDTH, 2 * TOGGLE_BUTTON_HEIGHT);
447         pauseButton_->SetImages(MEDIA_IMAGE_PLAY_CENTER, MEDIA_IMAGE_PLAY_CENTER);
448         pauseButton_->SetVisible(false);
449         pauseButton_->SetOnClickListener(this);
450     }
451 
452     if (volumeButton_ == nullptr) {
453         volumeButton_ = new UIToggleButton();
454         if (volumeButton_ == nullptr) {
455             GRAPHIC_LOGE("new UIToggleButton fail");
456             return;
457         }
458         // 56:pox x offset, 88:pos y offset
459         volumeButton_->SetPosition(DEFAULT_VIEW_WIDTH - 56, DEFAULT_VIEW_HEIGHT - 88, TOGGLE_BUTTON_WIDTH,
460                                    TOGGLE_BUTTON_HEIGHT);
461         volumeButton_->SetImages(MEDIA_IMAGE_MUTE, MEDIA_IMAGE_VOLUME);
462         volumeButton_->SetOnClickListener(this);
463     }
464 }
465 
OnVideoComplete()466 void UIVideo::OnVideoComplete()
467 {
468     if (videoPlayer_ != nullptr) {
469         videoPlayer_->Stop();
470         completeFlag_ = true;
471 
472         if (sliderAnimator_ != nullptr) {
473             sliderAnimator_->Stop();
474         }
475         if (playSlider_ != nullptr) {
476             playSlider_->SetValue(playSlider_->GetRangeMax());
477         }
478         if (playButton_ != nullptr) {
479             playButton_->SetState(false);
480         }
481         if (pauseButton_ != nullptr) {
482             pauseButton_->SetVisible(true);
483         }
484         if (controllerGroup_ != nullptr) {
485             controllerGroup_->SetVisible(true);
486         }
487         Invalidate();
488     }
489 }
490 
InitControllerSlider()491 void UIVideo::InitControllerSlider()
492 {
493     if (playSlider_ == nullptr) {
494         playSlider_ = new UISlider();
495         if (playSlider_ == nullptr) {
496             GRAPHIC_LOGE("new UISlider fail");
497             return;
498         }
499         // 80:pos x, 74:width offset, 5:multiply, 6:divide, 4:height
500         playSlider_->SetPosition(80, DEFAULT_VIEW_HEIGHT - 74, DEFAULT_VIEW_WIDTH * 5 / 6, 4);
501         playSlider_->SetValidHeight(4);                         // 4:height
502         playSlider_->SetValidWidth(DEFAULT_VIEW_WIDTH * 5 / 6); // 5:multiply, 6:divide
503         playSlider_->SetRange(100, 0);                          // 100:range max
504         playSlider_->SetValue(0);
505         playSlider_->SetKnobWidth(KNOB_WIDTH);
506         playSlider_->SetSliderRadius(5, 5); // 5:background radius, 5:foreground radius
507         playSlider_->SetKnobRadius(KNOB_WIDTH / 2); // 2:half
508         playSlider_->SetKnobStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
509         playSlider_->SetBackgroundStyle(STYLE_BACKGROUND_COLOR, 0x1A888888); // 0x1A888888:slider background color
510         playSlider_->SetBackgroundStyle(STYLE_BACKGROUND_OPA, 90);           // 90:background opa
511         playSlider_->SetDirection(UISlider::Direction::DIR_LEFT_TO_RIGHT);
512         playSlider_->SetSliderEventListener(this);
513     }
514 }
515 
DeleteController()516 void UIVideo::DeleteController()
517 {
518     if (controllerGroup_ != nullptr) {
519         controllerGroup_->RemoveAll();
520         Remove(controllerGroup_);
521         delete controllerGroup_;
522         controllerGroup_ = nullptr;
523     }
524     if (playButton_ != nullptr) {
525         delete playButton_;
526         playButton_ = nullptr;
527     }
528     if (pauseButton_ != nullptr) {
529         delete pauseButton_;
530         pauseButton_ = nullptr;
531     }
532     if (volumeButton_ != nullptr) {
533         delete volumeButton_;
534         volumeButton_ = nullptr;
535     }
536     if (playSlider_ != nullptr) {
537         delete playSlider_;
538         playSlider_ = nullptr;
539     }
540     if (sliderAnimatorCallback_ != nullptr) {
541         delete sliderAnimatorCallback_;
542         sliderAnimatorCallback_ = nullptr;
543     }
544     if (titleLabel_ != nullptr) {
545         delete titleLabel_;
546         titleLabel_ = nullptr;
547     }
548     if (totalTimeLabel_ != nullptr) {
549         delete totalTimeLabel_;
550         totalTimeLabel_ = nullptr;
551     }
552     if (currentTimeLabel_ != nullptr) {
553         delete currentTimeLabel_;
554         currentTimeLabel_ = nullptr;
555     }
556 }
557 
OnClick(UIView & view,const ClickEvent & event)558 bool UIVideo::OnClick(UIView& view, const ClickEvent& event)
559 {
560     if (videoPlayer_ == nullptr) {
561         return true;
562     }
563     if (&view == playButton_) {
564         if (completeFlag_) {
565             completeFlag_ = false;
566             if (pauseButton_ != nullptr) {
567                 pauseButton_->SetVisible(true);
568             }
569             if (controllerGroup_ != nullptr) {
570                 controllerGroup_->SetVisible(true);
571             }
572             return true;
573         }
574 
575         if ((sliderAnimatorCallback_ != nullptr) && (pauseButton_ != nullptr) && (playButton_ != nullptr)) {
576             sliderAnimatorCallback_->ResetTickTime();
577             if (playButton_->GetState()) {
578                 Play();
579                 pauseButton_->SetVisible(false);
580             } else {
581                 Pause();
582                 pauseButton_->SetVisible(true);
583             }
584             pauseButton_->Invalidate();
585         }
586     } else if (&view == pauseButton_) {
587         if ((pauseButton_ != nullptr) && pauseButton_->IsVisible()) {
588             pauseButton_->SetVisible(false);
589             Play();
590         }
591     } else if (&view == volumeButton_) {
592         if ((volumeButton_ != nullptr) && volumeButton_->GetState()) {
593             videoPlayer_->SetVolume(0, 0);
594         } else {
595             SetVolume(leftVolumeValue_, rightVolumeValue_);
596         }
597     }
598     return true;
599 }
600 
OnPress(UIView & view,const PressEvent & event)601 bool UIVideo::OnPress(UIView& view, const PressEvent& event)
602 {
603     if (sliderAnimatorCallback_ != nullptr) {
604         sliderAnimatorCallback_->ResetTickTime();
605         if (controllerGroup_ != nullptr) {
606             controllerGroup_->SetVisible(true);
607         }
608         Invalidate();
609     }
610     return true;
611 }
612 
OnChange(int32_t progress)613 void UIVideo::OnChange(int32_t progress)
614 {
615     if (videoPlayer_ != nullptr) {
616         if (sliderAnimatorCallback_ != nullptr) {
617             sliderAnimatorCallback_->ResetTickTime();
618         }
619         int64_t currentValue = progress * duration_ / 100; // 100:percent
620         if (currentTimeLabel_ != nullptr) {
621             char timer[10] = {0}; // 10:timer length
622             if (!GetTimerFromMSecond(currentValue, timer, sizeof(timer))) {
623                 return;
624             }
625             currentTimeLabel_->SetText(&timer[0]);
626         }
627         videoPlayer_->Rewind(currentValue, PLAYER_SEEK_PREVIOUS_SYNC);
628     }
629 }
630 
SetSurfaceInfo()631 void UIVideo::SetSurfaceInfo()
632 {
633     if (videoPlayer_ == nullptr) {
634         return;
635     }
636     int32_t width = 0;
637     int32_t height = 0;
638     videoPlayer_->GetVideoWidth(width);
639     videoPlayer_->GetVideoHeight(height);
640 
641     if ((width <= 0) || (height <= 0)) {
642         videoPlayer_->Reset();
643         return;
644     }
645 
646     int16_t viewWidth = GetWidth();
647     int16_t viewHeight = GetHeight();
648     if ((viewWidth <= 0) || (viewHeight <= 0)) {
649         videoPlayer_->Reset();
650         return;
651     }
652     float ratioX = static_cast<float>(width) / viewWidth;
653     float ratioY = static_cast<float>(height) / viewHeight;
654     uint16_t surfaceViewWidth;
655     uint16_t surfaceViewHeight;
656     uint16_t surfaceViewPositionX = 0;
657     uint16_t surfaceViewPositionY = 0;
658     if (ratioX > ratioY) {
659         surfaceViewWidth = viewWidth;
660         surfaceViewHeight = height / ratioX;
661         surfaceViewPositionY = (viewHeight - surfaceViewHeight) / 2; // 2:half
662     } else {
663         surfaceViewWidth = width / ratioY;
664         surfaceViewHeight = viewHeight;
665         surfaceViewPositionX = (viewWidth - surfaceViewWidth) / 2; // 2:half
666     }
667     if (surfaceView_ != nullptr) {
668         surfaceView_->SetPosition(surfaceViewPositionX, surfaceViewPositionY);
669         surfaceView_->SetWidth(surfaceViewWidth - 1);
670         surfaceView_->SetHeight(surfaceViewHeight);
671         videoPlayer_->SetVideoSurface(surfaceView_->GetSurface());
672     }
673 }
674 
GetTimerFromMSecond(int64_t currentTime,char * timer,int32_t len)675 bool UIVideo::GetTimerFromMSecond(int64_t currentTime, char* timer, int32_t len)
676 {
677     int64_t currentSecond = currentTime / 1000; // 1000:millisecond
678     int32_t second = currentSecond % 60;        // 60:second
679     int32_t minute = (currentSecond / 60) % 60; // 60:minute 60:second
680     int32_t hour = (currentSecond / 60) / 60;   // 60:minute 60:second
681     int32_t ret = 0;
682     ret = sprintf_s(timer, len, "%02d:%02d:%02d", hour, minute, second);
683     if (ret < 0) {
684         return false;
685     }
686     return true;
687 }
688 
Callback(UIView * view)689 void UIVideo::SliderAnimatorCallback::Callback(UIView* view)
690 {
691     if (video_ != nullptr) {
692         int64_t currentTime = 0;
693         video_->GetCurrentTime(currentTime);
694         if (timeLabel_ != nullptr) {
695             char timer[10] = {0}; // 10:timer length
696             if (!video_->GetTimerFromMSecond(currentTime, timer, sizeof(timer))) {
697                 return;
698             }
699             timeLabel_->SetText(&timer[0]);
700             timeLabel_->Invalidate();
701         }
702         if (slider_ != nullptr) {
703             int64_t curPosition = currentTime * slider_->GetRangeMax() / duration_;
704             slider_->SetValue(curPosition);
705             slider_->Invalidate();
706         }
707 
708         uint32_t runTime = 0;
709         if (sliderAnimator_ != nullptr) {
710             runTime = sliderAnimator_->GetRunTime();
711         }
712         if ((runTime > tickCount_) && (runTime - tickCount_ >= HIDE_MILLI_SECOND)) {
713             video_->ShowController(false);
714             tickCount_ = runTime;
715         }
716     }
717 }
718 } // namespace OHOS
719 
720 #endif // VERSION_LITE
721