> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/audio/open-audio-file.md).

# Play audio files using JavaScript

Enhance your web applications by learning how to play audio files using JavaScript. Integrate audio features seamlessly with this comprehensive guide for web. The Apryse Web SDK streamlines secure, se

{% hint style="info" %}
New licenses for WebViewer Audio are no longer offered.
{% endhint %}

## Initial setup

Before you begin, make sure your development environment includes [Node.js](https://nodejs.org/en/) and [npm](https://www.npmjs.com/get-npm/).

## Install

{% tabs %}
{% tab title="Shell" %}
{% code lineNumbers="true" %}

```sh
npm install @pdftron/webviewer-audio
```

{% endcode %}
{% endtab %}
{% endtabs %}

## How to use

Here is an example of how WebViewer and WebViewer-audio could be integrated into your application.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
import React, { useRef, useEffect } from 'react';
import WebViewer from '@pdftron/webviewer';
import { initializeAudioViewer } from '@pdftron/webviewer-audio';

const App = () => {
    const viewer = useRef(null);

    useEffect(() => {
      WebViewer(
        {
          path: '/webviewer/lib',
        },
        viewer.current,
      ).then(async instance => {
        // Extends WebViewer to allow loading media files (.mp3, .mp4, ogg, webm, etc.)
        const {
            loadAudio,
        } = await initializeAudioViewer(
            instance,
            {
                license: '---- Insert commercial license key here after purchase ----',
            }
        );

        // Load a media element at a specific url. Can be a local or public link
        // If local it needs to be relative to lib/ui/index.html.
        // Or at the root. (eg '/audio.mp3')
        const audioUrl = '/audio.mp3';
        loadAudio(audioUrl);
      });
    }, []);

    return (
        <div className="App">
            <div className="webviewer" ref={viewer} />
        </div>
    );
}

export default App;
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Loading as a Script Tag

If your environment can not import WebViewer Video from the module, you can instead include WebViewer Video as a script tag. Simply, take the file `node_modules/@pdftron/webviewer-audio/dist/main.js` and add it to your project's html page.

{% tabs %}
{% tab title="HTML" %}
{% code lineNumbers="true" %}

```html
<head>
    <!-- ... -->
    <!-- main.js can be renamed -->
    <script src="./main.js"></script>
</head>
```

{% endcode %}
{% endtab %}
{% endtabs %}

This will add the object `WebViewerAudio` to the window. This object contains `initializeAudioViewer`. So the previous code can be changed to:

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
// ...
const {
    loadAudio,
} = await window.WebViewerAudio.initializeAudioViewer(
    instance,
    {
        license: '---- Insert commercial license key here after purchase ----',
    }
);
// ...
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/web/audio/open-audio-file.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
