Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

record Audio using Titanium Framework

Can we record Audio using Titanium Framework? (both Android and iPhone)

— asked July 28th 2010 by Radhakrishnan Selvaraj
  • audio
  • recording
2 Comments
  • Any support for recording audio yet for Android?

    Is there any indication in the API when something won't work on a certain platform? I haven't seen any, but maybe I'm missing something.

    — commented August 26th 2010 by Ben Buie
  • Go to vocalrank.com. That android audio recording module picks up where Codeboxed stopped. It works with TiStudio 3.0, and targets Android SDK 10. There's a sample apk available for download and more info about the Android audio recorder module.

    — commented February 12th 2013 by Eric Dean

18 Answers

  • You can try this code for audio recording

    var win = Titanium.UI.createWindow({
        title: 'Video Recording from Appcelerator Titanium',
        backgroundColor: '#fff'
    });
    
    // const value grabbed from
    // http://developer.android.com/reference/android/provider/MediaStore.Audio.Media.html#RECORD_SOUND_ACTION
    var RECORD_SOUND_ACTION = "android.provider.MediaStore.RECORD_SOUND";
    var soundUri = null; // Will be set as a result of recording action.
    
    var recordButton = Titanium.UI.createButton({
        top: 10, left: 10, right: 10, height: 35, title: "Record Audio"
    });
    var labelResultCaption = Titanium.UI.createLabel({
        top: 50, left: 10, right: 10, height: 35, visible: false, color: 'yellow'
    });
    var labelResult = Titanium.UI.createLabel({
        top: 90, left: 10, right: 10, height: 100, visible: false,
        backgroundColor: 'white', color: 'black',
        verticalAlign: 'top'
    });
    
    var shareButton = Titanium.UI.createButton({
        top: 50, left: 10, right: 10, height: 35,
        title: 'Share Recorded Video', visible: false
    });
    win.add(shareButton);
    var saveButton = Titanium.UI.createButton({
        top: 100, left: 10, right: 10, height: 35,
        title: 'Save Recorded Video', visible: false
    });
    win.add(saveButton);
    
    recordButton.addEventListener('click', function() 
    {
        var intent = Titanium.Android.createIntent({ action: RECORD_SOUND_ACTION });
        Titanium.Android.currentActivity.startActivityForResult(intent, function(e) 
        {
            if (e.error) 
            {
                Titanium.UI.createNotification({
                duration: Titanium.UI.NOTIFICATION_DURATION_LONG,
                message: 'Error: ' + e.error
                }).show();
            } 
            else 
            {
                if (e.resultCode === Titanium.Android.RESULT_OK) 
                {
                    soundUri = e.intent.data;
                    Titanium.UI.createNotification({
                    duration: Titanium.UI.NOTIFICATION_DURATION_LONG,
                    message: 'Video captured; now share or save it!'
                    }).show();
                    // note that this isn't a physical file! it's a URI in to the MediaStore.
                    shareButton.visible = true;
                    saveButton.visible = true;
                } 
                else 
                {
                    Titanium.UI.createNotification({
                    duration: Titanium.UI.NOTIFICATION_DURATION_LONG,
                    message: 'Canceled/Error? Result code: ' + e.resultCode
                    }).show();
                }
            }
        });
    });
    saveButton.addEventListener('click', function() 
    {
        var source = Titanium.Filesystem.getFile(soundUri);
        var target = Titanium.Filesystem.getFile('appdata://harsh.amr');
        // note: source.exists() will return false, because this is a URI into the MediaStore.
        // BUT we can still call "copy" to save the data to an actual file
        source.copy(target.nativePath);
    
    
        Ti.UI.createNotification({
        duration: Titanium.UI.NOTIFICATION_DURATION_LONG,
        message: 'Saved to: ' + target.nativePath
        }).show();
    });
    shareButton.addEventListener('click', function() 
    {
        var intent = Titanium.Android.createIntent({
        action: Titanium.Android.ACTION_SEND,
        type: 'audio/amr'
        });
        intent.putExtraUri(Titanium.Android.EXTRA_STREAM, soundUri);
        Titanium.Android.currentActivity.startActivity(
        Titanium.Android.createIntentChooser(intent, 'Send Video via'));
    });
    
    
    
    win.add(recordButton);
    win.add(labelResultCaption);
    win.add(labelResult);
    win.open();
    
    — answered May 9th 2012 by Harsh Punnoose
    permalink
    4 Comments
    • I just used the above code, and I stopped after tapping the record button with a message to insert a SD card. I have one in my device. The error code is 0.

      — commented May 16th 2012 by Eric Dean
    • it may be because of your sd card is not detected. you can check your sd card is working properly. try with your default recording application in your device. this code is properly working on my device.

      — commented May 16th 2012 by Harsh Punnoose
    • Hey Harsh, you are right. I was still plugged in via USB when I ran your code and the device was recognizing USB as priority over my internal SD card. Running the app disconnected from the build machine works flawlessly.

      — commented May 16th 2012 by Eric Dean
    • Awsm,Really Thanks.Above code solves my problem…

      — commented October 13th 2012 by pankaj Goyal
  • Hi Maurice,

    All Titanium API methods come from modules, the Titanium platform is formed from a lot of modules written as this one, the only difference is that you don't have to write a REQUIRE statement at the beginning ;)

    And lets face it, did you manage to do an app that works on both platforms without using an (ifAndroid) statement?

    The two platforms are very different, and the guys from Appcelerator are doing a tremendous job to make as many features as they can to run platform independent.

    This is why a lot of us are writing this modules to help everybody out, and take the load from Appcelerator.

    We will make this recording module have the same behavior as the iPhone one, but i hope this version of the module will help until then.

    Thank you.

    — answered August 6th 2011 by Alexandru Budin
    permalink
    0 Comments
  • Yes, it can. Please refer to titanium api to see how to save record audio.

    — answered August 2nd 2010 by Dinh Trong
    permalink
    1 Comment
    • Hi Ovan,

      Thanks for your Answer. But I found in documentation that Audio Recording is supported only in iPhone and not Android. There was no example in KitchenSink for Android,but demonstrated for iPhone.

      — commented August 2nd 2010 by Radhakrishnan Selvaraj
  • There's an audio recording sample in Kitchen Sink, you can check it here.

    If you downloaded Kitchen Sink, you can test it under Phone -> Sound -> Recording.

    — answered August 2nd 2010 by Goran Skledar
    permalink
    1 Comment
    • Hi Goran,

      I checked it. In the KitchenSink itself, the Sound Recording is supported only for iPhone and not Android. I even manually tried to do, but in vain.

      — commented August 2nd 2010 by Radhakrishnan Selvaraj
  • Hi Friends,

    As of now, it seems that, Audio Recording is supported only for iPhone through Titanium. I found it from this link:

    http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Media.AudioRecorder-object.html

    I did not receive any answer from the Appcelerator people, so I assume this is the case.

    — answered August 2nd 2010 by Radhakrishnan Selvaraj
    permalink
    1 Comment
    • Ahh, I missed the android part of your question sorry. Yes, AudioRecorder doesn't seem to be implemented on Android yet - there's a ticket for it here.

      — commented August 2nd 2010 by Goran Skledar
  • I found in documentation that Audio Recording is supported only in iPhone and not Android. There was no example in KitchenSink for Android,but demonstrated for iPhone. So, I consider, it is not supported for Android.

    — answered August 23rd 2010 by Radhakrishnan Selvaraj
    permalink
    0 Comments
  • any status as to when audio recording will be available for android?

    — answered November 12th 2010 by tom roarty
    permalink
    0 Comments
  • Is audio recording supported on Android yet??

    — answered January 15th 2011 by Brian Dittmer
    permalink
    0 Comments
  • BUMP!

    Come on guys, you can't leave us developers in the cold for so long.
    How about a nice reply saying when it's planned for?

    — answered July 20th 2011 by Maurice Faber
    permalink
    0 Comments
  • Android recording is supported – see here > http://developer.appcelerator.com/blog/2011/02/launching-activities-and-using-content-uris-in-android.html

    The problem for me is Ti currently provides no way (outside of creating a custom Module) to determine the actual file name for the recording from the recording URI.

    Why they didn't take the extra step to provide that is puzzling….but I hope it gets addressed pronto!

    — answered July 26th 2011 by Brian Dittmer
    permalink
    0 Comments
  • I'm sorry, but that only covers commanding an 'intent' (which uses the most ugly embedded components I have seen on a mobile device btw).

    According to Androids docs, it is also possible to use the MediaRecorder class (android.media.MediaRecorder).

    Why not plug that into Titanium? It seems very similar to the iPhone's setup.

    docs I found:
    http://www.brighthub.com/mobile/google-android/articles/40737.aspx
    http://www.benmccann.com/dev-blog/android-audio-recording-tutorial/

    — answered July 26th 2011 by Maurice Faber
    permalink
    0 Comments
  • Now recording is possible on Android, natively, without Intents.
    We just released the Audio Recorder Titanium Module for Android.
    You can see more details on Codeboxed Titanium Audio Recorder Module
    Follow us on Twitter: @codeboxed for more updates.

    — answered August 4th 2011 by Alexandru Budin
    permalink
    0 Comments
  • That was rather quick? Thank you guys for making this possible!

    — answered August 6th 2011 by Maurice Faber
    permalink
    0 Comments
  • Hmmmm….I don't see your docs updated. The Ti.Media.AudioRecorder still shows it's only compatible with idevices:

    http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Media.AudioRecorder-object

    Also, I have updated your sdk, but after rebuilding my app I still get errors about Ti.Media.createAudioRecorder not being available on Android.

    What am I missing here? I surely do hope your addition did not find it's way into some proprietary Android corner of your sdk.

    — answered August 6th 2011 by Maurice Faber
    permalink
    0 Comments
  • Aha, it's a third party module that only works for Android. Hmmm…now wouldn't it be nice if it was plugged onto your Ti.Media.AudioRecorder api.

    Please, we all want you to incorporate this solution so we don't have to write code twice, right ppl?

    — answered August 6th 2011 by Maurice Faber
    permalink
    0 Comments
  • Wow, thanks for replying so quickly.
    I have to say so far I have only made some demos with titanium without fancy stuff, so yeah, no need yet for ifAndroid logic.
    But it's very kool to read you saying this module will be adapted to follow the existing api. I hope it's not too hard.
    It would be real nice to have my demo ready with android recording working without too much tweaking. But it's not a must for me right now to support android, as its just a demo.

    But thanks again!

    — answered August 6th 2011 by Maurice Faber
    permalink
    0 Comments
  • it may be because of your sd card is not detected. you can check your sd card is working properly. try with your default recording application in your device. this code is properly working on my device.

    — answered May 16th 2012 by Harsh Punnoose
    permalink
    0 Comments
  • Now recording is possible on Android, natively, without Intents.
    We just released the Audio Recorder Titanium Module for Android.
    You can see more details on Codeboxed Titanium Audio Recorder Module
    Follow us on Twitter: @codeboxed for more updates.

    — answered August 4th 2011 by Alexandru Budin
    permalink
    1 Comment
    • Hi Everybody,

      I am using Android 2.2.I added the module run the project and having this error in console

      The 'apiversion' for 'com.codeboxed.audiorecorder' in the module manifest is not a valid value. Please use a version of the module that has an 'apiversion' value of 2 or greater set in it's manifest file

      What could be the issue ?

      — commented March 20th 2013 by Ali Akram
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.