通用评论

index.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. return _this2.setState({ duration: event.target.duration });
  50. };
  51. this.player.onended = function () {
  52. return _this2.setState({ isPlaying: false, currentDuration: 0 });
  53. };
  54. this.player.onpause = function () {
  55. return _this2.setState({ isPlaying: false, isLoading: false });
  56. };
  57. this.player.onplay = function () {
  58. return _this2.setState({ isPlaying: false, isLoading: true });
  59. };
  60. this.player.onplaying = function () {
  61. return _this2.setState({ isPlaying: true, isLoading: false });
  62. };
  63. this.player.ontimeupdate = function (event) {
  64. return _this2.setState({ currentDuration: event.target.currentTime });
  65. };
  66. this.player.src = src;
  67. }
  68. }
  69. }, {
  70. key: "componentDidUpdate",
  71. value: function componentDidUpdate(prevProps) {
  72. var _this3 = this;
  73. if (this.props.src !== prevProps.src) {
  74. this.player.oncanplay = function (event) {
  75. return _this3.setState({ duration: event.target.duration });
  76. };
  77. this.player.onended = function () {
  78. return _this3.setState({ isPlaying: false, currentDuration: 0 });
  79. };
  80. this.player.onpause = function () {
  81. return _this3.setState({ isPlaying: false, isLoading: false });
  82. };
  83. this.player.onplay = function () {
  84. return _this3.setState({ isPlaying: false, isLoading: true });
  85. };
  86. this.player.onplaying = function () {
  87. return _this3.setState({ isPlaying: true, isLoading: false });
  88. };
  89. this.player.ontimeupdate = function (event) {
  90. return _this3.setState({ currentDuration: event.target.currentTime });
  91. };
  92. this.player.src = this.props.src;
  93. }
  94. }
  95. }, {
  96. key: "clickPlayOrPause",
  97. value: function clickPlayOrPause(isPlaying) {
  98. if (!this.player) {
  99. return;
  100. }
  101. if (isPlaying) {
  102. this.player.pause();
  103. } else {
  104. // 如果是播放,先暂停其他的播放
  105. var playerList = document.getElementsByTagName("audio");
  106. if (playerList) {
  107. Array.from(playerList).forEach(function (player) {
  108. if (!player.paused) {
  109. player.pause();
  110. }
  111. });
  112. }
  113. this.player.play();
  114. }
  115. }
  116. }, {
  117. key: "getPlayIcon",
  118. value: function getPlayIcon() {
  119. var _state = this.state,
  120. isPlaying = _state.isPlaying,
  121. isLoading = _state.isLoading;
  122. var playIconElem = void 0;
  123. if (isLoading) {
  124. playIconElem = _react2.default.createElement("img", { className: "icon-loading", src: _loading2.default, alt: "iconLoading" });
  125. } else if (isPlaying) {
  126. playIconElem = _react2.default.createElement(_icon2.default, { className: "pause", type: "pause" });
  127. } else {
  128. playIconElem = _react2.default.createElement("i", { className: "schedule schedule-icon_image_audio" });
  129. }
  130. return playIconElem;
  131. }
  132. }, {
  133. key: "render",
  134. value: function render() {
  135. var _this4 = this;
  136. var _state2 = this.state,
  137. currentDuration = _state2.currentDuration,
  138. isPlaying = _state2.isPlaying,
  139. duration = _state2.duration;
  140. return _react2.default.createElement(
  141. "div",
  142. { className: "comment-item-speak-audio-container" },
  143. _react2.default.createElement("audio", {
  144. ref: function ref(_ref) {
  145. _this4.player = _ref;
  146. },
  147. style: { display: "none" }
  148. }),
  149. _react2.default.createElement(
  150. "span",
  151. {
  152. className: "icon",
  153. onClick: function onClick() {
  154. _this4.clickPlayOrPause(isPlaying);
  155. }
  156. },
  157. this.getPlayIcon()
  158. ),
  159. _react2.default.createElement(_slider2.default, {
  160. step: 0.001,
  161. className: "slider",
  162. tooltipVisible: false,
  163. value: currentDuration,
  164. max: duration,
  165. onChange: function onChange(value) {
  166. if (_this4.player) {
  167. _this4.player.currentTime = value;
  168. }
  169. }
  170. }),
  171. _react2.default.createElement(
  172. "span",
  173. { className: "time" },
  174. _dayjs2.default.utc(_dayjs2.default.duration(currentDuration, "seconds").asMilliseconds()).format("mm:ss"),
  175. "/",
  176. _dayjs2.default.utc(_dayjs2.default.duration(duration, "seconds").asMilliseconds()).format("mm:ss")
  177. )
  178. );
  179. }
  180. }]);
  181. return AudioPlayer;
  182. }(_react2.default.Component);
  183. exports.default = AudioPlayer;
  184. //# sourceMappingURL=index.js.map