Titanium iPhone: как получить доступ к голосовым заметкам программно и сохранить звуковой объект в файле? - PullRequest
1 голос
/ 02 ноября 2011

Я пишу приложение Titanium для iPhone, когда хочу, чтобы пользователь поделился своими записями голосовых заметок.

Есть два варианта: (1) Запись -> Сохранить -> Поделиться (2) Просмотр голосовых заметок -> Поделиться

Я сталкиваюсь с проблемой в обоих случаях. В (1) мне удалось записать аудио и воспроизвести его. Но я запутался, как преобразовать этот записанный звуковой объект в файловый объект, чтобы я мог поделиться этим файлом. и в (2) я не могу найти способ программного доступа к записям голосовых заметок.

Любая помощь ???

Ответы [ 3 ]

0 голосов
/ 02 ноября 2011

Решение для вопроса 1:

Вы можете напрямую создать файловый объект и записать на него свои записанные данные следующим образом:

// Save the data to file. Overwrite if fill
var file = Titanium.Filesystem.getFile( 'Your path', 'yourfile.wav' );

// Write the data.
file.write( recordData );

Решение для вопроса 2: Я не уверен, что голосовые заметки доступны на Titanium.

0 голосов
/ 07 ноября 2011

Мне удалось сохранить звуковой файл после долгих исследований.Решение Для выпуска 1:

var file = var file = Titanium.Filesystem.getFile( 'thePath', 'newRecording.wav' );

// Write the data.

file.write( sound.toBlob() );

Все еще работаем над вторым выпуском.Любая помощь ???

0 голосов
/ 02 ноября 2011

Не знаю, что такое голосовое напоминание, но я могу помочь вам с 1-м выпуском:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *err = nil;
    [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
    if(err){
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }
    [audioSession setActive:YES error:&err];
    err = nil;
    if(err){
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }

    recordSetting = [[NSMutableDictionary alloc] init];

    // We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];

    // We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
    [recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];

    // We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
    [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];


    // Create a new dated file

    recorderFilePath = [[NSString stringWithFormat:@"%@/Parking.caf", DOCUMENTS_FOLDER] retain];

    NSLog(@"recorderFilePath: %@",recorderFilePath);

    NSURL *url = [NSURL fileURLWithPath:recorderFilePath];

    err = nil;

    NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
    if(audioData)
    {
        NSFileManager *fm = [NSFileManager defaultManager];
        [fm removeItemAtPath:[url path] error:&err];
    }

    err = nil;
    recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
    if(!recorder){
        NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        UIAlertView *alert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: [err localizedDescription]
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }

    //prepare to record
    [recorder setDelegate:self];
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;

    BOOL audioHWAvailable = audioSession.inputIsAvailable;
    if (! audioHWAvailable) {
        UIAlertView *cantRecordAlert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: @"Audio input hardware not available"
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [cantRecordAlert show];
        [cantRecordAlert release]; 
        return;
    }

    // start recording
    [recorder recordForDuration:(NSTimeInterval) 30];
...