Titanium iPhone:如何以编程方式访问语音备忘录并将声音对象存储在文件中?

发布于 2024-12-13 08:55:37 字数 230 浏览 0 评论 0原文

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

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

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

有什么帮助吗???

I am writing an Titanium iPhone app when I want user to share his Voice Memos records.

There are two options :
(1) Record -> save -> Share
(2) Browse Voice Memo -> Share

I am facing issue in both of this. In (1) I managed to record an audio and play it back. But I am confused how to convert this recorded sound object to file object so that i can share that file. and in (2) I am unable to find out a way to get access to Voice Memos records programmatically.

Any help???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故笙诉离歌 2024-12-20 08:55:37

不知道什么是语音备忘录,但我可以帮助您解决第一个问题:

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];

don't shure what is Voice Memo, but i can help you with 1st issue:

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];
自此以后,行同陌路 2024-12-20 08:55:37

问题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的解决方案:
我不确定钛合金中是否提供语音备忘录。

Solution for issue 1:

You can create a file object directly and write your recorded data on it like this,

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

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

Solution for issue 2:
I am not sure if Voice Memos are available in Titanium.

眼睛会笑 2024-12-20 08:55:37

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

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

// Write the data.

file.write( sound.toBlob() );

仍在解决第二个问题。有什么帮助吗???

I managed to save sound file after a lot of research.
Solution For issue1:

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

// Write the data.

file.write( sound.toBlob() );

Still working out for second issue. Any help???

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文