<commit_before>---
layout: "fluid/docs_base"
version: "3.14.0"
versionHref: "/docs/native"
path: ""
category: native
id: "media"
title: "Media"
header_sub_title: "Class in module "
doc: "Media"
docType: "class"
---

<h1 class="api-title">Media</h1>

<a class="improve-v2-docs" href="http://github.com/ionic-team/ionic-native/edit/master/src/@ionic-native/plugins/media/index.ts#L117">
  Improve this doc
</a>






<p>This plugin provides the ability to record and play back audio files on a device.</p>
<p>Some hints if you are using iOS and recording doesn&#39;t work:
1.) Try to use a absolute file path but remove beginning &quot;file://&quot;.
Then it looks like: <code>/var/mobile/Containers/Data/Application/AF438B8B-7724-4FBB-8E69-083463224FC4/tmp/my_file.m4a</code>
Example: <code>this.media.create(this.file.tempDirectory.replace(/^file:\/\//, &#39;&#39;) + &#39;my_file.m4a&#39;)</code>
2.) If that&#39;s not working, too, create the file before using.
Example:</p>
<pre><code class="lang-typescript">import { MediaPlugin, MediaObject } from &#39;@ionic-native/media&#39;;
import { File } from &#39;@ionic-native/file&#39;;

...

constructor(private media: MediaPlugin, private file: File) { }

...

this.file.createFile(this.file.tempDirectory, &#39;my_file.m4a&#39;, true).then(() =&gt; {
  let file = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, &#39;&#39;) + &#39;my_file.m4a&#39;);
  file.startRecord();
  window.setTimeout(() =&gt; file.stopRecord(), 10000);
});
</code></pre>
<p>You can find the reasons here: <a href="https://github.com/ionic-team/ionic-native/issues/1452#issuecomment-299605906">https://github.com/ionic-team/ionic-native/issues/1452#issuecomment-299605906</a></p>


<p>Repo:
  <a href="https://github.com/apache/cordova-plugin-media">
    https://github.com/apache/cordova-plugin-media
  </a>
</p>


<h2><a class="anchor" name="installation" href="#installation"></a>Installation</h2>
<ol class="installation">
  <li>Install the Cordova and Ionic Native plugins:<br>
    <pre><code class="nohighlight">$ ionic cordova plugin add cordova-plugin-media
$ npm install --save @ionic-native/media
</code></pre>
  </li>
  <li><a href="https://ionicframework.com/docs/native/#Add_Plugins_to_Your_App_Module">Add this plugin to your app's module</a></li>
</ol>



<h2><a class="anchor" name="platforms" href="#platforms"></a>Supported platforms</h2>
<ul>
  <li>Android</li><li>BlackBerry 10</li><li>Browser</li><li>iOS</li><li>Tizen</li><li>Ubuntu</li><li>Windows</li><li>Windows Phone</li>
</ul>






<h2><a class="anchor" name="usage" href="#usage"></a>Usage</h2>
<pre><code class="lang-typescript">import { MediaPlugin, MediaObject } from &#39;@ionic-native/media&#39;;


constructor(private media: MediaPlugin) { }


...


// Create a MediaPlugin instance.  Expects path to file or url as argument
// We can optionally pass a second argument to track the status of the media

const onStatusUpdate = (status) =&gt; console.log(status);
const onSuccess = () =&gt; console.log(&#39;Action is successful.&#39;);
const onError = (error) =&gt; console.error(error.message);

const file: MediaObject = this.media.create(&#39;path/to/file.mp3&#39;, onStatusUpdate, onSuccess, onError);

// play the file
file.play();

// pause the file<commit_msg>Automated build of native docs ionic-team/ionic-native@9f1a0f95857542330fac0b2b12ec158f6d805e67<commit_after>---
layout: "fluid/docs_base"
version: "3.14.0"
versionHref: "/docs/native"
path: ""
category: native
id: "media"
title: "Media"
header_sub_title: "Class in module "
doc: "Media"
docType: "class"
---

<h1 class="api-title">Media</h1>

<a class="improve-v2-docs" href="http://github.com/ionic-team/ionic-native/edit/master/src/@ionic-native/plugins/media/index.ts#L114">
  Improve this doc
</a>






<p>This plugin provides the ability to record and play back audio files on a device.</p>
<p>Some hints if you are using iOS and recording doesn&#39;t work:
1.) Try to use a absolute file path but remove beginning &quot;file://&quot;.
Then it looks like: <code>/var/mobile/Containers/Data/Application/AF438B8B-7724-4FBB-8E69-083463224FC4/tmp/my_file.m4a</code>
Example: <code>this.media.create(this.file.tempDirectory.replace(/^file:\/\//, &#39;&#39;) + &#39;my_file.m4a&#39;)</code>
2.) If that&#39;s not working, too, create the file before using.
Example:</p>
<pre><code class="lang-typescript">import { Media, MediaObject } from &#39;@ionic-native/media&#39;;
import { File } from &#39;@ionic-native/file&#39;;

...

constructor(private media: Media, private file: File) { }

...

this.file.createFile(this.file.tempDirectory, &#39;my_file.m4a&#39;, true).then(() =&gt; {
  let file = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, &#39;&#39;) + &#39;my_file.m4a&#39;);
  file.startRecord();
  window.setTimeout(() =&gt; file.stopRecord(), 10000);
});
</code></pre>
<p>You can find the reasons here: <a href="https://github.com/ionic-team/ionic-native/issues/1452#issuecomment-299605906">https://github.com/ionic-team/ionic-native/issues/1452#issuecomment-299605906</a></p>


<p>Repo:
  <a href="https://github.com/apache/cordova-plugin-media">
    https://github.com/apache/cordova-plugin-media
  </a>
</p>


<h2><a class="anchor" name="installation" href="#installation"></a>Installation</h2>
<ol class="installation">
  <li>Install the Cordova and Ionic Native plugins:<br>
    <pre><code class="nohighlight">$ ionic cordova plugin add cordova-plugin-media
$ npm install --save @ionic-native/media
</code></pre>
  </li>
  <li><a href="https://ionicframework.com/docs/native/#Add_Plugins_to_Your_App_Module">Add this plugin to your app's module</a></li>
</ol>



<h2><a class="anchor" name="platforms" href="#platforms"></a>Supported platforms</h2>
<ul>
  <li>Android</li><li>BlackBerry 10</li><li>Browser</li><li>iOS</li><li>Tizen</li><li>Ubuntu</li><li>Windows</li><li>Windows Phone</li>
</ul>






<h2><a class="anchor" name="usage" href="#usage"></a>Usage</h2>
<pre><code class="lang-typescript">import { Media, MediaObject } from &#39;@ionic-native/media&#39;;


constructor(private media: Media) { }


...


// Create a Media instance.  Expects path to file or url as argument
// We can optionally pass a second argument to track the status of the media

const onStatusUpdate = (status) =&gt; console.log(status);
const onSuccess = () =&gt; console.log(&#39;Action is successful.&#39;);
const onError = (error) =&gt; console.error(error.message);

const file: MediaObject = this.media.create(&#39;path/to/file.mp3&#39;, onStatusUpdate, onSuccess, onError);

// play the file
file.play();

// pause the file
{'fraction_non_alphanumeric': 0.19198312236286919, 'fraction_numerical': 0.04083785412899337, 'mean_word_length': 5.07227813357731, 'pattern_counts': {'":': 0, '<': 141, '<?xml version=': 0, '>': 141, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5880465', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2323, 'n_words': 502}