> 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
