• Resolved juankibcn

    (@juankibcn)


    Hi,

    I’m running into an issue while trying to use AjaxPress together with a persistent audio player (Mediastream). What I’m trying to achieve

    I want to load and play episodes dynamically via AJAX, keeping a sticky player across navigation, while also maintaining a live stream player. The issues 1. On-demand player (AOD)

    The audio starts playing correctly on the first click, but the player UI does not render in the DOM until I click the button a second time.

    • First click → audio plays, but player is not visible
    • Second click → player appears and works correctly

    2. Live player

    The live player resets on the second click.

    So:

    • First click → everything works fine
    • Second click → the live stream player reloads/restarts unexpectedly

    Expected behavior

    • The AOD player should render immediately on the first click
    • The live player should persist and not reset

    Live example

    https://radioteletaxi.com

    You may need to click once to trigger the issue.

    document.addEventListener('click', function(e) {
    const btn = e.target.closest('.rp-play-episode');
    if (!btn) return;

    e.preventDefault();

    const mediaId = btn.getAttribute('data-media-id');

    if (!mediaId) {
    alert('This episode does not have a media_id yet.');
    return;
    }

    const sticky = document.getElementById('rtt-player-sticky');
    if (!sticky) return;

    if (!window.loadMSPlayer) {
    console.error('loadMSPlayer is not available');
    return;
    }

    if (window.rttPlayer && typeof window.rttPlayer.pause === 'function') {
    window.rttPlayer.pause();
    }

    sticky.innerHTML = '<div id="rtt-mdstrm-aod-player"></div>';
    sticky.offsetHeight;

    window.loadMSPlayer('rtt-mdstrm-aod-player', {
    type: 'media',
    id: mediaId,
    player: 'YOUR_PLAYER_ID'
    });
    });

    What I’ve already checked

    • loadMSPlayer is available and working
    • Audio playback works correctly
    • The container is injected into the DOM
    • The issue only happens on first interaction

    Question

    Could this be related to how AjaxPress handles DOM updates or lifecycle when injecting dynamic content?

    It feels like a timing/render issue, but I’m not sure how to hook properly into AjaxPress events.

    Also, I’ve opened a support ticket but haven’t received a response yet, so any help would be really appreciated

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Author Jafran Hasan

    (@iamjafran)

    Hi,

    Sorry for the delayed response.

    This behavior is caused by how AjaxPress works internally. AjaxPress doesn’t replace page content via AJAX; it loads pages inside a full-screen iframe and swaps between iframes during navigation. Each navigation creates a completely new document, so JavaScript state, DOM injections, and page-level variables do not persist.

    This explains the issue:

    1. The AOD player is created inside the current page. When navigation occurs, that page becomes a hidden background iframe. Audio may continue playing, but the player UI is no longer visible.
    2. Your live player guard (if (window.rttPlayer) return) only exists within a single iframe. Each new page gets a fresh window, so the player is recreated and eventually reset.

    Recommended Fix

    Create and manage both the AOD and live players in window.top, which is the AjaxPress shell and does not navigate.

    • Create the player container in window.top.document.
    • Store player instances on window.top (e.g. window.top.rttPlayer).
    • Render players into dynamically created containers in the top document rather than page-level elements.
    • This allows the player and its state to survive all AjaxPress navigations.

    A useful pattern is:

    const w = window.top;
    const d = w.document;
    

    Then create/reuse a global player container and initialize the player there.Additional Notes

    • window.top.loadMSPlayer() should be available because the shell page also loads the Mediastream script.
    • Use the same approach for the live player and store the instance on window.top.rttPlayer.
    • AjaxPress dispatches an ajaxpress:ready event after each navigation if you need to run code on page changes.
    • One small thing: the snippet you sent appears to be missing:
    const btn = e.target.closest('.rp-play-episode');
    

    Your live site already has this, so it looks like a copy/paste omission.

    Please give this approach a try and let us know if you still see issues with the first click behavior.

Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.