title: Phonegap Media API - Record and Play Audio - Android
body: <p>I'm hoping to record some audio and then have the ability to play it back. Its important for me to be able to <a href="https://stackoverflow.com/questions/18542547/capture-audio-phonegap-3-0-without-interface">customize the record interface</a>. </p>
  
  <p>In the below example after recording I get a duration of -1 and no audio is played.</p>
  
  <p><strong>Step 1.</strong>
  Added <a href="http://docs.phonegap.com/en/3.3.0/cordova_media_media.md.html" rel="nofollow noreferrer">Media Plugin</a></p>
  
  <pre><code>cordova plugin add org.apache.cordova.media
  </code></pre>
  
  <p><strong>Step 2.</strong> My Code</p>
  
  <p>Note src is "amr" as requested from docs. </p>
  
  <blockquote>
    <p>Android devices record audio in Adaptive Multi-Rate format. The
    specified file should end with a .amr extension.</p>
  </blockquote>
  
  <p>However, "myrecording.amr" does not exist in my file structure as I'm hoping it will be created.</p>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>var data = {
    rec: ""
  };
  $scope.record = function(){
      console.log('record');
      var src = "myrecording.amr";
      data.rec = new Media(src,
      function() {
        console.log("recordAudio():Audio Success");
      },
  
      function(err) {
        console.log("recordAudio():Audio Error: "+ err.code);
      });
  
      data.rec.startRecord();
  }
  
  $scope.stopRecording = function(){
      console.log('stop');
      data.rec.stopRecord();
  }
  
  $scope.playRecording = function(){
      console.log('play');
      data.rec.play();
  }
  
  $scope.logDuration = function(){
      console.log(data.rec.getDuration());
  }
  </code></pre>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;button ng-click="record()"&gt;Record&lt;/button&gt;
  &lt;button ng-click="stopRecording()"&gt;Stop Record&lt;/button&gt;
  &lt;button ng-click="playRecording()"&gt;Play Record&lt;/button&gt;
  &lt;button ng-click="logDuration()"&gt;Log Duration&lt;/button&gt;
  </code></pre>
  
  <p><strong>Output From Above</strong></p>
  
  <p>No audio is played when I click play.</p>
  
  <pre><code>0     999846   log      record
  1     001845   log      stop
  2     002000   log      recordAudio():Audio Success
  3     004657   log      play
  4     008989   log      -1
  </code></pre>
  
  <p>Any help would be greatly appreciated. Let me know if I can answer any questions.</p>