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 #ifndef AUDIO_PLAYER_H 17 #define AUDIO_PLAYER_H 18 19 #include <pthread.h> 20 #include "jsi.h" 21 #include "player.h" 22 23 namespace OHOS { 24 namespace ACELite { 25 class AudioPlayer; 26 27 class AudioEventListener { 28 public: 29 AudioEventListener() = default; 30 31 explicit AudioEventListener(JSIValue callback); 32 33 ~AudioEventListener(); 34 35 JSIValue GetCallback() const; 36 37 void OnTrigger() const; 38 39 private: 40 JSIValue callback_; 41 }; 42 43 void TriggerEventListener(const AudioEventListener *listener); 44 void AsyncExecuteCallback(void *arg); 45 46 class AudioPlayerCallback : public Media::PlayerCallback { 47 public: 48 AudioPlayerCallback() = delete; 49 50 explicit AudioPlayerCallback(AudioPlayer *audioPlayer); 51 52 virtual ~AudioPlayerCallback() = default; 53 54 void OnPlaybackComplete() override; 55 56 void OnError(int32_t errorType, int32_t errorCode) override; 57 58 void OnInfo(int type, int extra) override; 59 60 void OnVideoSizeChanged(int width, int height) override; 61 62 void OnRewindToComplete() override; 63 64 private: 65 static void *PlaybackCompleteHandler(void *arg); 66 67 AudioPlayer *audioPlayer_; 68 }; 69 70 class AudioPlayer { 71 public: 72 static AudioPlayer *GetInstance(); 73 74 void ForkUpdateTimeThread(); 75 76 void StopUpdateTimeThread(); 77 78 bool CreatePlayer(); 79 80 bool DestoryPlayer(); 81 82 bool ResetPlayer(); 83 84 void ReleaseEventListeners(); 85 86 void ReleaseSrc(); 87 88 bool Play(); 89 90 bool Pause(); 91 92 bool Stop(); 93 94 char *GetSrc() const; 95 96 double GetCurrentTime() const; 97 98 double GetDuration() const; 99 100 bool GetAutoPlay() const; 101 102 bool IsLooping() const; 103 104 bool IsMuted() const; 105 106 const char *GetStatus() const; 107 108 double GetVolume() const; 109 110 bool IsPlaying() const; 111 112 bool SetSrc(char *src); 113 114 bool SetCurrentTime(double currentTime) const; 115 116 bool SetAutoPlay(bool autoPlay); 117 118 bool SetLoop(bool loop) const; 119 120 bool SetVolume(double volume); 121 122 bool SetMuted(bool muted); 123 124 const AudioEventListener *GetOnPlayListener() const; 125 126 const AudioEventListener *GetOnPauseListener() const; 127 128 const AudioEventListener *GetOnStopListener() const; 129 130 const AudioEventListener *GetOnLoadedDataListener() const; 131 132 const AudioEventListener *GetOnEndedListener() const; 133 134 const AudioEventListener *GetOnErrorListener() const; 135 136 const AudioEventListener *GetOnTimeUpdateListener() const; 137 138 void SetOnPlayListener(AudioEventListener *onPlayListener); 139 140 void SetOnPauseListener(AudioEventListener *onPauseListener); 141 142 void SetOnStopListener(AudioEventListener *onStopListener); 143 144 void SetOnLoadedDataListener(AudioEventListener *onLoadedDataListener); 145 146 void SetOnEndedListener(AudioEventListener *onEndedListener); 147 148 void SetOnErrorListener(AudioEventListener *onErrorListener); 149 150 void SetOnTimeUpdateListener(AudioEventListener *onTimeUpdateListener); 151 152 private: 153 AudioPlayer(); 154 155 ~AudioPlayer() = default; 156 157 static pthread_mutex_t lock_; 158 static pthread_cond_t condition_; 159 static void *UpdateTimeHandler(void *arg); 160 161 Media::Player *player_; 162 std::shared_ptr<AudioPlayerCallback> callback_; 163 AudioEventListener *onPlayListener_; 164 AudioEventListener *onPauseListener_; 165 AudioEventListener *onStopListener_; 166 AudioEventListener *onLoadedDataListener_; 167 AudioEventListener *onEndedListener_; 168 AudioEventListener *onErrorListener_; 169 AudioEventListener *onTimeUpdateListener_; 170 char *src_; 171 char *status_; 172 double volume_; 173 bool autoPlay_; 174 bool muted_; 175 bool isRunning_; 176 }; 177 } // namespace ACELite 178 } // namespace OHOS 179 #endif // AUDIO_PLAYER_H