Video Autoplay su iPhone e dispositivi apple non funziona? Ecco uno snippet che ti aiuterà.

Il problema è dovuto al fatto che iPhone e dispositivi Apple in generale hanno una politica stringente di risparmio energetico (visto chissà cosa combinano già in background), pertanto ignorano beatamente il tuo “autoplay” sul tag video HTML5.

Come aggirare il problema?
Bisogna “agganciarsi” ad un’azione dell’utente per far partire il video, altrimenti non c’è trippa per felini.

Come si fa? Via Javascript ovviamente, ecco uno snippet:

jQuery(document).ready(function(){
  // definisco l'attributo playing per capire se il video sta già andando o no
  Object.defineProperty(HTMLMediaElement.prototype, 'playing', {
      get: function () {
          return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2);
      }
  });
  // alle azioni quali click, touchstart e scroll faccio partire il video se non sta girando
  jQuery('body').on('click touchstart scroll', function () {
      document.querySelectorAll('video').foreach( function(videoElement){
        if (videoElement.playing) {
            // video is already playing so do nothing
        }
        else {
            // video is not playing
            // so play video now
            videoElement.play();
        }      
      });
  });  
});

Diciamoci un poco di cose:

  1. La genialata di definire un attributo “playing” non è mia, ma di tale Shakti Singh Cheema
  2. Questo snippet fa partire tutti i video della pagina
  3. Ho mischiato javascript puro e jquery, ma tutto si può scrivere in entrambi i modi

Sentiti libero di fare del codice quello che vuoi e di segnalare eventuali problemi nei commenti.

Spero di essere stato utile.

File managed entire folder Drupal

Some information on managed and unmanaged files in Drupal

If a file is on a certain folder of the webserver within the Drupal folders it does not mean that Drupal knows it!

When Drupal “store” the presence of a certain file (i.e. public://myfile.pdf) it became a managed file.

You can add a file to managed with file_save_data function.

Luckly or unfortunaly a function that manage an entire directory of files, maybe in a recursive way, is not present in Drupal core.

How to import a folder of files to Drupal?

I create a script to this in a clean and easy way.

  $destination = 'public://path-to-folder';
  $regex = '/.*\.jpg$/'; // This will identify jpg files

  $paths = file_scan_directory($destination,$regex);
  foreach($paths as $path => $data){
    $realPath = drupal_realpath($path);
    $handle = fopen($realPath, 'r');
    $file = file_save_data($handle, $path, FILE_EXISTS_REPLACE);
    fclose($handle);
    if(!$file){
      dpm('ERROR');
    }
    else{
      dpm("YEAH!!");
      dpm($file);
    }
  }

Note that dpm function is provided with a contrib module: Devel.

Hint

I have used this functionality to put lots of images in the media library (made with Media module) and make them available for redactors.