2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2023-12-08 00:13:39 +08:00
|
|
|
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#pragma once
|
|
|
|
|
2024-04-24 22:28:59 +08:00
|
|
|
#if defined(AX_ENABLE_MEDIA)
|
|
|
|
|
|
|
|
# include "UIButton.h"
|
|
|
|
# include "ui/UIWidget.h"
|
|
|
|
# include "ui/UILayout.h"
|
|
|
|
# include "2d/Sprite.h"
|
|
|
|
# include <chrono>
|
2022-02-24 17:57:08 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
# if AX_VIDEOPLAYER_DEBUG_DRAW
|
2023-06-11 13:08:08 +08:00
|
|
|
# include "2d/DrawNode.h"
|
2022-02-23 18:39:02 +08:00
|
|
|
# endif
|
|
|
|
|
2022-04-26 15:16:02 +08:00
|
|
|
# undef ERROR
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup ui
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-04-24 22:28:59 +08:00
|
|
|
class MediaPlayer;
|
|
|
|
|
|
|
|
class AX_GUI_DLL MediaController : public ax::ui::Widget
|
|
|
|
{
|
|
|
|
public:
|
2024-05-21 23:41:02 +08:00
|
|
|
enum class Orientation
|
|
|
|
{
|
|
|
|
Default,
|
|
|
|
RotatedLeft,
|
|
|
|
RotatedRight,
|
|
|
|
};
|
|
|
|
|
2024-04-24 22:28:59 +08:00
|
|
|
explicit MediaController(MediaPlayer* player) : _mediaPlayer(player) {}
|
|
|
|
~MediaController() override = 0;
|
|
|
|
|
|
|
|
virtual void updateControllerState() = 0;
|
2024-04-25 23:20:41 +08:00
|
|
|
virtual void setTimelineBarHeight(float height) = 0;
|
2024-04-24 22:28:59 +08:00
|
|
|
|
2024-05-21 23:41:02 +08:00
|
|
|
void setOrientation(Orientation orientation);
|
|
|
|
Orientation getOrientation() const { return _orientation; }
|
|
|
|
|
2024-04-24 22:28:59 +08:00
|
|
|
protected:
|
|
|
|
MediaPlayer* _mediaPlayer = nullptr;
|
2024-05-21 23:41:02 +08:00
|
|
|
Orientation _orientation = Orientation::Default;
|
2024-04-24 22:28:59 +08:00
|
|
|
};
|
|
|
|
inline MediaController::~MediaController() = default; // Required since the destructor is pure virtual
|
|
|
|
|
|
|
|
class MediaPlayerControl : public ax::ui::Button
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static MediaPlayerControl* create(SpriteFrame* frame);
|
|
|
|
|
|
|
|
MediaPlayerControl() = default;
|
|
|
|
~MediaPlayerControl() override;
|
|
|
|
|
|
|
|
virtual bool init(SpriteFrame* frame);
|
|
|
|
|
|
|
|
void onSizeChanged() override;
|
|
|
|
Vec2 getVirtualRendererSize() const override;
|
|
|
|
Vec2 getNormalSize() const override;
|
|
|
|
|
|
|
|
void onPressStateChangedToNormal() override;
|
|
|
|
void onPressStateChangedToPressed() override;
|
|
|
|
void onPressStateChangedToDisabled() override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Sprite* _overlay = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AX_GUI_DLL BasicMediaController : public MediaController
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit BasicMediaController(MediaPlayer* player);
|
|
|
|
|
|
|
|
static BasicMediaController* create(MediaPlayer* mediaPlayer);
|
|
|
|
|
|
|
|
bool init() override;
|
|
|
|
void initRenderer() override;
|
|
|
|
|
|
|
|
void onPressStateChangedToPressed() override;
|
|
|
|
void setContentSize(const Vec2& contentSize) override;
|
|
|
|
void update(float delta) override;
|
|
|
|
void onEnter() override;
|
|
|
|
void setGlobalZOrder(float globalZOrder) override;
|
|
|
|
|
|
|
|
void updateControllerState() override;
|
2024-04-25 23:20:41 +08:00
|
|
|
void setTimelineBarHeight(float height) override;
|
2024-04-24 22:28:59 +08:00
|
|
|
|
|
|
|
virtual void createControls();
|
|
|
|
virtual void updateControlsGlobalZ(float globalZOrder);
|
|
|
|
virtual void updateControls();
|
|
|
|
virtual void updateControlsForContentSize(const Vec2& contentSize);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Widget* _controlPanel = nullptr;
|
|
|
|
|
|
|
|
MediaPlayerControl* _fullScreenEnterButton = nullptr;
|
|
|
|
MediaPlayerControl* _fullScreenExitButton = nullptr;
|
|
|
|
MediaPlayerControl* _playButton = nullptr;
|
|
|
|
MediaPlayerControl* _stopButton = nullptr;
|
|
|
|
MediaPlayerControl* _pauseButton = nullptr;
|
|
|
|
|
|
|
|
Sprite* _timelineSelector = nullptr;
|
|
|
|
Sprite* _timelineTotal = nullptr;
|
|
|
|
Sprite* _timelinePlayed = nullptr;
|
|
|
|
Layout* _mediaOverlay = nullptr;
|
|
|
|
Widget* _primaryButtonPanel = nullptr;
|
|
|
|
|
|
|
|
EventListenerTouchOneByOne* _timelineTouchListener = nullptr;
|
|
|
|
float _playRate = 1.f;
|
|
|
|
std::chrono::steady_clock::time_point _lastTouch;
|
2024-04-25 23:20:41 +08:00
|
|
|
bool _controlsReady = false;
|
|
|
|
float _timelineBarHeight;
|
2024-04-24 22:28:59 +08:00
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
2023-06-10 22:05:49 +08:00
|
|
|
* @class MediaPlayer
|
|
|
|
* @brief Play a media file.
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2023-06-10 22:05:49 +08:00
|
|
|
* @note MediaPlayer play a media file base on system widget.
|
|
|
|
* It's mean MediaPlayer play a media file above all graphical elements of axmol.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2023-06-10 22:05:49 +08:00
|
|
|
class AX_GUI_DLL MediaPlayer : public ax::ui::Widget
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
public:
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Videoplayer play event type.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
enum class EventType
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
PLAYING = 0,
|
|
|
|
PAUSED,
|
|
|
|
STOPPED,
|
|
|
|
COMPLETED,
|
2024-04-25 23:20:41 +08:00
|
|
|
ERROR,
|
|
|
|
FULLSCREEN_SWITCH
|
2021-12-25 10:04:45 +08:00
|
|
|
};
|
|
|
|
|
2024-04-24 22:28:59 +08:00
|
|
|
enum class MediaState
|
|
|
|
{
|
|
|
|
CLOSED = 0,
|
|
|
|
LOADING,
|
|
|
|
PLAYING,
|
|
|
|
PAUSED,
|
|
|
|
STOPPED,
|
|
|
|
FINISHED,
|
|
|
|
ERROR
|
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
|
|
|
* Styles of how the the video player is presented
|
|
|
|
* For now only used on iOS to use either MPMovieControlStyleEmbedded (DEFAULT) or
|
|
|
|
* MPMovieControlStyleNone (NONE)
|
|
|
|
*/
|
|
|
|
enum class StyleType
|
|
|
|
{
|
|
|
|
DEFAULT = 0,
|
|
|
|
NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2023-06-10 22:05:49 +08:00
|
|
|
* A callback which will be called after specific MediaPlayer event happens.
|
2021-12-25 10:04:45 +08:00
|
|
|
*/
|
2024-05-03 22:15:08 +08:00
|
|
|
typedef std::function<void(Object*, MediaPlayer::EventType)> ccVideoPlayerCallback;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/**
|
2023-06-10 22:05:49 +08:00
|
|
|
*Static create method for instancing a MediaPlayer.
|
2021-12-25 10:04:45 +08:00
|
|
|
*/
|
2023-06-10 22:05:49 +08:00
|
|
|
CREATE_FUNC(MediaPlayer);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-04-24 22:28:59 +08:00
|
|
|
bool init() override;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
2023-06-10 22:05:49 +08:00
|
|
|
* Sets a file path as a video source for MediaPlayer.
|
2021-12-25 10:04:45 +08:00
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
virtual void setFileName(std::string_view videoPath);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the local video file name.
|
|
|
|
*
|
|
|
|
* @return The video file name.
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
virtual std::string_view getFileName() const { return _videoURL; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
2023-06-10 22:05:49 +08:00
|
|
|
* Sets a URL as a video source for MediaPlayer.
|
2021-12-25 10:04:45 +08:00
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
virtual void setURL(std::string_view _videoURL);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
|
|
|
* @brief Get the URL of remoting video source.
|
|
|
|
*
|
|
|
|
* @return A remoting URL address.
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
virtual std::string_view getURL() const { return _videoURL; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
|
|
|
* @brief Set if playback is done in loop mode
|
|
|
|
*
|
|
|
|
* @param looping the video will or not automatically restart at the end
|
|
|
|
*/
|
|
|
|
virtual void setLooping(bool looping);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
|
|
|
* Set if the player will enable user input for basic pause and resume of video
|
|
|
|
*
|
|
|
|
* @param enableInput If true, input will be handled for basic functionality (pause/resume)
|
|
|
|
*/
|
|
|
|
virtual void setUserInputEnabled(bool enableInput);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
|
|
|
* Set the style of the player
|
|
|
|
*
|
|
|
|
* @param style The corresponding style
|
|
|
|
*/
|
|
|
|
virtual void setStyle(StyleType style);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-04-28 12:14:08 +08:00
|
|
|
/**
|
2023-06-01 12:48:40 +08:00
|
|
|
* Sets play rate, currently, only supported by windows
|
2022-04-28 12:14:08 +08:00
|
|
|
*
|
|
|
|
* @param fRate the rate for play, default 1.0
|
|
|
|
*/
|
|
|
|
virtual void setPlayRate(float fRate);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
|
|
|
* Starts playback.
|
|
|
|
*/
|
|
|
|
virtual void play();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pauses playback.
|
|
|
|
*/
|
2024-04-24 22:28:59 +08:00
|
|
|
void pause() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resumes playback.
|
|
|
|
*/
|
2024-04-24 22:28:59 +08:00
|
|
|
void resume() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops playback.
|
|
|
|
*/
|
|
|
|
virtual void stop();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Seeks to specified time position.
|
|
|
|
*
|
|
|
|
* @param sec The offset in seconds from the start to seek to.
|
|
|
|
*/
|
|
|
|
virtual void seekTo(float sec);
|
|
|
|
|
2024-04-24 22:28:59 +08:00
|
|
|
/**
|
|
|
|
* Gets the current media position.
|
|
|
|
*
|
|
|
|
* @return float The current position in seconds
|
|
|
|
*/
|
|
|
|
virtual float getCurrentTime();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets total video duration
|
|
|
|
*
|
|
|
|
* @return float The duration in seconds
|
|
|
|
*/
|
|
|
|
virtual float getDuration();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
2023-06-10 22:05:49 +08:00
|
|
|
* Checks whether the MediaPlayer is playing.
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
|
|
|
* @return True if currently playing, false otherwise.
|
|
|
|
*/
|
|
|
|
virtual bool isPlaying() const;
|
|
|
|
|
|
|
|
/**
|
2023-06-10 22:05:49 +08:00
|
|
|
* Checks whether the MediaPlayer is set with looping mode.
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
|
|
|
* @return true if the videoplayer is set to loop, false otherwise.
|
|
|
|
*/
|
|
|
|
virtual bool isLooping() const;
|
|
|
|
|
|
|
|
/**
|
2023-06-10 22:05:49 +08:00
|
|
|
* Checks whether the MediaPlayer is set to listen user input to resume and pause the video
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
|
|
|
* @return true if the videoplayer user input is set, false otherwise.
|
|
|
|
*/
|
|
|
|
virtual bool isUserInputEnabled() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Causes the video player to keep aspect ratio or no when displaying the video.
|
|
|
|
*
|
|
|
|
* @param enable Specify true to keep aspect ratio or false to scale the video until
|
|
|
|
* both dimensions fit the visible bounds of the view exactly.
|
|
|
|
*/
|
|
|
|
virtual void setKeepAspectRatioEnabled(bool enable);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether the video player keep aspect ratio when displaying the video.
|
|
|
|
*/
|
|
|
|
virtual bool isKeepAspectRatioEnabled() const { return _keepAspectRatioEnabled; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Causes the video player to enter or exit full-screen mode.
|
|
|
|
*
|
|
|
|
* @param fullscreen Specify true to enter full-screen mode or false to exit full-screen mode.
|
|
|
|
*/
|
|
|
|
virtual void setFullScreenEnabled(bool fullscreen);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether the video player is in full-screen mode.
|
|
|
|
*
|
|
|
|
* @return True if the video player is in full-screen mode, false otherwise.
|
|
|
|
*/
|
|
|
|
virtual bool isFullScreenEnabled() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a callback to be invoked when the video state is updated.
|
|
|
|
*
|
|
|
|
* @param callback The callback that will be run.
|
|
|
|
*/
|
2023-06-10 22:05:49 +08:00
|
|
|
virtual void addEventListener(const MediaPlayer::ccVideoPlayerCallback& callback);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief A function which will be called when video is playing.
|
|
|
|
*
|
2023-06-10 22:05:49 +08:00
|
|
|
* @param event @see MediaPlayer::EventType.
|
2021-12-25 10:04:45 +08:00
|
|
|
*/
|
|
|
|
virtual void onPlayEvent(int event);
|
2024-04-25 23:20:41 +08:00
|
|
|
|
2024-04-24 22:28:59 +08:00
|
|
|
void setVisible(bool visible) override;
|
|
|
|
void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override;
|
|
|
|
void onEnter() override;
|
|
|
|
void onExit() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-02-24 17:57:08 +08:00
|
|
|
void setContentSize(const Size& contentSize) override;
|
|
|
|
|
2024-04-24 22:28:59 +08:00
|
|
|
/**
|
|
|
|
* @brief Get current state of the media
|
|
|
|
*
|
|
|
|
* @return MediaState
|
|
|
|
*/
|
|
|
|
MediaState getState() const;
|
|
|
|
|
|
|
|
void setMediaController(MediaController* controller);
|
2024-04-25 23:20:41 +08:00
|
|
|
MediaController* getMediaController() const { return _mediaController; }
|
2024-04-24 22:28:59 +08:00
|
|
|
|
2023-06-10 22:05:49 +08:00
|
|
|
MediaPlayer();
|
2024-04-24 22:28:59 +08:00
|
|
|
~MediaPlayer() override;
|
2022-03-18 21:46:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
protected:
|
2024-04-24 22:28:59 +08:00
|
|
|
ax::ui::Widget* createCloneInstance() override;
|
|
|
|
void copySpecialProperties(Widget* model) override;
|
|
|
|
virtual void updateMediaController();
|
2024-04-25 23:20:41 +08:00
|
|
|
void sendEvent(int event);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
# if AX_VIDEOPLAYER_DEBUG_DRAW
|
2021-12-25 10:04:45 +08:00
|
|
|
DrawNode* _debugDrawNode;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
enum class Source
|
|
|
|
{
|
|
|
|
FILENAME = 0,
|
|
|
|
URL
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
bool _isPlaying = false;
|
|
|
|
bool _isLooping = false;
|
|
|
|
bool _fullScreenDirty = false;
|
|
|
|
bool _fullScreenEnabled = false;
|
|
|
|
bool _keepAspectRatioEnabled = false;
|
2024-04-24 22:28:59 +08:00
|
|
|
bool _userInputEnabled = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
StyleType _styleType = StyleType::DEFAULT;
|
|
|
|
|
|
|
|
std::string _videoURL;
|
|
|
|
Source _videoSource;
|
|
|
|
|
|
|
|
int _videoPlayerIndex = -1;
|
|
|
|
ccVideoPlayerCallback _eventCallback = nullptr;
|
|
|
|
|
2022-02-23 17:38:21 +08:00
|
|
|
void* _videoContext = nullptr;
|
2024-04-24 22:28:59 +08:00
|
|
|
|
|
|
|
MediaController* _mediaController = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
};
|
2023-06-10 22:05:49 +08:00
|
|
|
using VideoPlayer = MediaPlayer;
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace ui
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2019-11-23 20:27:39 +08:00
|
|
|
// end group
|
|
|
|
/// @}
|
|
|
|
#endif
|