howler.js icon indicating copy to clipboard operation
howler.js copied to clipboard

Audio doesn`t play from paused position when html5: true

Open oleksandrkryvosheiev opened this issue 8 years ago • 10 comments

Hello When I enable html5 option

this.howl = new Howl({ src: [api.getTrack + track.id], html5: true, format: ["mp3"] })

Then play howl, then pause howl, then play it once again, howl.seek() returns 0 and sound starts from beginning

Can i fix it somehow?

oleksandrkryvosheiev avatar Apr 15 '18 21:04 oleksandrkryvosheiev

I just tested this on Chrome/Firefox and couldn't reproduce the issue. Are you seeing this on some particular browser?

goldfire avatar May 04 '18 20:05 goldfire

I have same issue. Seems like in Safari it works ok, but in Chrome track always resumes from beginning.

Psirex avatar Jun 27 '18 16:06 Psirex

I have a simple React app and I'm seeing the problem on Chrome/Chromium but not on Firefox.

import React, { Component } from 'react';
import { Howl, Howler } from 'howler';

class App extends Component {

  state = {
    sound: null,
    audio: null,
    audioId: null,
    isPlaying: false,
  };

  startAudio = () => {
    if (this.state.isPlaying) {
      console.debug('Already playing!');
      console.debug(this.state.audio);
      return;
    }
    if (!this.state.audio) {
      this.setState({
        audio: new Howl({
          src: [
            'http://streaming-server',
          ],
          volume: 1,
          html5: true,
          format: ['mp3'],
          onend: () => {
            console.debug('Finished.');
            this.setState({
              isPlaying: false,
            });
            this.state.audio.seek(0, this.state.audioId);
          },
          onplay: () => {
            console.debug('Playing.');
          },
          onpause: () => {
            console.debug('Pausing.');
          },
        }),
        isPlaying: true
      }, () => {
        Howler.volume(1);
        const id = this.state.audio.play();
        console.debug(id);
        this.setState({ audioId: id });
      });
    } else {
      this.setState({ isPlaying: true }, () => {
        console.debug(this.state.audioId);
        this.state.audio.play(this.state.audioId);
      });
    }
  };

  pauseAudio = () => {
    if (!this.state.isPlaying) {
      console.debug('Not playing :S');
      console.debug(this.state.audio);
      return;
    }
    this.setState({ isPlaying: false }, () => {
      console.debug(this.state.audioId);
      console.debug(`${this.state.audio.seek(null, this.state.audioId)} s`);
      this.state.audio.pause(this.state.audioId);
    });
  };

  renderControls = () => {
    if (this.state.isPlaying) {
      return (
        <button onClick={this.pauseAudio}>Pause</button>
      );
    } else {
      return (
        <button onClick={this.startAudio}>Play</button>
      );
    }
  };

  render() {
    return (
      <div>
        {this.renderControls()}
      </div>
    );
  }
}

export default App;

Midnighter avatar Mar 27 '19 23:03 Midnighter

Closing due to inactivity. If this is still an issue, reply with more info and I'll reopen.

goldfire avatar Jun 21 '20 17:06 goldfire

@goldfire still I am facing the issue, am testing on Version 83.0.4103.116

tettusud avatar Jul 17 '20 15:07 tettusud

@goldfire It still doesn't work. Both on a PC and an Android device, setting html5: true makes seeking impossible. Please help. Version is 2.2.0 Thanks

teezzan avatar Aug 11 '20 20:08 teezzan

In my case this was caused by the fact that accidentally I was calling play() twice. When this was fixed, I was able to resume the audio from the same position when it was paused.

northern-mindset avatar Feb 07 '21 08:02 northern-mindset

This isn't an issue with Howler.

Howler attempts to assign HTMLAudioElement.currentTime to the seek value that is stored for that audio node. However it seems that on Chromium based browsers this assignment will fail if the server for the sound source you have used does not support range requests, instead it will be set to 0.

Once I added support for range requests to my server (which is serving the MP3s), this problem no longer occurred.

See more details here.

yrnehli avatar Mar 13 '21 00:03 yrnehli

I have the same issue. But if I set the server settings 'Accept-Ranges':'bytes' it works as expected. With 'Accept-Ranges':'none', the play() method starts from beginning always after pause().

yalogica avatar Apr 10 '21 12:04 yalogica

@lavrentievmx how to set 'Accept-range' to bytes

mohamadabdelhady avatar Jan 12 '22 20:01 mohamadabdelhady