【问题标题】:Playing audio with AVAudio Record from SQLite BLOB使用 SQLite BLOB 中的 AVAudio Record 播放音频
【发布时间】:2013-04-20 07:26:11
【问题描述】:

我在 UITableViewCell 中有一个 UIButton,它连接到一个播放 AVAudio 文件的方法,如下所示:

cell.playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cell.playButton.tag = indexPath.row;
[cell.playButton addTarget:self action:@selector(useSelectedFile:) forControlEvents:UIControlEventTouchUpInside];

useSelectedFile 方法如下所示:

-(void) useSelectedFile:(id)sender{

    int tag = [(UIButton *)sender tag];
    NSDictionary *audioFileInformation = [audioCellsArray objectAtIndex:tag];
    NSData *selectedAudioFile = [audioFileInformation objectForKey:@"Data"];
    NSError *error;

    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"sound.caf"];
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
    [selectedAudioFile writeToFile:filePath atomically:YES];
    NSURL *file = [NSURL fileURLWithPath:filePath]; 


    _audioPlayerForButton = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:&error];
    if (error)
        NSLog(@"Error: %@",
              [error localizedDescription]);
    else
        [_audioPlayerForButton play];

}

我可以录制文件并将它们保存在数据库中并可以很好地提取数据,但是我无法播放它们。使用当前代码,我得到:错误:操作无法完成。 (OSStatus 错误 1954115647。)

当我尝试使用时:

_audioPlayerForButton = [[AVAudioPlayer alloc] initWithData:selectedAudioFile error:&error];

点击播放按钮时我会收到此错误:错误:操作无法完成。 (OSStatus 错误 2003334207。)

检查我从模拟器写入磁盘的文件似乎已损坏?文件系统不会像普通的 .caf 文件那样将其显示为音频文件(带有音符的黑色),而是显示为带有 QuickTime 符号的小白色文件,QuickTime 不会播放。

我尝试在命令行上使用 macerror 状态码,但它只返回“未知错误”。

有人对如何解决这个问题并让我的文件播放有任何想法或提示吗?

【问题讨论】:

    标签: ios objective-c ipad avaudioplayer


    【解决方案1】:

    事实证明我是对的,在比较文件后,我可以清楚地看到数据不同,所以我将文件存储为 BLOB 更改为 TEXT 并使用 base64 类对其进行编码 从这里:iPhone SDK base64 encode / decode

    【讨论】:

      猜你喜欢
      • 2012-07-18
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2023-03-09
      • 2016-05-02
      • 1970-01-01
      • 2019-05-09
      • 2014-09-19
      相关资源
      最近更新 更多