【问题标题】:Titanium iPhone : How to access Voice Memos programmatically and store the sound object in a file?Titanium iPhone:如何以编程方式访问语音备忘录并将声音对象存储在文件中?
【发布时间】:2011-12-20 02:50:13
【问题描述】:

当我希望用户分享他的语音备忘录记录时,我正在编写一个 Titanium iPhone 应用程序。

有两种选择: (1)记录->保存->分享 (2) 浏览语音备忘录 -> 分享

我在这两个方面都面临问题。在 (1) 中,我设法录制了音频并播放。但我很困惑如何将此录制的声音对象转换为文件对象,以便我可以共享该文件。在 (2) 中,我无法找到以编程方式访问语音备忘录记录的方法。

有什么帮助???

【问题讨论】:

  • 您是否将声音文件保存在任何地方?如果这样做,则必须使用 Titanium.Filesystem.getFile 访问该文件,它将作为文件对象工作。

标签: iphone titanium share voice


【解决方案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];

【讨论】:

  • 嗨 Sentine,感谢您的帮助。但我很期待 Titanium iPhone 应用程序的解决方案。你对此有什么想法吗? Voice Memo 是一个内置应用程序,用于浏览已录制的音频文件。
【解决方案2】:

问题 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 中是否提供语音备忘录。

【讨论】:

  • 感谢您的帮助..但是它对我没有用。经过大量研究,我设法使用以下方法保存了声音内容文件:file.write(sound.toBlob());
  • 变量recordData指的是声音文件:)
【解决方案3】:

经过大量研究,我设法保存了声音文件。 问题1的解决方案:

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

// Write the data.

file.write( sound.toBlob() );

仍在为第二期工作。有什么帮助???

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多