当用户离开/刷新页面时,如何添加一个监听音频当前播放时间的功能?

问题描述

我已将音频插入我的React网页。我正在尝试记录用户离开页面时音频的当前音频播放时间(例如刷新/跳转到另一个页面),以便当用户重新访问页面时音频可以从该点重新开始。我的理解是,我需要添加一个事件侦听器,或使用诸如componentWillUnmount之类的东西。谁可以帮我这个事?谢谢。

// @flow
import React,{ Component } from "react";

export class Audio extends React.Component {
  constructor(props) {
    super(props);
    this.state = { time: 0 };
  }

  render() {
    return (
      <audio
        loop
        autoPlay
        currentTime={this.state.time}
        className="backgroundAudio"
        pause={this._onPause}
      >
        <source
          src="example.mp3"
          type="audio/mpeg"
        />
        Your browser does not support the audio element.
      </audio>
    );
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)