Take the 2-minute tour ×

im trying to record an audio file using Media object in phonegap and then play it. i have no problem recording, but then when i try to play it my app shuts down, both on my emulator and my device. on the logcat it says: "null pointer exception at org.apache.cordova.readyPlayer (AudioPlayer.java)". many thanks to anyone who is willing to help me, it took me a while to realize im completely lost here. here is the code which i copied from http://docs.phonegap.com/en/2.4.0/cordova_media_media.md.html#media.stopRecord and added .play() after the .stopRecord(). i can add the manifest and activity if needed, i doubt that the problem is there but i could be wrong. index.html:

<script type="text/javascript" charset="utf-8" src="cordova-2.4.0.js"></script>

// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);

// Record audio
function recordAudio() {
    var src = "myrecording.amr";
    var mediaRec = new Media(src, onSuccess, onError);

    // Record audio

    // Stop recording after 10 sec
    var recTime = 0;
    var recInterval = setInterval(function() {
        recTime = recTime + 1;
        setAudioPosition(recTime + " sec");
        if (recTime >= 10) {
            document.getElementById('audio_position').innerHTML = "finished";
    }, 1000);

// Cordova is ready
function onDeviceReady() {

// onSuccess Callback
function onSuccess() {
    console.log("recordAudio():Audio Success");

// onError Callback 
function onError(error) {

// Set audio position
function setAudioPosition(position) {
    document.getElementById('audio_position').innerHTML = position;


<p id="media">Recording audio...</p>
<p id="audio_position"></p>


share|improve this question
What if you delete the mediaRec object after stopping the recording, create a new Media object with the same url and try to play that? –  Michael Apr 4 '13 at 11:03
actualy, five minutes ago i figured it out and then i saw your ansewr. it is correct. thank you very much anyway, next time i wont struggle with it for so long before asking, you could have saved me a LOT of time :) –  user2033402 Apr 4 '13 at 14:17
@user2033402 I'm currently stuggling with this issue, could you please post your solution. I'm using phonegap 3.0 but the syntax is the same. Why on earth would we need to delete the mediaRec object and create a new one –  Augie Gardner Oct 3 '13 at 15:49
add comment


Your Answer



Browse other questions tagged or ask your own question.