通用评论

index.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _slider = require("antd/es/slider");
  6. var _slider2 = _interopRequireDefault(_slider);
  7. var _icon = require("antd/es/icon");
  8. var _icon2 = _interopRequireDefault(_icon);
  9. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  10. require("antd/es/slider/style");
  11. require("antd/es/icon/style");
  12. var _react = require("react");
  13. var _react2 = _interopRequireDefault(_react);
  14. var _dayjs = require("dayjs");
  15. var _dayjs2 = _interopRequireDefault(_dayjs);
  16. var _duration = require("dayjs/plugin/duration");
  17. var _duration2 = _interopRequireDefault(_duration);
  18. var _utc = require("dayjs/plugin/utc");
  19. var _utc2 = _interopRequireDefault(_utc);
  20. require("./index.less");
  21. var _loading = require("../../assert/loading.gif");
  22. var _loading2 = _interopRequireDefault(_loading);
  23. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  24. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  25. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  26. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  27. _dayjs2.default.extend(_duration2.default);
  28. _dayjs2.default.extend(_utc2.default);
  29. var AudioPlayer = function (_React$Component) {
  30. _inherits(AudioPlayer, _React$Component);
  31. function AudioPlayer(props) {
  32. _classCallCheck(this, AudioPlayer);
  33. var _this = _possibleConstructorReturn(this, (AudioPlayer.__proto__ || Object.getPrototypeOf(AudioPlayer)).call(this, props));
  34. _this.state = {
  35. duration: 0,
  36. currentDuration: 0,
  37. isPlaying: false,
  38. isLoading: false
  39. };
  40. return _this;
  41. }
  42. _createClass(AudioPlayer, [{
  43. key: "componentDidMount",
  44. value: function componentDidMount() {
  45. var _this2 = this;
  46. if (this.player) {
  47. var src = this.props.src;
  48. this.player.oncanplay = function (event) {
  49. console.log(event, 1111);
  50. _this2.setState({ duration: event.target.duration });
  51. };
  52. this.player.onended = function (event) {
  53. console.log(event, 2222);
  54. _this2.setState({ isPlaying: false, currentDuration: 0 });
  55. };
  56. this.player.onpause = function (event) {
  57. console.log(event, 333);
  58. _this2.setState({ isPlaying: false });
  59. };
  60. this.player.onplay = function () {
  61. console.log(event, 5555);
  62. _this2.setState({ isPlaying: true, isLoading: false });
  63. };
  64. this.player.onplaying = function (event) {
  65. console.log(event, 4444);
  66. _this2.setState({ isPlaying: true, isLoading: false });
  67. };
  68. this.player.ontimeupdate = function (event) {
  69. return _this2.setState({ currentDuration: event.target.currentTime });
  70. };
  71. this.player.src = src;
  72. }
  73. }
  74. }, {
  75. key: "componentDidUpdate",
  76. value: function componentDidUpdate(prevProps) {
  77. var _this3 = this;
  78. if (this.props.src !== prevProps.src) {
  79. this.player.oncanplay = function (event) {
  80. console.log(event, 1111);
  81. _this3.setState({ duration: event.target.duration });
  82. };
  83. this.player.onended = function (event) {
  84. console.log(event, 2222);
  85. _this3.setState({ isPlaying: false, currentDuration: 0 });
  86. };
  87. this.player.onpause = function (event) {
  88. console.log(event, 333);
  89. _this3.setState({ isPlaying: false });
  90. };
  91. this.player.onplay = function () {
  92. console.log(event, 5555);
  93. _this3.setState({ isPlaying: true, isLoading: false });
  94. };
  95. this.player.onplaying = function (event) {
  96. console.log(event, 4444);
  97. _this3.setState({ isPlaying: true, isLoading: false });
  98. };
  99. this.player.ontimeupdate = function (event) {
  100. return _this3.setState({ currentDuration: event.target.currentTime });
  101. };
  102. this.player.src = this.props.src;
  103. }
  104. }
  105. }, {
  106. key: "clickPlayOrPause",
  107. value: function clickPlayOrPause(isPlaying) {
  108. if (!this.player) {
  109. return;
  110. }
  111. if (isPlaying) {
  112. this.player.pause();
  113. } else {
  114. // 如果是播放,先暂停其他的播放
  115. var playerList = document.getElementsByTagName("audio");
  116. if (playerList) {
  117. Array.from(playerList).forEach(function (player) {
  118. if (!player.paused) {
  119. player.pause();
  120. }
  121. });
  122. }
  123. this.player.play();
  124. this.setState({
  125. isLoading: true
  126. });
  127. }
  128. }
  129. }, {
  130. key: "getPlayIcon",
  131. value: function getPlayIcon() {
  132. var _state = this.state,
  133. isPlaying = _state.isPlaying,
  134. isLoading = _state.isLoading;
  135. var playIconElem = void 0;
  136. if (isLoading) {
  137. playIconElem = _react2.default.createElement("img", { className: "icon-loading", src: _loading2.default, alt: "iconLoading" });
  138. } else if (isPlaying) {
  139. playIconElem = _react2.default.createElement(_icon2.default, { className: "pause", type: "pause" });
  140. } else {
  141. playIconElem = _react2.default.createElement("i", { className: "schedule schedule-icon_image_audio" });
  142. }
  143. return playIconElem;
  144. }
  145. }, {
  146. key: "render",
  147. value: function render() {
  148. var _this4 = this;
  149. var _state2 = this.state,
  150. currentDuration = _state2.currentDuration,
  151. isPlaying = _state2.isPlaying,
  152. duration = _state2.duration;
  153. return _react2.default.createElement(
  154. "div",
  155. { className: "comment-item-speak-audio-container" },
  156. _react2.default.createElement("audio", {
  157. ref: function ref(_ref) {
  158. _this4.player = _ref;
  159. },
  160. style: { display: "none" }
  161. }),
  162. _react2.default.createElement(
  163. "span",
  164. {
  165. className: "icon",
  166. onClick: function onClick() {
  167. _this4.clickPlayOrPause(isPlaying);
  168. }
  169. },
  170. this.getPlayIcon()
  171. ),
  172. _react2.default.createElement(_slider2.default, {
  173. step: 0.001,
  174. className: "slider",
  175. tooltipVisible: false,
  176. value: currentDuration,
  177. max: duration,
  178. onChange: function onChange(value) {
  179. if (_this4.player) {
  180. _this4.player.currentTime = value;
  181. }
  182. }
  183. }),
  184. _react2.default.createElement(
  185. "span",
  186. { className: "time" },
  187. _dayjs2.default.utc(_dayjs2.default.duration(currentDuration, "seconds").asMilliseconds()).format("mm:ss"),
  188. "/",
  189. _dayjs2.default.utc(_dayjs2.default.duration(duration, "seconds").asMilliseconds()).format("mm:ss")
  190. )
  191. );
  192. }
  193. }]);
  194. return AudioPlayer;
  195. }(_react2.default.Component);
  196. exports.default = AudioPlayer;
  197. //# sourceMappingURL=index.js.map