通用评论

index.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  23. 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; }
  24. 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; }
  25. _dayjs2.default.extend(_duration2.default);
  26. _dayjs2.default.extend(_utc2.default);
  27. var AudioPlayer = function (_React$Component) {
  28. _inherits(AudioPlayer, _React$Component);
  29. function AudioPlayer(props) {
  30. _classCallCheck(this, AudioPlayer);
  31. var _this = _possibleConstructorReturn(this, (AudioPlayer.__proto__ || Object.getPrototypeOf(AudioPlayer)).call(this, props));
  32. _this.state = {
  33. duration: 0,
  34. currentDuration: 0,
  35. isPlaying: false
  36. };
  37. return _this;
  38. }
  39. _createClass(AudioPlayer, [{
  40. key: "componentDidMount",
  41. value: function componentDidMount() {
  42. var _this2 = this;
  43. if (this.player) {
  44. var src = this.props.src;
  45. this.player.oncanplay = function (event) {
  46. return _this2.setState({ duration: event.target.duration });
  47. };
  48. this.player.onended = function () {
  49. return _this2.setState({ isPlaying: false, currentDuration: 0 });
  50. };
  51. this.player.onpause = function () {
  52. return _this2.setState({ isPlaying: false });
  53. };
  54. this.player.onplay = function () {
  55. return _this2.setState({ isPlaying: true });
  56. };
  57. this.player.ontimeupdate = function (event) {
  58. return _this2.setState({ currentDuration: event.target.currentTime });
  59. };
  60. this.player.src = src;
  61. }
  62. }
  63. }, {
  64. key: "componentDidUpdate",
  65. value: function componentDidUpdate(prevProps) {
  66. var _this3 = this;
  67. if (this.props.src !== prevProps.src) {
  68. this.player.oncanplay = function (event) {
  69. return _this3.setState({ duration: event.target.duration });
  70. };
  71. this.player.onended = function () {
  72. return _this3.setState({ isPlaying: false, currentDuration: 0 });
  73. };
  74. this.player.onpause = function () {
  75. return _this3.setState({ isPlaying: false });
  76. };
  77. this.player.onplay = function () {
  78. return _this3.setState({ isPlaying: true });
  79. };
  80. this.player.ontimeupdate = function (event) {
  81. return _this3.setState({ currentDuration: event.target.currentTime });
  82. };
  83. this.player.src = this.props.src;
  84. }
  85. }
  86. }, {
  87. key: "clickPlayOrPause",
  88. value: function clickPlayOrPause(isPlaying) {
  89. if (!this.player) {
  90. return;
  91. }
  92. if (isPlaying) {
  93. this.player.pause();
  94. } else {
  95. // 如果是播放,先暂停其他的播放
  96. var playerList = document.getElementsByTagName("audio");
  97. if (playerList) {
  98. Array.from(playerList).forEach(function (player) {
  99. if (!player.paused) {
  100. player.pause();
  101. }
  102. });
  103. }
  104. this.player.play();
  105. }
  106. }
  107. }, {
  108. key: "render",
  109. value: function render() {
  110. var _this4 = this;
  111. var _state = this.state,
  112. currentDuration = _state.currentDuration,
  113. isPlaying = _state.isPlaying,
  114. duration = _state.duration;
  115. return _react2.default.createElement(
  116. "div",
  117. { className: "comment-item-speak-audio-container" },
  118. _react2.default.createElement("audio", {
  119. ref: function ref(_ref) {
  120. _this4.player = _ref;
  121. },
  122. style: { display: "none" }
  123. }),
  124. _react2.default.createElement(
  125. "span",
  126. {
  127. className: "icon",
  128. onClick: function onClick() {
  129. _this4.clickPlayOrPause(isPlaying);
  130. }
  131. },
  132. isPlaying ? _react2.default.createElement(_icon2.default, { className: "pause", type: "pause" }) : _react2.default.createElement("i", { className: "schedule schedule-icon_image_audio" })
  133. ),
  134. _react2.default.createElement(_slider2.default, {
  135. step: 0.001,
  136. className: "slider",
  137. tooltipVisible: false,
  138. value: currentDuration,
  139. max: duration,
  140. onChange: function onChange(value) {
  141. if (_this4.player) {
  142. _this4.player.currentTime = value;
  143. }
  144. }
  145. }),
  146. _react2.default.createElement(
  147. "span",
  148. { className: "time" },
  149. _dayjs2.default.utc(_dayjs2.default.duration(currentDuration, "seconds").asMilliseconds()).format("mm:ss"),
  150. "/",
  151. _dayjs2.default.utc(_dayjs2.default.duration(duration, "seconds").asMilliseconds()).format("mm:ss")
  152. )
  153. );
  154. }
  155. }]);
  156. return AudioPlayer;
  157. }(_react2.default.Component);
  158. exports.default = AudioPlayer;
  159. //# sourceMappingURL=index.js.map