locked
CreateCollisionOption.replaceExisting fails second time round

    Question

  • Hello,

     I am trying to make a simple voice recorder that writes over the same file the whole time.

    ........createFileAsync("myFile.mp3", Windows.Storage.CreateCollisionOption.replaceExisting ).then(.......

    I can record to an mp3 and play back one time ..... but the second time i try to record i get :

    Exception thrown at line 227, column5 in function code

    0x80070005 Javascript runtime error: Access is denied

    I think my file is still locked by the async recording but I do not know or cannot find how to release() / dispose()/ flushAsync()   or whatever I need to do!!

     any help would be appreciated 

    Thanks in advance.

    Friday, October 12, 2012 11:44 AM

Answers

  • ok

    I have now done more testing and found that it is something to do with the Playing back of the file.  I can now record it many times as I like but once I have loaded it into an audio object it seems to block writing access.  I have tried setting the audioElement to null once it have played back but that does not work.


    *****20 minutes later......

     Finally.
    it was the playback of the recording file.

    I simply had to set the AudioObject.src = null  after playback and that freed up the file for re-recording.

    Thanks for your input.
    • Edited by Middlecut Tuesday, October 16, 2012 4:39 PM
    • Marked as answer by Middlecut Tuesday, October 16, 2012 4:40 PM
    Tuesday, October 16, 2012 4:09 PM

All replies

  • after you done using(reading/writing) the file you need to dispose the stream
    Sunday, October 14, 2012 9:35 AM
  • exactly.. But I cant find out how to do that?  in javascript I can find nothing on disposing a stream for the mediaCapture.

    Thanks for your reply


    • Edited by Middlecut Monday, October 15, 2012 8:29 AM
    Monday, October 15, 2012 8:16 AM
  • Hi M,

    Can you post a simple repro for this so we can investigate further?

    Thanks!

    -Jeff


    Jeff Sanders (MSFT)

    Monday, October 15, 2012 2:46 PM
    Moderator
  •    // Start the media capture.
        function startMediaCaptureSession() {
            initMediaCapture();
            recordingState = true;
            var ratingOutput = document.getElementById("ratingOutput");
            ratingOutput.innerText = "RECORDING";
     //       Windows.Storage.KnownFolders.homeGroup.createFileAsync("audioCapture.mp3", Windows.Storage.CreationCollisionOption.generateUniqueName).then(function (newFile) {
            Windows.Storage.ApplicationData.current.localFolder.createFileAsync("audioCapture.mp3", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (newFile) {
                storageFile = newFile;
                oMediaCapture.startRecordToStorageFileAsync(profile, storageFile).then(function (result) {
                   
                }, errorHandler);
            });
        }
    
        // Stop the video capture.
        function stopMediaCaptureSession() {
            recordingState = false;
            var ratingOutput = document.getElementById("ratingOutput");
            ratingOutput.innerText = "Stopped recording";
            oMediaCapture.stopRecordAsync().then(function (result) {
               
                playAudio();
            }, errorHandler);
        }
    
    
        function playAudio() {
            var ratingOutput = document.getElementById("ratingOutput");
            ratingOutput.innerText = "PlayAudio";
            // var audioElement = new Audio(Windows.Storage.KnownFolders.homeGroup + storageFile.name);
            var filePath = Windows.Storage.ApplicationData.current.localFolder.path + "/" + storageFile.name;
            var audioElement = new Audio(filePath);
    
            audioElement.onplay = function () {
                var ratingOutput = document.getElementById("ratingOutput");
                ratingOutput.innerText = "Playing audio";
            }
    
            audioElement.onended = function () {
                var ratingOutput = document.getElementById("ratingOutput");
                ratingOutput.innerText = "Finished";
                audioElement = null;
    
            }
    
            audioElement.onerror = function (e) {
                var ratingOutput = document.getElementById("ratingOutput");
                ratingOutput.innerText = "Error" ;
            }
            audioElement.play();
        }


    Hi,

    Sorry for the delay, I had to get back to work on my development laptop.

    The application should recored when you press a button and when you stop it should play what you have just recorderd.  Pressing the button the second time should start recording again.  I can do this when i generate a unique file name every time but that will then clog up the storage space so i wanted just to overwrite the same file the whole time.  The second time round i get the access denied which indicates that the file is still locked from either playing the file or recording the file.   I don't know how to clear this lock.

    Here is a sample of code where I think I need to close the stream but do not know HOW to close the stream.

    (the pasting of this code snippet on a windows 8 laptop was a complete PAIN.. just took me 10 minutes and I could still only get it above this text)

    Tuesday, October 16, 2012 8:26 AM
  • Before you start the 2nd record you need to set the storage file to null.

    You should be able to do something like this in your code:

        if (storageFile != null) {
                storageFile=null;
            }

    I took the MediaCapture sample and changed it to replaceExisting for the file creation and then added this to the startRecord() function and it works beautiful!

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, October 16, 2012 2:49 PM
    Moderator
  • Thanks for your suggestions.

     I just tried this :

        // Start the media capture.
        function startMediaCaptureSession() {
            initMediaCapture();
            recordingState = true;
            var ratingOutput = document.getElementById("ratingOutput");
            ratingOutput.innerText = "RECORDING";
            if (storageFile != null) {
                storageFile = null;
            }
     //       Windows.Storage.KnownFolders.homeGroup.createFileAsync("audioCapture.mp3", Windows.Storage.CreationCollisionOption.generateUniqueName).then(function (newFile) {
            Windows.Storage.ApplicationData.current.localFolder.createFileAsync("audioCapture.mp3", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (newFile) {
               
                storageFile = newFile;
                oMediaCapture.startRecordToStorageFileAsync(profile, storageFile).then(function (result) {
                   
                }, errorHandler);
            });
        }

    But it is still giving the following error on the second time i try:

    Could it be the playback which is locking the file somehow?

    Tuesday, October 16, 2012 3:09 PM
  • ok

    I have now done more testing and found that it is something to do with the Playing back of the file.  I can now record it many times as I like but once I have loaded it into an audio object it seems to block writing access.  I have tried setting the audioElement to null once it have played back but that does not work.


    *****20 minutes later......

     Finally.
    it was the playback of the recording file.

    I simply had to set the AudioObject.src = null  after playback and that freed up the file for re-recording.

    Thanks for your input.
    • Edited by Middlecut Tuesday, October 16, 2012 4:39 PM
    • Marked as answer by Middlecut Tuesday, October 16, 2012 4:40 PM
    Tuesday, October 16, 2012 4:09 PM