MediaElement Player style breaks for logged in users

Created on 1 January 2023, almost 2 years ago
Updated 7 July 2023, over 1 year ago

Problem/Motivation

I am using Drupal 9.5. MediaElement Video/Audio player breaks when logged in as a user or admin. It breaks for both mediaelement library 4.2.17 and 5.1. For 5.1 library, it shows mejs-controls.svg not found and using 4.2.17 it shows addition overlapped play icon and control panel messed up.

I debugged a little and so fat what understand, inside the behavior "Drupal.behaviors.mediaelement" the "once" is triggering more than once, which actually causing the style breakup issue.

      $(once('mediaelement', '.mediaelementjs', context)).each(function () {
        $(this).mediaelementplayer(settings.mediaelement);
      });

I replace the code using core/jquery.once library, I see it working okay.



Steps to reproduce

Install the module and enable the mediaelement formatter on the field in view mode. Logged in as user and visit the page.

Proposed resolution

Remaining tasks

User interface changes

API changes

For MediaElement js library there is change from version 4 to 5 about svg images.

## Migrating from `4.x` to `5.x` version

In order to successfully install `5.x` in an existing setup, you must consider the following guidelines:

The major change in this version is the new svg sprite map. The sprite map is now built with a connection between `<symbol>` and `<use>`.
Every `<symbol>` is connected with an html attribute `id` by the `<use>` tag.

Another major change in Drupal 9.5 is core/jquery.once library is deprecated and replaced by core/once - which actually causing the issue I guess.

Data model changes

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡§πŸ‡©Bangladesh shafiqhossain

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States shadowwolf218

    Confirming this issue with the 2.x-dev branch as well. Drupal 9.5.10, PHP 8.2.5

    Steps to reproduce: install module, set mediaelement as the formatter for the (video) view field, log in and visit any page with a video.

    The version of the previously posted fix I opted for is somewhat different, and I cannot tell you why it works - or, in fact, *if* it works (or simply breaks something invisible to me). However, I can confirm that the prior fix did break the rendering for logged in users, causing a fallback to the browser's native player...and whatever I've managed, despite fairly thorough testing, doesn't seem to have broken anything.

    As far as I can tell, '.mediaelementjs' in the original code is the culprit. For reasons that are unclear to me, but perhaps have to do with how the containers are classed, replacing this with 'mediaelementjs' fixed the issue entirely. When the issue is present, for logged in users (and *only* logged in users) the div "mejs__layers mejs__overlay mejs__overlay-play" renders 3-4 separate times, causing the video to be nonfunctional and show the overlapping in the pictures above.

    From my limited understanding, this is likely because Drupal produces additional script calls to verify a user's logged-in status and check access permissions, and for some reason Drupal/once is failing to append "data-once" in a way that prevents those calls from re-triggering the bundle script every time this happens.

    I also removed what I believe to be a redundant "attach," but I'll freely admit that about a half-hour of A/B testing showed no functional difference with or without that.

    My "fix":

    'use strict';
    
    (function (Drupal, $, once) {
      Drupal.behaviors.mediaelement = {
        attach: function (context, settings) {
          $(once('mediaelement', 'mediaelementjs', context)).each(function () {
            $(this).mediaelementplayer(settings.mediaelement);
          });
    
          // Attach player to other elements if MediaElement.js is set to global.
          if (settings.mediaelement.attachSitewide !== undefined) {
            $(once('mediaelement', 'audio,video', context)).each(function () {
              $(this).mediaelementplayer(settings.mediaelement);
            });
          }
        }
      };
    })(Drupal, jQuery, once);

    Hope this helps in some way.

  • πŸ‡ΊπŸ‡¦Ukraine Kalavan

    Hello,

    We need to use video.mediaelementjs or audio.mediaelementjs instead of .mediaelementjs.
    It will fix duplication also on ajax requests.

  • πŸ‡§πŸ‡©Bangladesh shafiqhossain

    @kalavan is correct.

    I have updated the to 2.0.0, still having the same problem. I see @shadowwolf218 recommendation is implemented, but its not working for me. The main reason is that, in the player, ".mediaelementjs" referenced multiple times, that breaks the player.

    So I have created a patch for it.

    And again I am using 4.2.x version of the player. Currently its 7.0.x. Using this patch the player is working, but there is a change after 4.2.x, the location of mejs-controls.svg file, its now in configuration. Using 7.0 player version, console shows its missing, as a result the main play button is missing. For that, I think path of mejs-controls.svg need to provide, which actually can be done through standalone call.

    iconSprite: 'mejs-controls.svg'

  • Status changed to Needs review 3 months ago
  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru
Production build 0.71.5 2024