Website with audio MP3 does not touch iPhone

Asked

Viewed 615 times

1

I have a website where I play MP3 songs when I click on PLAY. On PC it works perfectly, but on iPhone it doesn’t play.

PLAY BUTTON

echo "<a href='#' id='tocar-musica'>
    <input type='hidden' class='nm-musica' value='$consulta[nm_musica]' name='dados'>
    <input type='hidden' class='id-musica' value='$consulta[id_musica]' name='dados'>
    <img src='http://www.meusite.net/images/btn-play-enable.png' id='botao' title='Ouvir música' alt='Play'></a>";

AJAX

    $(document).on('click', '#tocar-musica', function () {

    id_musica = $(this).find('.id-musica').val();
    nm_musica = $(this).find('.nm-musica').val();

    $.ajax({
        type: "GET",
        url: "http://www.meusite.net/play_musica.php",
        data: "musica="+id_musica

    }).done(function(msg) {
        $('#botao').html(msg);
        $('#play').html("<img src='http://www.meusite.net/images/equalizador.gif'><br>"+nm_musica);
    });     
});

PLAY_MUSICA.PHP

<?php
$musica = $_GET['musica'];
?>

<audio autoplay preload="none" controls="controls">
   <source src="http://www.meusite.net/audio/<?php echo $musica?>.mp3" type="audio/mp3">
   O seu navegador não suporta áudio em HTML5.
</audio>
  • But does the player appear? You want it to automatically play the music or you want to click the play?

  • @Guilhermenascimento, I wish it was automatically, but I’ve been reading AUTOPLAY does not work on iOS

  • This is exactly what I was saying, but there is a solution, I just need to know if the player appears on the screen and if clicking on it the music plays. Another thing I noticed is why an ajax just to generate the player?

  • @Guilhermenascimento, I used Ajax not to show the file path when passing the mouse over it and because I want to insert a click counter. Actually the player does not appear on the screen, because as it is a list with several songs, I only show a gif of equalizer and the name of the song that is playing.

  • But you do not need to generate the player in the back end other thing, when returning the mp3 the user will surely see the path of the music, but this is another story. Just a minute, I’ll try to formulate something.

  • @Guilhermenascimento, and because I also intended to insert a click counter. Thanks!

  • i remade the script on a localhost page, downloaded by iphone the local ip http://192.168.2.149 (ip of my PC) in Safari, clicked play and then clicked on the screen, did not open the iOS player, the music only played from the background.

Show 2 more comments

1 answer

2


First tip, always use var when declaring the variables, ex: var variavel = ...;, should look something like:

$(document).on('click', '#tocar-musica', function () {
    var id_musica = $(this).find('.id-musica').val();
    var nm_musica = $(this).find('.nm-musica').val();

I don’t see the need for Ajax (it won’t help much to hide), but that’s not the case now. It’s not necessary to "render/generate" the player on play_musica.php, manages only the path:

<?php
$musica = $_GET['musica'];
echo 'http://www.meusite.net/audio/' . $musica . '.mp3';

The ajax should look like this:

function gerarPlayer(path)
{
    path = path.replace(/^\s+|\s+$/gm, '');//Remove espaços em branco, é equivalente ao String.trim

    return (
        '<audio autoplay preload="none" controls="controls">' +
        '<source src="' + path + '" type="audio/mp3">' +
        'O seu navegador não suporta áudio em HTML5.' +
        '</audio>'
    );
}

$(document).on('click', '#tocar-musica', function () {
    var id_musica = $(this).find('.id-musica').val();
    var nm_musica = $(this).find('.nm-musica').val();

    $.ajax({
        type: "GET",
        url: "http://www.meusite.net/play_musica.php",
        data: "musica=" + id_musica

    }).done(function(msg) {
        $('#botao').html(gerarPlayer(msg));
        $('#play').html("<img src='http://www.meusite.net/images/equalizador.gif'><br>" + nm_musica);
    });     
});

Autoplay on iOS

Autoplay is blocked on iOS (and other mobile systems) for convenience, that is to save the battery while browsing the internet for example, so the user chooses the time he wants to listen to the music by clicking on the play (even if delegates via events) in the elements generated by the tags <audio> or <video>

However, it is possible to make the player play the music as soon as the user navigates. Since you are using jQuery then I will provide an example with such. At the moment you click or do an action you can fire the play of the music, for example:

document.addEventListener("touchstart", function() {
    [].forEach.call(document.querySelectorall("#botao audio"), function (audio) {
        audio.play();
    });
});

With jQuery it would be:

$(document).bind("touchstart", function() {
    $("#botao audio").each(function() {
        this.play();
    });
});

This example will look for if you have an added player, if you have it start the music when the user touches the screen (action ontouchstart).

Audio tag events for javascript:

  • abort - Fires when the loading of an audio/video is aborted
  • canplay - Fires when browser can start playing audio/video
  • canplaythrough - Shoots when the browser can tap non-stop because of buffers
  • durationchange - Fires when the audio/video duration is changed
  • emptied - Fires when playlist is empty
  • ended - Fire when playlist is finished
  • error - Fires when an error occurs during load
  • loadeddata - Fires when browser loaded data from current frame
  • loadedmetadata - Fires when the browser loads the meta data from an audio/video
  • loadstart - Fires when the browser starts loading an audio/video
  • pause - Fires when audio/video is paused
  • play - Fires when audio/video is playing
  • playing - Fires when the audio/video when it rotates after waiting for a buffer
  • progress - Fires when the browser is downloading the audio/video
  • ratechange - Fires when the playing speed of the audio/video is changed
  • seeked - Fires when user finishes moving Seek to a new position
  • seeking - Fires when user starts moving Seek to a new position
  • stalled - Fires when the browser is trying to retrieve meta data, but this data is not available
  • suspend - Fires when the browser can’t get any data from the media
  • timeupdate - Fires when playback position is modified
  • volumechange - Fires when volume is changed
  • waiting - Fires when the media stops because the buffer is expected to load
  • Thanks William, I’ll test and tell you! Abs

  • There is an error, in this line instead of (msg) you put (music)... $('#botao'). html(gerarPlayer(music)); Corrects if someone wants to use =)

  • On that line $("#botoo audio"). each(Function() {, would be "(#boot audio")?

  • It worked! Thanks... Abs

  • The only thing is that with the iPhone locked it keeps showing the file path on the screen, has how not to show it?

  • @Ricardo opens the player when the music starts?

  • No, not on the website or browser

  • @Ricardo Where exactly this url appears, you said on the screen. It would be in the address bar?

  • Sorry guy, appears in the iPhone player same, had understood if was appearing in the <audio tag player>

  • @Ricardo I will test, I think the player always appears, but I remember having used on iPad and the player does not appear, I will test on iOS8 now (iphone), as soon as I get back to you, until more,

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.