The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

704 lines
26KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: VideoDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Plays video files.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics, juce_gui_extra, juce_video
  26. exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
  27. type: Component
  28. mainClass: VideoDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #include "../Assets/DemoUtilities.h"
  34. #if JUCE_MAC || JUCE_WINDOWS
  35. //==============================================================================
  36. // so that we can easily have two video windows each with a file browser, wrap this up as a class..
  37. class MovieComponentWithFileBrowser : public Component,
  38. public DragAndDropTarget,
  39. private FilenameComponentListener
  40. {
  41. public:
  42. MovieComponentWithFileBrowser()
  43. : videoComp (true)
  44. {
  45. addAndMakeVisible (videoComp);
  46. addAndMakeVisible (fileChooser);
  47. fileChooser.addListener (this);
  48. fileChooser.setBrowseButtonText ("browse");
  49. }
  50. void setFile (const File& file)
  51. {
  52. fileChooser.setCurrentFile (file, true);
  53. }
  54. void paintOverChildren (Graphics& g) override
  55. {
  56. if (isDragOver)
  57. {
  58. g.setColour (Colours::red);
  59. g.drawRect (fileChooser.getBounds(), 2);
  60. }
  61. }
  62. void resized() override
  63. {
  64. videoComp.setBounds (getLocalBounds().reduced (10));
  65. }
  66. bool isInterestedInDragSource (const SourceDetails&) override { return true; }
  67. void itemDragEnter (const SourceDetails&) override
  68. {
  69. isDragOver = true;
  70. repaint();
  71. }
  72. void itemDragExit (const SourceDetails&) override
  73. {
  74. isDragOver = false;
  75. repaint();
  76. }
  77. void itemDropped (const SourceDetails& dragSourceDetails) override
  78. {
  79. setFile (dragSourceDetails.description.toString());
  80. isDragOver = false;
  81. repaint();
  82. }
  83. private:
  84. VideoComponent videoComp;
  85. bool isDragOver = false;
  86. FilenameComponent fileChooser { "movie", {}, true, false, false, "*", {}, "(choose a video file to play)"};
  87. void filenameComponentChanged (FilenameComponent*) override
  88. {
  89. auto url = URL (fileChooser.getCurrentFile());
  90. // this is called when the user changes the filename in the file chooser box
  91. auto result = videoComp.load (url);
  92. videoLoadingFinished (url, result);
  93. }
  94. void videoLoadingFinished (const URL& url, Result result)
  95. {
  96. ignoreUnused (url);
  97. if (result.wasOk())
  98. {
  99. // loaded the file ok, so let's start it playing..
  100. videoComp.play();
  101. resized(); // update to reflect the video's aspect ratio
  102. }
  103. else
  104. {
  105. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  106. "Couldn't load the file!",
  107. result.getErrorMessage());
  108. }
  109. }
  110. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MovieComponentWithFileBrowser)
  111. };
  112. //==============================================================================
  113. class VideoDemo : public Component,
  114. public DragAndDropContainer,
  115. private FileBrowserListener
  116. {
  117. public:
  118. VideoDemo()
  119. {
  120. setOpaque (true);
  121. movieList.setDirectory (File::getSpecialLocation (File::userMoviesDirectory), true, true);
  122. directoryThread.startThread (1);
  123. fileTree.addListener (this);
  124. fileTree.setColour (FileTreeComponent::backgroundColourId, Colours::lightgrey.withAlpha (0.6f));
  125. addAndMakeVisible (fileTree);
  126. addAndMakeVisible (resizerBar);
  127. loadLeftButton .onClick = [this] { movieCompLeft .setFile (fileTree.getSelectedFile (0)); };
  128. loadRightButton.onClick = [this] { movieCompRight.setFile (fileTree.getSelectedFile (0)); };
  129. addAndMakeVisible (loadLeftButton);
  130. addAndMakeVisible (loadRightButton);
  131. addAndMakeVisible (movieCompLeft);
  132. addAndMakeVisible (movieCompRight);
  133. // we have to set up our StretchableLayoutManager so it know the limits and preferred sizes of it's contents
  134. stretchableManager.setItemLayout (0, // for the fileTree
  135. -0.1, -0.9, // must be between 50 pixels and 90% of the available space
  136. -0.3); // and its preferred size is 30% of the total available space
  137. stretchableManager.setItemLayout (1, // for the resize bar
  138. 5, 5, 5); // hard limit to 5 pixels
  139. stretchableManager.setItemLayout (2, // for the movie components
  140. -0.1, -0.9, // size must be between 50 pixels and 90% of the available space
  141. -0.7); // and its preferred size is 70% of the total available space
  142. setSize (500, 500);
  143. }
  144. ~VideoDemo()
  145. {
  146. fileTree.removeListener (this);
  147. }
  148. void paint (Graphics& g) override
  149. {
  150. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  151. }
  152. void resized() override
  153. {
  154. // make a list of two of our child components that we want to reposition
  155. Component* comps[] = { &fileTree, &resizerBar, nullptr };
  156. // this will position the 3 components, one above the other, to fit
  157. // vertically into the rectangle provided.
  158. stretchableManager.layOutComponents (comps, 3,
  159. 0, 0, getWidth(), getHeight(),
  160. true, true);
  161. // now position out two video components in the space that's left
  162. auto area = getLocalBounds().removeFromBottom (getHeight() - resizerBar.getBottom());
  163. {
  164. auto buttonArea = area.removeFromTop (30);
  165. loadLeftButton .setBounds (buttonArea.removeFromLeft (buttonArea.getWidth() / 2).reduced (5));
  166. loadRightButton.setBounds (buttonArea.reduced (5));
  167. }
  168. movieCompLeft .setBounds (area.removeFromLeft (area.getWidth() / 2).reduced (5));
  169. movieCompRight.setBounds (area.reduced (5));
  170. }
  171. private:
  172. std::unique_ptr<FileChooser> fileChooser;
  173. WildcardFileFilter moviesWildcardFilter { "*", "*", "Movies File Filter" };
  174. TimeSliceThread directoryThread { "Movie File Scanner Thread" };
  175. DirectoryContentsList movieList { &moviesWildcardFilter, directoryThread };
  176. FileTreeComponent fileTree { movieList };
  177. StretchableLayoutManager stretchableManager;
  178. StretchableLayoutResizerBar resizerBar { &stretchableManager, 1, false };
  179. TextButton loadLeftButton { "Load Left" },
  180. loadRightButton { "Load Right" };
  181. MovieComponentWithFileBrowser movieCompLeft, movieCompRight;
  182. void selectionChanged() override
  183. {
  184. // we're just going to update the drag description of out tree so that rows can be dragged onto the file players
  185. fileTree.setDragAndDropDescription (fileTree.getSelectedFile().getFullPathName());
  186. }
  187. void fileClicked (const File&, const MouseEvent&) override {}
  188. void fileDoubleClicked (const File&) override {}
  189. void browserRootChanged (const File&) override {}
  190. void selectVideoFile()
  191. {
  192. fileChooser.reset (new FileChooser ("Choose a file to open...", File::getCurrentWorkingDirectory(),
  193. "*", false));
  194. fileChooser->launchAsync (FileBrowserComponent::openMode | FileBrowserComponent::canSelectFiles,
  195. [this] (const FileChooser& chooser)
  196. {
  197. String chosen;
  198. auto results = chooser.getURLResults();
  199. // TODO: support non local files too
  200. if (results.size() > 0)
  201. movieCompLeft.setFile (results[0].getLocalFile());
  202. });
  203. }
  204. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VideoDemo)
  205. };
  206. #elif JUCE_IOS || JUCE_ANDROID
  207. //==============================================================================
  208. class VideoDemo : public Component,
  209. private Timer
  210. {
  211. public:
  212. VideoDemo()
  213. : videoCompWithNativeControls (true),
  214. videoCompNoNativeControls (false)
  215. {
  216. loadLocalButton .onClick = [this] { selectVideoFile(); };
  217. loadUrlButton .onClick = [this] { showVideoUrlPrompt(); };
  218. seekToStartButton.onClick = [this] { seekVideoToStart(); };
  219. playButton .onClick = [this] { playVideo(); };
  220. pauseButton .onClick = [this] { pauseVideo(); };
  221. unloadButton .onClick = [this] { unloadVideoFile(); };
  222. volumeLabel .setColour (Label::textColourId, Colours::white);
  223. currentPositionLabel.setColour (Label::textColourId, Colours::white);
  224. volumeLabel .setJustificationType (Justification::right);
  225. currentPositionLabel.setJustificationType (Justification::right);
  226. volumeSlider .setRange (0.0, 1.0);
  227. positionSlider.setRange (0.0, 1.0);
  228. volumeSlider .setSliderSnapsToMousePosition (false);
  229. positionSlider.setSliderSnapsToMousePosition (false);
  230. volumeSlider.setSkewFactor (1.5);
  231. volumeSlider.setValue (1.0, dontSendNotification);
  232. #if JUCE_SYNC_VIDEO_VOLUME_WITH_OS_MEDIA_VOLUME
  233. curVideoComp->onGlobalMediaVolumeChanged = [this]() { volumeSlider.setValue (curVideoComp->getAudioVolume(), dontSendNotification); };
  234. #endif
  235. volumeSlider .onValueChange = [this]() { curVideoComp->setAudioVolume ((float) volumeSlider.getValue()); };
  236. positionSlider.onValueChange = [this]() { seekVideoToNormalisedPosition (positionSlider.getValue()); };
  237. positionSlider.onDragStart = [this]()
  238. {
  239. positionSliderDragging = true;
  240. wasPlayingBeforeDragStart = curVideoComp->isPlaying();
  241. if (wasPlayingBeforeDragStart)
  242. curVideoComp->stop();
  243. };
  244. positionSlider.onDragEnd = [this]()
  245. {
  246. if (wasPlayingBeforeDragStart)
  247. curVideoComp->play();
  248. wasPlayingBeforeDragStart = false;
  249. // Ensure the slider does not temporarily jump back on consecutive timer callback.
  250. Timer::callAfterDelay (500, [this]() { positionSliderDragging = false; });
  251. };
  252. playSpeedComboBox.addItem ("25%", 25);
  253. playSpeedComboBox.addItem ("50%", 50);
  254. playSpeedComboBox.addItem ("100%", 100);
  255. playSpeedComboBox.addItem ("200%", 200);
  256. playSpeedComboBox.addItem ("400%", 400);
  257. playSpeedComboBox.setSelectedId (100, dontSendNotification);
  258. playSpeedComboBox.onChange = [this]() { curVideoComp->setPlaySpeed (playSpeedComboBox.getSelectedId() / 100.0); };
  259. setTransportControlsEnabled (false);
  260. addAndMakeVisible (loadLocalButton);
  261. addAndMakeVisible (loadUrlButton);
  262. addAndMakeVisible (volumeLabel);
  263. addAndMakeVisible (volumeSlider);
  264. addChildComponent (videoCompWithNativeControls);
  265. addChildComponent (videoCompNoNativeControls);
  266. addAndMakeVisible (positionSlider);
  267. addAndMakeVisible (currentPositionLabel);
  268. addAndMakeVisible (playSpeedComboBox);
  269. addAndMakeVisible (seekToStartButton);
  270. addAndMakeVisible (playButton);
  271. addAndMakeVisible (unloadButton);
  272. addChildComponent (pauseButton);
  273. setSize (500, 500);
  274. RuntimePermissions::request (RuntimePermissions::readExternalStorage,
  275. [] (bool granted)
  276. {
  277. if (! granted)
  278. {
  279. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  280. "Permissions warning",
  281. "External storage access permission not granted, some files"
  282. " may be inaccessible.");
  283. }
  284. });
  285. setPortraitOrientationEnabled (true);
  286. }
  287. ~VideoDemo()
  288. {
  289. curVideoComp->onPlaybackStarted = nullptr;
  290. curVideoComp->onPlaybackStopped = nullptr;
  291. curVideoComp->onErrorOccurred = nullptr;
  292. curVideoComp->onGlobalMediaVolumeChanged = nullptr;
  293. setPortraitOrientationEnabled (false);
  294. }
  295. void paint (Graphics& g) override
  296. {
  297. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  298. }
  299. void resized() override
  300. {
  301. auto area = getLocalBounds();
  302. int marginSize = 5;
  303. int buttonHeight = 20;
  304. area.reduce (0, marginSize);
  305. auto topArea = area.removeFromTop (buttonHeight);
  306. loadLocalButton.setBounds (topArea.removeFromLeft (topArea.getWidth() / 6));
  307. loadUrlButton.setBounds (topArea.removeFromLeft (loadLocalButton.getWidth()));
  308. volumeLabel.setBounds (topArea.removeFromLeft (loadLocalButton.getWidth()));
  309. volumeSlider.setBounds (topArea.reduced (10, 0));
  310. auto transportArea = area.removeFromBottom (buttonHeight);
  311. auto positionArea = area.removeFromBottom (buttonHeight).reduced (marginSize, 0);
  312. playSpeedComboBox.setBounds (transportArea.removeFromLeft (jmax (50, transportArea.getWidth() / 5)));
  313. auto controlWidth = transportArea.getWidth() / 3;
  314. currentPositionLabel.setBounds (positionArea.removeFromRight (jmax (150, controlWidth)));
  315. positionSlider.setBounds (positionArea);
  316. seekToStartButton.setBounds (transportArea.removeFromLeft (controlWidth));
  317. playButton .setBounds (transportArea.removeFromLeft (controlWidth));
  318. unloadButton .setBounds (transportArea.removeFromLeft (controlWidth));
  319. pauseButton.setBounds (playButton.getBounds());
  320. area.removeFromTop (marginSize);
  321. area.removeFromBottom (marginSize);
  322. videoCompWithNativeControls.setBounds (area);
  323. videoCompNoNativeControls.setBounds (area);
  324. if (positionSlider.getWidth() > 0)
  325. positionSlider.setMouseDragSensitivity (positionSlider.getWidth());
  326. }
  327. private:
  328. TextButton loadLocalButton { "Load Local" };
  329. TextButton loadUrlButton { "Load URL" };
  330. Label volumeLabel { "volumeLabel", "Vol:" };
  331. Slider volumeSlider { Slider::LinearHorizontal, Slider::NoTextBox };
  332. VideoComponent videoCompWithNativeControls;
  333. VideoComponent videoCompNoNativeControls;
  334. #if JUCE_IOS || JUCE_MAC
  335. VideoComponent* curVideoComp = &videoCompWithNativeControls;
  336. #else
  337. VideoComponent* curVideoComp = &videoCompNoNativeControls;
  338. #endif
  339. bool isFirstSetup = true;
  340. Slider positionSlider { Slider::LinearHorizontal, Slider::NoTextBox };
  341. bool positionSliderDragging = false;
  342. bool wasPlayingBeforeDragStart = false;
  343. Label currentPositionLabel { "currentPositionLabel", "-:- / -:-" };
  344. ComboBox playSpeedComboBox { "playSpeedComboBox" };
  345. TextButton seekToStartButton { "|<" };
  346. TextButton playButton { "Play" };
  347. TextButton pauseButton { "Pause" };
  348. TextButton unloadButton { "Unload" };
  349. std::unique_ptr<FileChooser> fileChooser;
  350. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VideoDemo)
  351. JUCE_DECLARE_WEAK_REFERENCEABLE (VideoDemo)
  352. //==============================================================================
  353. void setPortraitOrientationEnabled (bool shouldBeEnabled)
  354. {
  355. auto allowedOrientations = Desktop::getInstance().getOrientationsEnabled();
  356. if (shouldBeEnabled)
  357. allowedOrientations |= Desktop::upright;
  358. else
  359. allowedOrientations &= ~Desktop::upright;
  360. Desktop::getInstance().setOrientationsEnabled (allowedOrientations);
  361. }
  362. void setTransportControlsEnabled (bool shouldBeEnabled)
  363. {
  364. positionSlider .setEnabled (shouldBeEnabled);
  365. playSpeedComboBox.setEnabled (shouldBeEnabled);
  366. seekToStartButton.setEnabled (shouldBeEnabled);
  367. playButton .setEnabled (shouldBeEnabled);
  368. unloadButton .setEnabled (shouldBeEnabled);
  369. pauseButton .setEnabled (shouldBeEnabled);
  370. }
  371. void selectVideoFile()
  372. {
  373. fileChooser.reset (new FileChooser ("Choose a video file to open...", File::getCurrentWorkingDirectory(),
  374. "*", true));
  375. fileChooser->launchAsync (FileBrowserComponent::openMode | FileBrowserComponent::canSelectFiles,
  376. [this] (const FileChooser& chooser)
  377. {
  378. auto results = chooser.getURLResults();
  379. if (results.size() > 0)
  380. loadVideo (results[0]);
  381. });
  382. }
  383. void loadVideo (const URL& url)
  384. {
  385. unloadVideoFile();
  386. #if JUCE_IOS || JUCE_MAC
  387. askIfUseNativeControls (url);
  388. #else
  389. loadUrl (url);
  390. setupVideoComp (false);
  391. #endif
  392. }
  393. void askIfUseNativeControls (const URL& url)
  394. {
  395. auto* aw = new AlertWindow ("Choose viewer type", {}, AlertWindow::NoIcon);
  396. aw->addButton ("Yes", 1, KeyPress (KeyPress::returnKey));
  397. aw->addButton ("No", 0, KeyPress (KeyPress::escapeKey));
  398. aw->addTextBlock ("Do you want to use the viewer with native controls?");
  399. auto callback = ModalCallbackFunction::forComponent (videoViewerTypeChosen, this, url);
  400. aw->enterModalState (true, callback, true);
  401. }
  402. static void videoViewerTypeChosen (int result, VideoDemo* owner, URL url)
  403. {
  404. if (owner != nullptr)
  405. {
  406. owner->setupVideoComp (result != 0);
  407. owner->loadUrl (url);
  408. }
  409. }
  410. void setupVideoComp (bool useNativeViewerWithNativeControls)
  411. {
  412. auto* oldVideoComp = curVideoComp;
  413. if (useNativeViewerWithNativeControls)
  414. curVideoComp = &videoCompWithNativeControls;
  415. else
  416. curVideoComp = &videoCompNoNativeControls;
  417. if (isFirstSetup || oldVideoComp != curVideoComp)
  418. {
  419. oldVideoComp->onPlaybackStarted = nullptr;
  420. oldVideoComp->onPlaybackStopped = nullptr;
  421. oldVideoComp->onErrorOccurred = nullptr;
  422. oldVideoComp->setVisible (false);
  423. curVideoComp->onPlaybackStarted = [this]() { processPlaybackStarted(); };
  424. curVideoComp->onPlaybackStopped = [this]() { processPlaybackPaused(); };
  425. curVideoComp->onErrorOccurred = [this](const String& errorMessage) { errorOccurred (errorMessage); };
  426. curVideoComp->setVisible (true);
  427. #if JUCE_SYNC_VIDEO_VOLUME_WITH_OS_MEDIA_VOLUME
  428. oldVideoComp->onGlobalMediaVolumeChanged = nullptr;
  429. curVideoComp->onGlobalMediaVolumeChanged = [this]() { volumeSlider.setValue (curVideoComp->getAudioVolume(), dontSendNotification); };
  430. #endif
  431. }
  432. isFirstSetup = false;
  433. }
  434. void loadUrl (const URL& url)
  435. {
  436. curVideoComp->loadAsync (url, [this] (const URL& u, Result r) { videoLoadingFinished (u, r); });
  437. }
  438. void showVideoUrlPrompt()
  439. {
  440. auto* aw = new AlertWindow ("Enter URL for video to load", {}, AlertWindow::NoIcon);
  441. aw->addButton ("OK", 1, KeyPress (KeyPress::returnKey));
  442. aw->addButton ("Cancel", 0, KeyPress (KeyPress::escapeKey));
  443. aw->addTextEditor ("videoUrlTextEditor", "https://www.rmp-streaming.com/media/bbb-360p.mp4");
  444. auto callback = ModalCallbackFunction::forComponent (videoUrlPromptClosed, this, Component::SafePointer<AlertWindow> (aw));
  445. aw->enterModalState (true, callback, true);
  446. }
  447. static void videoUrlPromptClosed (int result, VideoDemo* owner, Component::SafePointer<AlertWindow> aw)
  448. {
  449. if (result != 0 && owner != nullptr && aw != nullptr)
  450. {
  451. auto url = aw->getTextEditorContents ("videoUrlTextEditor");
  452. if (url.isNotEmpty())
  453. owner->loadVideo (url);
  454. }
  455. }
  456. void videoLoadingFinished (const URL& url, Result result)
  457. {
  458. ignoreUnused (url);
  459. if (result.wasOk())
  460. {
  461. resized(); // update to reflect the video's aspect ratio
  462. setTransportControlsEnabled (true);
  463. currentPositionLabel.setText (getPositionString (0.0, curVideoComp->getVideoDuration()), sendNotification);
  464. positionSlider.setValue (0.0, dontSendNotification);
  465. playSpeedComboBox.setSelectedId (100, dontSendNotification);
  466. }
  467. else
  468. {
  469. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  470. "Couldn't load the file!",
  471. result.getErrorMessage());
  472. }
  473. }
  474. static String getPositionString (double playPositionSeconds, double durationSeconds)
  475. {
  476. auto positionMs = static_cast<int> (1000 * playPositionSeconds);
  477. int posMinutes = positionMs / 60000;
  478. int posSeconds = (positionMs % 60000) / 1000;
  479. int posMillis = positionMs % 1000;
  480. auto totalMs = static_cast<int> (1000 * durationSeconds);
  481. int totMinutes = totalMs / 60000;
  482. int totSeconds = (totalMs % 60000) / 1000;
  483. int totMillis = totalMs % 1000;
  484. return String::formatted ("%02d:%02d:%03d / %02d:%02d:%03d",
  485. posMinutes, posSeconds, posMillis,
  486. totMinutes, totSeconds, totMillis);
  487. }
  488. void updatePositionSliderAndLabel()
  489. {
  490. auto position = curVideoComp->getPlayPosition();
  491. auto duration = curVideoComp->getVideoDuration();
  492. currentPositionLabel.setText (getPositionString (position, duration), sendNotification);
  493. if (! positionSliderDragging)
  494. positionSlider.setValue (duration != 0 ? (position / duration) : 0.0, dontSendNotification);
  495. }
  496. void seekVideoToStart()
  497. {
  498. seekVideoToNormalisedPosition (0.0);
  499. }
  500. void seekVideoToNormalisedPosition (double normalisedPos)
  501. {
  502. normalisedPos = jlimit (0.0, 1.0, normalisedPos);
  503. auto duration = curVideoComp->getVideoDuration();
  504. auto newPos = jlimit (0.0, duration, duration * normalisedPos);
  505. curVideoComp->setPlayPosition (newPos);
  506. currentPositionLabel.setText (getPositionString (newPos, curVideoComp->getVideoDuration()), sendNotification);
  507. positionSlider.setValue (normalisedPos, dontSendNotification);
  508. }
  509. void playVideo()
  510. {
  511. curVideoComp->play();
  512. }
  513. void processPlaybackStarted()
  514. {
  515. playButton.setVisible (false);
  516. pauseButton.setVisible (true);
  517. startTimer (20);
  518. }
  519. void pauseVideo()
  520. {
  521. curVideoComp->stop();
  522. }
  523. void processPlaybackPaused()
  524. {
  525. // On seeking to a new pos, the playback may be temporarily paused.
  526. if (positionSliderDragging)
  527. return;
  528. pauseButton.setVisible (false);
  529. playButton.setVisible (true);
  530. }
  531. void errorOccurred (const String& errorMessage)
  532. {
  533. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  534. "An error has occurred",
  535. errorMessage + ", video will be unloaded.");
  536. unloadVideoFile();
  537. }
  538. void unloadVideoFile()
  539. {
  540. curVideoComp->closeVideo();
  541. setTransportControlsEnabled (false);
  542. stopTimer();
  543. pauseButton.setVisible (false);
  544. playButton.setVisible (true);
  545. currentPositionLabel.setText ("-:- / -:-", sendNotification);
  546. positionSlider.setValue (0.0, dontSendNotification);
  547. }
  548. void timerCallback() override
  549. {
  550. updatePositionSliderAndLabel();
  551. }
  552. };
  553. #endif