0

我正在编写一个简单的音频录制代码,该代码获取录制的音频并让另一个上传功能 - 将其上传到 Web 服务器。该代码基于 KitchenSinks 录音机。问题是“var myRecording”似乎不包含录音。我的代码遗漏了什么?如何获取录制文件,存储它,然后再次获取并上传?那里有录音机/上传器的例子吗?这是我的一些代码:

var myRecording = null;

var buttonUpload = Titanium.UI.createButton({
    title: 'Upload Recording'
});

Titanium.Media.audioSessionMode = Ti.Media.AUDIO_SESSION_MODE_PLAY_AND_RECORD;
var recording = Ti.Media.createAudioRecorder();

recording.compression = Ti.Media.AUDIO_FORMAT_ULAW;
recording.format = Ti.Media.AUDIO_FILEFORMAT_WAVE;

Ti.Media.addEventListener('recordinginput', function(e) {
    Ti.API.info('Input availability changed: '+e.available);
    if (!e.available && recording.recording) {
        b1.fireEvent('click', {});
    }
});

var file;
var sound;

var b1 = Titanium.UI.createButton({
    title:'Start Recording',
    width:200,
    height:40,
    top:20
});

b1.addEventListener('click', function(){
    if (recording.recording)
    {
        file = recording.stop();
        var f = Ti.Filesystem.getFile(file.path);
        var m = f.move(Ti.Filesystem.applicationDataDirectory + '/audio.m4a');
        b1.title = "Make New Recording";
        b2.show();
        pause.hide();
    }
    else
    {
        if (!Ti.Media.canRecord) {
            Ti.UI.createAlertDialog({
                title:'Error!',
                message:'No microphone is found.'
            }).show();
            return;
    }
        b1.title = "Stop Recording";
        recording.start();
        b2.hide();
        pause.show();       
    }
});
win.add(b1);

var pause = Titanium.UI.createButton({
    title:'Pause Recording',
    width:200,
    height:40,
    top:80
});
win.add(pause);
pause.hide();

pause.addEventListener('click', function(){
    if (recording.paused) {
        pause.title = 'Pause Recording';
        recording.resume();
    }
    else {
        pause.title = 'Continue Recording';
        recording.pause();
    }
});

var b2 = Titanium.UI.createButton({
    title:'Play recording',
    width:200,
    height:40,
    top:80
});

win.add(b2);
b2.hide();
b2.addEventListener('click', function(){
    if (sound && sound.playing)
    {
        sound.stop();
        sound.release();
        sound = null;
        b2.title = 'Play recording';
    }
    else
    {
        Ti.API.info("recording file size: "+file.size);
        sound = Titanium.Media.createSound({sound:file});
        if(Ti.Platform.osname == 'iphone'){
        win.rightNavButton = buttonUpload;
        }else{
        buttonUpload.right = 20;
        buttonUpload.top = 20;
        }
        sound.addEventListener('complete', function(){
            b2.title = 'Play recording';
        });
        sound.play();
        b2.title = 'Stop Playback';

    }
});

buttonUpload.addEventListener('click', function(e){
        myRecording = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, '/audio.m4a');
        if(myRecording != null) {
        myPostFunction();
        }else{
        alert('Nothing is recorded!');
        }
});

function myPostFunction(){ continues here…
4

0 回答 0