How to fix audio play() in React for iOS?












0















I am making a timer app in React. Whenever the timer counts back to 0 it should play a wav sound file. It works fine on IE, Chrome, Edge, Android, but not on iOS.



I have found an explanation why this is happening, but not how to work around it.




In Safari on iOS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, preload and autoplay are disabled. No data is loaded until the user initiates it. This means the JavaScript play() and load() methods are also inactive until the user initiates playback, unless the play() or load() method is triggered by user action. In other words, a user-initiated Play button works, but an onLoad="play()" event does not. (Source: Apple Developer)




 <audio id="beep" src="http://www.pacdv.com/sounds/interface_sound_effects/sound106.wav"></audio>

startTimer = () => {
document.getElementById('beep').play();
}


The start timer function runs when the timer reaches 0, not on a click event. Expected result would be on iOS to play the audio when counts back to 0.










share|improve this question

























  • Not sure if this will work, but you could make an invisible button which plays the audio in its onClick and then try to invoke the onClick() function of this invisible button in your startTimer() function. This is just a theory, not sure if it will work as I haven't tested it

    – Pranay
    Jan 2 at 18:12











  • I couldn't make it work like this.

    – Gabriella Csernus
    Jan 3 at 11:43











  • Looks like there's no workaround for this problem. See this SO post:stackoverflow.com/a/31777081/9064259. I skimmed through the IBM document there. Give a read to the Autoplay section on page 6

    – Pranay
    Jan 3 at 17:10
















0















I am making a timer app in React. Whenever the timer counts back to 0 it should play a wav sound file. It works fine on IE, Chrome, Edge, Android, but not on iOS.



I have found an explanation why this is happening, but not how to work around it.




In Safari on iOS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, preload and autoplay are disabled. No data is loaded until the user initiates it. This means the JavaScript play() and load() methods are also inactive until the user initiates playback, unless the play() or load() method is triggered by user action. In other words, a user-initiated Play button works, but an onLoad="play()" event does not. (Source: Apple Developer)




 <audio id="beep" src="http://www.pacdv.com/sounds/interface_sound_effects/sound106.wav"></audio>

startTimer = () => {
document.getElementById('beep').play();
}


The start timer function runs when the timer reaches 0, not on a click event. Expected result would be on iOS to play the audio when counts back to 0.










share|improve this question

























  • Not sure if this will work, but you could make an invisible button which plays the audio in its onClick and then try to invoke the onClick() function of this invisible button in your startTimer() function. This is just a theory, not sure if it will work as I haven't tested it

    – Pranay
    Jan 2 at 18:12











  • I couldn't make it work like this.

    – Gabriella Csernus
    Jan 3 at 11:43











  • Looks like there's no workaround for this problem. See this SO post:stackoverflow.com/a/31777081/9064259. I skimmed through the IBM document there. Give a read to the Autoplay section on page 6

    – Pranay
    Jan 3 at 17:10














0












0








0








I am making a timer app in React. Whenever the timer counts back to 0 it should play a wav sound file. It works fine on IE, Chrome, Edge, Android, but not on iOS.



I have found an explanation why this is happening, but not how to work around it.




In Safari on iOS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, preload and autoplay are disabled. No data is loaded until the user initiates it. This means the JavaScript play() and load() methods are also inactive until the user initiates playback, unless the play() or load() method is triggered by user action. In other words, a user-initiated Play button works, but an onLoad="play()" event does not. (Source: Apple Developer)




 <audio id="beep" src="http://www.pacdv.com/sounds/interface_sound_effects/sound106.wav"></audio>

startTimer = () => {
document.getElementById('beep').play();
}


The start timer function runs when the timer reaches 0, not on a click event. Expected result would be on iOS to play the audio when counts back to 0.










share|improve this question
















I am making a timer app in React. Whenever the timer counts back to 0 it should play a wav sound file. It works fine on IE, Chrome, Edge, Android, but not on iOS.



I have found an explanation why this is happening, but not how to work around it.




In Safari on iOS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, preload and autoplay are disabled. No data is loaded until the user initiates it. This means the JavaScript play() and load() methods are also inactive until the user initiates playback, unless the play() or load() method is triggered by user action. In other words, a user-initiated Play button works, but an onLoad="play()" event does not. (Source: Apple Developer)




 <audio id="beep" src="http://www.pacdv.com/sounds/interface_sound_effects/sound106.wav"></audio>

startTimer = () => {
document.getElementById('beep').play();
}


The start timer function runs when the timer reaches 0, not on a click event. Expected result would be on iOS to play the audio when counts back to 0.







ios reactjs audio playback






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 18:13









shim

4,10664779




4,10664779










asked Jan 2 at 17:43









Gabriella CsernusGabriella Csernus

5210




5210













  • Not sure if this will work, but you could make an invisible button which plays the audio in its onClick and then try to invoke the onClick() function of this invisible button in your startTimer() function. This is just a theory, not sure if it will work as I haven't tested it

    – Pranay
    Jan 2 at 18:12











  • I couldn't make it work like this.

    – Gabriella Csernus
    Jan 3 at 11:43











  • Looks like there's no workaround for this problem. See this SO post:stackoverflow.com/a/31777081/9064259. I skimmed through the IBM document there. Give a read to the Autoplay section on page 6

    – Pranay
    Jan 3 at 17:10



















  • Not sure if this will work, but you could make an invisible button which plays the audio in its onClick and then try to invoke the onClick() function of this invisible button in your startTimer() function. This is just a theory, not sure if it will work as I haven't tested it

    – Pranay
    Jan 2 at 18:12











  • I couldn't make it work like this.

    – Gabriella Csernus
    Jan 3 at 11:43











  • Looks like there's no workaround for this problem. See this SO post:stackoverflow.com/a/31777081/9064259. I skimmed through the IBM document there. Give a read to the Autoplay section on page 6

    – Pranay
    Jan 3 at 17:10

















Not sure if this will work, but you could make an invisible button which plays the audio in its onClick and then try to invoke the onClick() function of this invisible button in your startTimer() function. This is just a theory, not sure if it will work as I haven't tested it

– Pranay
Jan 2 at 18:12





Not sure if this will work, but you could make an invisible button which plays the audio in its onClick and then try to invoke the onClick() function of this invisible button in your startTimer() function. This is just a theory, not sure if it will work as I haven't tested it

– Pranay
Jan 2 at 18:12













I couldn't make it work like this.

– Gabriella Csernus
Jan 3 at 11:43





I couldn't make it work like this.

– Gabriella Csernus
Jan 3 at 11:43













Looks like there's no workaround for this problem. See this SO post:stackoverflow.com/a/31777081/9064259. I skimmed through the IBM document there. Give a read to the Autoplay section on page 6

– Pranay
Jan 3 at 17:10





Looks like there's no workaround for this problem. See this SO post:stackoverflow.com/a/31777081/9064259. I skimmed through the IBM document there. Give a read to the Autoplay section on page 6

– Pranay
Jan 3 at 17:10












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010828%2fhow-to-fix-audio-play-in-react-for-ios%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010828%2fhow-to-fix-audio-play-in-react-for-ios%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter