Skip to Content

Adding video and audio to web apps

Follow this guide to learn how to add video and audio files to your web application.

Introduction

Prepr simplifies asset management and provides multi-format support so you can embed video and audio files in your web application. This guide covers the end-to-end workflow from defining video/audio assets in your Prepr schema, adding these assets to content items and fetching them via the API.

Prerequisites

You need to have the following setup before you can embed video and audio files in your web application.

For more information on how to set up a content model in Prepr, check out the content modeling basics. If you’re still unsure about how to structure your content, check out our example models for inspiration.

Embedding video and audio files in your web application

The steps below make use of the example of including a video or audio file in the content (Dynamic content field) of blog articles.

Define the Assets field in your model

To allow editors to add video or audio to content items, you need to add the Assets field to the corresponding model as follows:

  1. Click the Schema tab and click the model you want to update.

Add assets to dynamic content field

  1. You can simply add an Assets field to your model or as shown in this Blog article example, click to open an existing Dynamic content field and go to the Settings tab.

  2. Go to the Assets section and enable Video, Audio, and any other settings you need for the video or audio such as Caption.

For more information on different settings, check out the Field types guide.

Now, content editors can add and configure a video or audio when they create or edit a blog article content item.

Add video or audio to a content item

Now that you’ve allowed content editors to add a video or audio file to content items for a model, let’s look at the typical steps a content editor follows to do this.

  1. Navigate to the Content tab. You can either create a new content item or use an existing one.

Add video to content item

  1. You can simply add a video or audio to your content item or like in this example, go to the Dynamic content editor, click the icon and drag the video or audio file from your local storage to the asset block.

  2. You can also configure your video/audio file, if enabled in the settings, by hovering over the video/audio file and clicking the icon in the top right corner, then selecting the needed action.

  3. You can add subtitles to your videos directly in Prepr (For Mux Player only) and set a custom video thumbnail.

Now that you have content items with video or audio files, let’s look at how to retrieve them in your front end.

Retrieve video/audio using the API

Mux integration
All the video and audio files uploaded to Prepr are stored in and played from Mux .

Before you can render any video or audio in your front-end, you need to retrieve these assets using the API. You can retrieve the following fields needed to render video or audio assets:

  • playback_id, the unique ID generated in Mux when a video or audio file is stored in Prepr.
  • cover, if the thumbnail is set for the video asset.
  • url, the video URL in the format: https://stream.mux.com/{PLAYBACK_ID}.m3u8. If you use Mux Player to render your video in your front end, the playback_id is sufficient.

For more information on other fields you can include in your query, check out the GraphQL API reference.

query { BlogArticle (id: "36136491-a333-44be-a53f-111ef759779c") { _id title _slug content { __typename ... on Assets { _type name items { _id playback_id url duration caption # Request thumbnail image cover alignment } } } } }

Please check out the Connecting front-end apps guide to learn more about retrieving content in a specific front-end framework.

Add a web player to your front end

Now that you’ve retrieved the video/audio asset for displaying, you can add your chosen web player to your web application. Because Mux delivers your videos in the HTTP Live Streaming (HLS) format, we recommend using a player that supports HLS such as the Mux Player  For other popular web players, check out the Mux documentation .

The instructions below show you how to install and set up the Mux Player.

  1. For standard video and audio files, install the Mux player package in your project with the following command line:
npm install @mux/mux-player
  1. If you’re only rendering background videos such as those for more dynamic background hero sections, then install the lightweight Mux Background Video component  instead:
npm install @mux/mux-background-video
  1. Get the playback_id, and if needed, the video thumbnail (cover) you retrieved in the previous step.

    If subtitles are added to videos in Prepr, these can only be rendered automatically with the Mux Player.

  2. For video or audio files, you can import the Mux player like in the code snippets below.

Example player components

// components/MuxVideoPlayer.js 'use client'; import '@mux/mux-player'; export default function MuxVideoPlayer({ playbackId, title, poster}) { return ( <mux-player playback-id={playbackId} metadata-video-title={title} poster={poster} stream-type="on-demand" style={{ aspectRatio: '16/9', width: '100%' }} > </mux-player> ); }

For more details on customizing the thumbnail for your Mux video, check out the Mux docs .

  1. You can then render the video or audio in the relevant pages in your front end like in the example code snippet below:
// app/blog/[slug]/page.js import MuxVideoPlayer from '@/components/MuxVideoPlayer'; export default async function Page({ params }) { const data = await getPreprData(params.slug); return ( <article> <h1>{data.title}</h1> <MuxVideoPlayer playbackId={data.video.playback_id} title={data.title} poster={data.video.cover} subtitleUrl={data.video.caption_url} /> </article> ); }

That’s it. Once you publish your content item, your video or audio file becomes available in your web application in the best possible format.

Want to learn more?

Check out the following guides to learn how to add other asset types to your web application:

Last updated on