{"id":13515529,"url":"https://github.com/apache/cordova-plugin-media","last_synced_at":"2025-05-14T17:02:42.861Z","repository":{"id":9085304,"uuid":"10860604","full_name":"apache/cordova-plugin-media","owner":"apache","description":"Apache Cordova Media Plugin","archived":false,"fork":false,"pushed_at":"2024-10-26T01:35:51.000Z","size":987,"stargazers_count":389,"open_issues_count":124,"forks_count":767,"subscribers_count":43,"default_branch":"master","last_synced_at":"2025-05-08T15:24:11.055Z","etag":null,"topics":["android","cordova","hacktoberfest","ios","java","javascript","library","mobile","nodejs","objective-c"],"latest_commit_sha":null,"homepage":"https://cordova.apache.org/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apache.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-06-22T07:00:17.000Z","updated_at":"2025-05-07T03:44:15.000Z","dependencies_parsed_at":"2024-06-18T12:31:39.928Z","dependency_job_id":"b6df3253-a463-41a7-a8c6-f056426b8be0","html_url":"https://github.com/apache/cordova-plugin-media","commit_stats":{"total_commits":416,"total_committers":93,"mean_commits":4.473118279569892,"dds":0.8413461538461539,"last_synced_commit":"7330fff87bd101984687b98bba6001a312866518"},"previous_names":[],"tags_count":67,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apache%2Fcordova-plugin-media","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apache%2Fcordova-plugin-media/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apache%2Fcordova-plugin-media/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apache%2Fcordova-plugin-media/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apache","download_url":"https://codeload.github.com/apache/cordova-plugin-media/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253851590,"owners_count":21973768,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","cordova","hacktoberfest","ios","java","javascript","library","mobile","nodejs","objective-c"],"created_at":"2024-08-01T05:01:12.522Z","updated_at":"2025-05-14T17:02:42.759Z","avatar_url":"https://github.com/apache.png","language":"JavaScript","readme":"---\ntitle: Media\ndescription: Record and play audio on the device.\n---\n\u003c!--\n# license: Licensed to the Apache Software Foundation (ASF) under one\n#         or more contributor license agreements.  See the NOTICE file\n#         distributed with this work for additional information\n#         regarding copyright ownership.  The ASF licenses this file\n#         to you under the Apache License, Version 2.0 (the\n#         \"License\"); you may not use this file except in compliance\n#         with the License.  You may obtain a copy of the License at\n#\n#           http://www.apache.org/licenses/LICENSE-2.0\n#\n#         Unless required by applicable law or agreed to in writing,\n#         software distributed under the License is distributed on an\n#         \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n#         KIND, either express or implied.  See the License for the\n#         specific language governing permissions and limitations\n#         under the License.\n--\u003e\n\n# cordova-plugin-media\n\n[![Android Testsuite](https://github.com/apache/cordova-plugin-media/actions/workflows/android.yml/badge.svg)](https://github.com/apache/cordova-plugin-media/actions/workflows/android.yml) [![Chrome Testsuite](https://github.com/apache/cordova-plugin-media/actions/workflows/chrome.yml/badge.svg)](https://github.com/apache/cordova-plugin-media/actions/workflows/chrome.yml) [![iOS Testsuite](https://github.com/apache/cordova-plugin-media/actions/workflows/ios.yml/badge.svg)](https://github.com/apache/cordova-plugin-media/actions/workflows/ios.yml) [![Lint Test](https://github.com/apache/cordova-plugin-media/actions/workflows/lint.yml/badge.svg)](https://github.com/apache/cordova-plugin-media/actions/workflows/lint.yml)\n\n\nThis plugin provides the ability to record and play back audio files on a device.\n\n__NOTE__: The current implementation does not adhere to a W3C\nspecification for media capture, and is provided for convenience only.\nA future implementation will adhere to the latest W3C specification\nand may deprecate the current APIs.\n\nThis plugin defines a global `Media` Constructor.\n\nAlthough in the global scope, it is not available until after the `deviceready` event.\n\n```js\ndocument.addEventListener(\"deviceready\", onDeviceReady, false);\nfunction onDeviceReady() {\n    console.log(Media);\n}\n```\n\n## Installation\n\n```bash\ncordova plugin add cordova-plugin-media\n```\n\n## Supported Platforms\n\n- Android\n- iOS\n- Browser\n\n### Android Quirks\n\n#### SDK Target Less Than 29\n\nFrom the official [Storage updates in Android 11](https://developer.android.com/about/versions/11/privacy/storage) documentation, the [`WRITE_EXTERNAL_STORAGE`](https://developer.android.com/reference/android/Manifest.permission#WRITE_EXTERNAL_STORAGE) permission is no longer operational and does not provide access.\n\n\u003e If this permission is not allowlisted for an app that targets an API level before [`Build.VERSION_CODES.Q`](https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q) (SDK 29) this permission cannot be granted to apps.\n\nIf you need to add this permission, please add the following to your `config.xml`.\n\n```xml\n\u003cconfig-file target=\"AndroidManifest.xml\" parent=\"/*\" xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n    \u003cuses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" android:maxSdkVersion=\"28\" /\u003e\n\u003c/config-file\u003e\n```\n\n## Media\n\n```js\nvar media = new Media(src, mediaSuccess, [mediaError], [mediaStatus]);\n```\n\n### Parameters\n\n- __src__: A URI containing the audio content. _(DOMString)_\n\n- __mediaSuccess__: (Optional) The callback that executes after a `Media` object has completed the current play, record, or stop action. _(Function)_\n\n- __mediaError__: (Optional) The callback that executes if an error occurs. It takes an integer error code. _(Function)_\n\n- __mediaStatus__: (Optional) The callback that executes to indicate status changes. It takes a integer status code. _(Function)_\n\n- __mediaDurationUpdate__: (Optional) the callback that executes when the file's duration updates and is available. _(Function)_\n\n__NOTE__: `cdvfile` path is supported as `src` parameter:\n```javascript\nvar my_media = new Media('cdvfile://localhost/temporary/recording.mp3', ...);\n```\n\n### Constants\n\nThe following constants are reported as the only parameter to the\n`mediaStatus` callback:\n\n- `Media.MEDIA_NONE`     = 0;\n- `Media.MEDIA_STARTING` = 1;\n- `Media.MEDIA_RUNNING`  = 2;\n- `Media.MEDIA_PAUSED`   = 3;\n- `Media.MEDIA_STOPPED`  = 4;\n\n### Methods\n\n- `media.getCurrentAmplitude`: Returns the current amplitude within an audio file.\n\n- `media.getCurrentPosition`: Returns the current position within an audio file.\n\n- `media.getDuration`: Returns the duration of an audio file.\n\n- `media.play`: Start or resume playing an audio file.\n\n- `media.pause`: Pause playback of an audio file.\n\n- `media.pauseRecord`: Pause recording of an audio file.\n\n- `media.release`: Releases the underlying operating system's audio resources.\n\n- `media.resumeRecord`: Resume recording of an audio file.\n\n- `media.seekTo`: Moves the position within the audio file.\n\n- `media.setVolume`: Set the volume for audio playback.\n\n- `media.startRecord`: Start recording an audio file.\n\n- `media.stopRecord`: Stop recording an audio file.\n\n- `media.stop`: Stop playing an audio file.\n\n- `media.setRate`: Set the playback rate for the audio file.\n\n### Additional ReadOnly Parameters\n\n- __position__: The position within the audio playback, in seconds.\n    - Not automatically updated during play; call `getCurrentPosition` to update.\n\n- __duration__: The duration of the media, in seconds.\n\n\n## media.getCurrentAmplitude\n\nReturns the current amplitude within an audio file.\n\n    media.getCurrentAmplitude(mediaSuccess, [mediaError]);\n\n### Supported Platforms\n\n- Android\n- iOS\n\n### Parameters\n\n- __mediaSuccess__: The callback that is passed the current amplitude (0.0 - 1.0).\n\n- __mediaError__: (Optional) The callback to execute if an error occurs.\n\n### Quick Example\n\n```js\n// Audio player\n//\nvar my_media = new Media(src, onSuccess, onError);\n\n// Record audio\nmy_media.startRecord();\n\nmediaTimer = setInterval(function () {\n    // get media amplitude\n    my_media.getCurrentAmplitude(\n        // success callback\n        function (amp) {\n            console.log(amp + \"%\");\n        },\n        // error callback\n        function (e) {\n            console.log(\"Error getting amp=\" + e);\n        }\n    );\n}, 1000);\n```\n\n## media.getCurrentPosition\n\nReturns the current position within an audio file.  Also updates the `Media` object's `position` parameter.\n\n    media.getCurrentPosition(mediaSuccess, [mediaError]);\n\n### Parameters\n\n- __mediaSuccess__: The callback that is passed the current position in seconds.\n\n- __mediaError__: (Optional) The callback to execute if an error occurs.\n\n### Quick Example\n\n```js\n// Audio player\n//\nvar my_media = new Media(src, onSuccess, onError);\n\n// Update media position every second\nvar mediaTimer = setInterval(function () {\n    // get media position\n    my_media.getCurrentPosition(\n        // success callback\n        function (position) {\n            if (position \u003e -1) {\n                console.log((position) + \" sec\");\n            }\n        },\n        // error callback\n        function (e) {\n            console.log(\"Error getting pos=\" + e);\n        }\n    );\n}, 1000);\n```\n\n## media.getDuration\n\nReturns the duration of an audio file in seconds. If the duration is unknown, it returns a value of -1.\n\n\n    media.getDuration();\n\n### Quick Example\n\n```js\n// Audio player\n//\nvar my_media = new Media(src, onSuccess, onError);\n\n// Get duration\nvar counter = 0;\nvar timerDur = setInterval(function() {\n    counter = counter + 100;\n    if (counter \u003e 2000) {\n        clearInterval(timerDur);\n    }\n    var dur = my_media.getDuration();\n    if (dur \u003e 0) {\n        clearInterval(timerDur);\n        document.getElementById('audio_duration').innerHTML = (dur) + \" sec\";\n    }\n}, 100);\n```\n\n#### Android Quirk\n\nNewer versions of Android have aggressive routines that limit background processing. If you are trying to get the duration while your app is in the background longer than roughly 5 minutes, you will get more consistent results by using the [`mediaDurationUpdate` callback of the constructor](#parameters). \n\n## media.pause\n\nPauses playing an audio file.\n\n    media.pause();\n\n\n### Quick Example\n\n```js\n// Play audio\n//\nfunction playAudio(url) {\n    // Play the audio file at url\n    var my_media = new Media(url,\n        // success callback\n        function () { console.log(\"playAudio():Audio Success\"); },\n        // error callback\n        function (err) { console.log(\"playAudio():Audio Error: \" + err); }\n    );\n\n    // Play audio\n    my_media.play();\n\n    // Pause after 10 seconds\n    setTimeout(function () {\n        my_media.pause();\n    }, 10000);\n}\n```\n\n## media.pauseRecord\n\nPauses recording an audio file.\n\n    media.pauseRecord();\n\n\n### Supported Platforms\n\n- iOS\n\n\n### Quick Example\n\n```js\n// Record audio\n//\nfunction recordAudio() {\n    var src = \"myrecording.mp3\";\n    var mediaRec = new Media(src,\n        // success callback\n        function() {\n            console.log(\"recordAudio():Audio Success\");\n        },\n\n        // error callback\n        function(err) {\n            console.log(\"recordAudio():Audio Error: \"+ err.code);\n        });\n\n    // Record audio\n    mediaRec.startRecord();\n\n    // Pause Recording after 5 seconds\n    setTimeout(function() {\n        mediaRec.pauseRecord();\n    }, 5000);\n}\n```\n\n## media.play\n\nStarts or resumes playing an audio file.\n\n```js\nmedia.play();\n```\n\n### Quick Example\n\n```js\n// Play audio\n//\nfunction playAudio(url) {\n    // Play the audio file at url\n    var my_media = new Media(url,\n        // success callback\n        function () {\n            console.log(\"playAudio():Audio Success\");\n        },\n        // error callback\n        function (err) {\n            console.log(\"playAudio():Audio Error: \" + err);\n        }\n    );\n    // Play audio\n    my_media.play();\n}\n```\n\n### iOS Quirks\n\n- __numberOfLoops__: Pass this option to the `play` method to specify\n  the number of times you want the media file to play, e.g.:\n\n        var myMedia = new Media(\"http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3\")\n        myMedia.play({ numberOfLoops: 2 })\n\n- __playAudioWhenScreenIsLocked__: Pass in this option to the `play`\n  method to specify whether you want to allow playback when the screen\n  is locked.  If set to `true` (the default value), the state of the\n  hardware mute button is ignored, e.g.:\n\n        var myMedia = new Media(\"http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3\");\n        myMedia.play({ playAudioWhenScreenIsLocked : true });\n        myMedia.setVolume('1.0');\n\n\u003e Note: To allow playback with the screen locked or background audio you have to add `audio` to `UIBackgroundModes` in the `info.plist` file. See [Apple documentation](https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW23). Also note that the audio has to be started before going to background.\n\n- __order of file search__: When only a file name or simple path is\n  provided, iOS searches in the `www` directory for the file, then in\n  the application's `documents/tmp` directory:\n\n        var myMedia = new Media(\"audio/beer.mp3\")\n        myMedia.play()  // first looks for file in www/audio/beer.mp3 then in \u003capplication\u003e/documents/tmp/audio/beer.mp3\n\n## media.release\n\nReleases the underlying operating system's audio resources.\nThis is particularly important for Android, since there are a finite amount of\nOpenCore instances for media playback. Applications should call the `release`\nfunction for any `Media` resource that is no longer needed.\n\n    media.release();\n\n\n### Quick Example\n\n```js\n// Audio player\n//\nvar my_media = new Media(src, onSuccess, onError);\n\nmy_media.play();\nmy_media.stop();\nmy_media.release();\n```\n\n## media.resumeRecord\n\nResume recording an audio file.\n\n    media.resumeRecord();\n\n\n### Supported Platforms\n\n- iOS\n\n\n### Quick Example\n\n```js\n// Record audio\n//\nfunction recordAudio() {\n    var src = \"myrecording.mp3\";\n    var mediaRec = new Media(src,\n        // success callback\n        function() {\n            console.log(\"recordAudio():Audio Success\");\n        },\n\n        // error callback\n        function(err) {\n            console.log(\"recordAudio():Audio Error: \"+ err.code);\n        });\n\n    // Record audio\n    mediaRec.startRecord();\n\n    // Pause Recording after 5 seconds\n    setTimeout(function() {\n        mediaRec.pauseRecord();\n    }, 5000);\n\n    // Resume Recording after 10 seconds\n    setTimeout(function() {\n        mediaRec.resumeRecord();\n    }, 10000);\n}\n```\n\n## media.seekTo\n\nSets the current position within an audio file.\n\n    media.seekTo(milliseconds);\n\n### Parameters\n\n- __milliseconds__: The position to set the playback position within the audio, in milliseconds.\n\n\n### Quick Example\n\n```js\n// Audio player\n//\nvar my_media = new Media(src, onSuccess, onError);\n    my_media.play();\n// SeekTo to 10 seconds after 5 seconds\nsetTimeout(function() {\n    my_media.seekTo(10000);\n}, 5000);\n```\n\n## media.setVolume\n\nSet the volume for an audio file.\n\n    media.setVolume(volume);\n\n### Parameters\n\n- __volume__: The volume to set for playback.  The value must be within the range of 0.0 to 1.0.\n\n### Supported Platforms\n\n- Android\n- iOS\n\n### Quick Example\n\n```js\n// Play audio\n//\nfunction playAudio(url) {\n    // Play the audio file at url\n    var my_media = new Media(url,\n        // success callback\n        function() {\n            console.log(\"playAudio():Audio Success\");\n        },\n        // error callback\n        function(err) {\n            console.log(\"playAudio():Audio Error: \"+err);\n    });\n\n    // Play audio\n    my_media.play();\n\n    // Mute volume after 2 seconds\n    setTimeout(function() {\n        my_media.setVolume('0.0');\n    }, 2000);\n\n    // Set volume to 1.0 after 5 seconds\n    setTimeout(function() {\n        my_media.setVolume('1.0');\n    }, 5000);\n}\n```\n\n## media.startRecord\n\nStarts recording an audio file.\n\n    media.startRecord();\n\n### Supported Platforms\n\n- Android\n- iOS\n\n### Quick Example\n\n```js\n// Record audio\n//\nfunction recordAudio() {\n    var src = \"myrecording.mp3\";\n    var mediaRec = new Media(src,\n        // success callback\n        function() {\n            console.log(\"recordAudio():Audio Success\");\n        },\n\n        // error callback\n        function(err) {\n            console.log(\"recordAudio():Audio Error: \"+ err.code);\n        });\n\n    // Record audio\n    mediaRec.startRecord();\n}\n```\n\n### Android Quirks\n\n- Android devices record audio in AAC ADTS file format. The specified file should end with a _.aac_ extension.\n- The hardware volume controls are wired up to the media volume while any Media objects are alive. Once the last created Media object has `release()` called on it, the volume controls revert to their default behaviour. The controls are also reset on page navigation, as this releases all Media objects.\n\n### iOS Quirks\n\n- iOS only records to files of type _.wav_ and _.m4a_ and returns an error if the file name extension is not correct.\n\n- If a full path is not provided, the recording is placed in the application's `documents/tmp` directory. This can be accessed via the `File` API using `LocalFileSystem.TEMPORARY`. Any subdirectory specified at record time must already exist.\n\n- Files can be recorded and played back using the documents URI:\n\n        var myMedia = new Media(\"documents://beer.mp3\")\n\n- Since iOS 10 it's mandatory to provide an usage description in the `info.plist` if trying to access privacy-sensitive data. When the system prompts the user to allow access, this usage description string will displayed as part of the permission dialog box, but if you didn't provide the usage description, the app will crash before showing the dialog. Also, Apple will reject apps that access private data but don't provide an usage description.\n\nThis plugins requires the following usage description:\n\n* `NSMicrophoneUsageDescription` describes the reason that the app accesses the user's microphone. \n\nTo add this entry into the `info.plist`, you can use the `edit-config` tag in the `config.xml` like this:\n\n```\n\u003cedit-config target=\"NSMicrophoneUsageDescription\" file=\"*-Info.plist\" mode=\"merge\"\u003e\n    \u003cstring\u003eneed microphone access to record sounds\u003c/string\u003e\n\u003c/edit-config\u003e\n```\n\n## media.stop\n\nStops playing an audio file.\n\n    media.stop();\n\n### Quick Example\n\n```js\n// Play audio\n//\nfunction playAudio(url) {\n    // Play the audio file at url\n    var my_media = new Media(url,\n        // success callback\n        function() {\n            console.log(\"playAudio():Audio Success\");\n        },\n        // error callback\n        function(err) {\n            console.log(\"playAudio():Audio Error: \"+err);\n        }\n    );\n\n    // Play audio\n    my_media.play();\n\n    // Pause after 10 seconds\n    setTimeout(function() {\n        my_media.stop();\n    }, 10000);\n}\n```\n\n## media.stopRecord\n\nStops recording an audio file.\n\n    media.stopRecord();\n\n### Supported Platforms\n\n- Android\n- iOS\n\n### Quick Example\n\n```js\n// Record audio\n//\nfunction recordAudio() {\n    var src = \"myrecording.mp3\";\n    var mediaRec = new Media(src,\n        // success callback\n        function() {\n            console.log(\"recordAudio():Audio Success\");\n        },\n\n        // error callback\n        function(err) {\n            console.log(\"recordAudio():Audio Error: \"+ err.code);\n        }\n    );\n\n    // Record audio\n    mediaRec.startRecord();\n\n    // Stop recording after 10 seconds\n    setTimeout(function() {\n        mediaRec.stopRecord();\n    }, 10000);\n}\n```\n\n## media.setRate\n\nStops recording an audio file.\n\n    media.setRate(rate);\n\n### Supported Platforms\n\n- iOS\n- Android (API 23+)\n\n### Parameters\n\n- __rate__: The rate to set for playback.\n\n### Quick Example\n\n```js\n// Audio player\n//\nvar my_media = new Media(src, onSuccess, onError);\n    my_media.play();\n\n// Set playback rate to 2.0x after 10 seconds\nsetTimeout(function() {\n    my_media.setRate(2.0);\n}, 5000);\n```\n\n## MediaError\n\nA `MediaError` object is returned to the `mediaError` callback\nfunction when an error occurs.\n\n### Properties\n\n- __code__: One of the predefined error codes listed below.\n\n- __message__: An error message describing the details of the error.\n\n### Constants\n\n- `MediaError.MEDIA_ERR_ABORTED`        = 1\n- `MediaError.MEDIA_ERR_NETWORK`        = 2\n- `MediaError.MEDIA_ERR_DECODE`         = 3\n- `MediaError.MEDIA_ERR_NONE_SUPPORTED` = 4\n","funding_links":[],"categories":["JavaScript","Index"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapache%2Fcordova-plugin-media","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapache%2Fcordova-plugin-media","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapache%2Fcordova-plugin-media/lists"}