如何播放POVoiceHUD录制的音频文件

时间:2013-05-30 10:12:07

标签: ios objective-c cocoa-touch

我使用控件在应用程序POVoiceHUD中录制语音。

https://github.com/polatolu/POVoiceHUD

我似乎错过了让文件播放/检索录制的音频的内容。演示项目不包括存储在[self.voiceHud startForFilePath:[NSString stringWithFormat:@"%@/Documents/MySound.caf", NSHomeDirectory()]];

中的录制项目的播放

我尝试过使用:

- (IBAction)play:(id)sender {

NSLog(@"playRecording");
// Init audio with playback capability
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
_voiceHud = [[POVoiceHUD alloc] initWithContentsOfURL:url error:&error];
[_voiceHud play];
}

但是我收到错误incompatible pointer typeno known class selector

1 个答案:

答案 0 :(得分:1)

这里POVoiceHUD文件负责录制你的音频文件。要播放这个录制的文件,你需要在viewVontroller.h&中找到一些代码。 .m以及POVoiceHUD.h& .M

试试此代码

POVoiceHUD.h内包含此行

 AVAudioPlayer *player;

 - (void)playRecording:(NSURL*)_url;

POVoiceHUD.m

- (void)playRecording:(NSURL*)_url {
    [self cancelRecording];

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

    NSError *error;

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:_url error:&error];
    [player prepareToPlay];
    [player play];
}

在viewVontroller.h内部

- (IBAction)btnRecordTapped:(id)sender;
- (IBAction)btnPlayTapped:(id)sender;

ViewController.m

- (IBAction)btnRecordTapped:(id)sender {

        [self.voiceHud startForFilePath:[NSString stringWithFormat:@"%@/Documents/MySound.caf", NSHomeDirectory()]];
}

- (IBAction)btnPlayTapped:(id)sender
{
    [self.voiceHud cancelRecording];

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Documents/MySound.caf", NSHomeDirectory()]];

    [self.voiceHud playRecording:url];
}

我希望它可以帮到你。