Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

embed audio that can be listened to only once

  • Affenbrotbaum
  • Affenbrotbaum's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 4 months ago - 2 years 4 months ago #232072 by Affenbrotbaum
embed audio that can be listened to only once was created by Affenbrotbaum
Please help us help you and fill where relevant:
Your LimeSurvey version: Version 3.26.1+210427
Own server or LimeSurvey hosting: Hosted by educational institution
Survey theme/template:
==================
Hi there,

As part of a regular single-choice question, I would like users to be able to play an embedded audio file, which they then answer a question about.
I have managed to embed the file using this code:

Code:
<p>In the following recording, is the word <strong>putting </strong>pronounced as <strong>putt<u>ing</u> </strong>[ɪŋ] or as <strong>putt<u>in'</u></strong> [ɪn]?</p>
 
<p style="text-align: left;">
<audio controls="controls"><source src="/upload/surveys/184592/files/michaela_ING_orig_sent_1_final.wav" type="audio/mp3" /></audio>
</p>


However, I would to like to adapt it so that participants can start the audio but can neither pause nor replay it. So, an autoplay solution would not be my preference.

I have found this snippet of code (taken from: Link ), which creates a seperate button and a counter that reduces the number of replays every time a user has listened to the audio in full. The code is written for video files and I have only tried it with MP4 so far. Ultimately, I need code that lets me embed audio though.

Inserted before the actual vid element, whose code has been slightly modified by adding
Code:
id="Mimmi" and by deleting the controls= snippet.
Code:
<button id="counter" onclick="document.getElementById('Mimmi').play(); return false;" style="border: solid black 1px"> </button>
 
<script>
let vidcount = 3; // CHANGE THIS NUMBER AS NEEDED
$(document).ready(function() {
document.getElementById("counter").innerHTML = "Click. "+vidcount+" times left.";
var vid = document.getElementById("Mimmi");
vid.onEnded = function() {
vidcount = vidcount-1;
if (vidcount > 0) {
document.getElementById("counter").innerHTML = vidcount+" times left.";
} else {
$('#ls-button-submit').show().trigger('click');
}
}
});
</script>


Unfortunately, while the button is created and the controls are hidden, the counter doesn't seem to work: the number of remaining replays is not reduced and participants could basically still replay the file as often as they like.

I am completely new to javascript, so I am pretty lost when it comes to debugging the code or adapting it to my use case (i.e. embedding audio instead of video files).
Is there anything wrong with the code? Is there a fix?

Any help would be much appreciated! Thanks a lot!
Jana
Last edit: 2 years 4 months ago by Affenbrotbaum. Reason: missing info and typo

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 4 months ago #232074 by tpartner
Replied by tpartner on topic embed audio that can be listened to only once
I see a couple of problems with using a simple <audio> element rather than a JavaScript audio library...

1) You cannot easily remove the controls for an <audio> element. Some browsers will not render the element without controls.

2) Many browsers will not allow autoplay and will not allow programmatically playing until the user interacts with the element.

The simplest solution for your requirements would be to hide the <audio> element when the respondent clicks "Play", preventing them from interacting with it any more. A downside to this is that the respondent won't be able to adjust the volume within the player - they will have to use device controls for that.

Code:
<p style="text-align: left;">
  <audio controls="controls">
    <source src="/upload/surveys/184592/files/michaela_ING_orig_sent_1_final.wav" type="audio/mp3" />
  </audio>
</p>
 
<script type="text/javascript" data-author="Tony Partner">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    var audio = $('#question{QID} audio:eq(0)');
    $(audio).on('play', function(e) {
      $(audio).fadeOut(500);
    });
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • Affenbrotbaum
  • Affenbrotbaum's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 4 months ago #232075 by Affenbrotbaum
Replied by Affenbrotbaum on topic embed audio that can be listened to only once
Thanks so much!

The code is running and the solution certainly does the trick for us.
Thanks for filling me in on what to consider when embedding audio as well.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose