0

我正在开发具有录制音频并播放存储在文档目录中的录制文件的应用程序。

这是我的代码:

录制按钮方法。

(IBAction)stopButtonPressed:(id)sender{

     UIButton *button = (UIButton *)sender;

     if (button.selected) { // Play recorded file.

     self.stopButton.selected = NO;

     NSError *error;

    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioRecorder.url     error:&error];

    self.audioPlayer.delegate = self;

    self.audioPlayer.volume = 1.0;

    [self.audioPlayer prepareToPlay];

    [self.audioPlayer play];

    }

    else{ // Stop recording.

    self.stopButton.selected = YES;

    if (recordTimer) {

    [recordTimer invalidate];

    }

    [self.audioPlayer stop];

    self.recordButton.selected = NO;

    [audioRecorder stop];

    self.readyLabel.text = @"READY";

    [self insertNewRecording];
        }
    }
4

2 回答 2

2

我认为您正在使用 ARC,在这种情况下,您需要保留 AVAudioPlayer,因为它会自动设置为 nil。在您的头文件中输入以下内容

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

希望这对你有用,干杯吉姆

于 2013-04-12T15:30:30.020 回答
1

嗯..我不确定。但作为一个测试,也许你可以尝试通过指定文件名来播放文件?例如,你有一个 forest.mp3 可以播放,代码的关键部分可以是这样的:

path = [[NSBundle mainBundle] pathForResource:@"forest" ofType:@"mp3"]; 
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:path]; 
NSError *audioError = nil; 
av = [[AVAudioPlayer alloc] initWithData:sampleData  error:&audioError];

如果一切顺利,也许这与您代码中的 audioRecorder.url 有关?

希望这能给你一些想法。

于 2013-04-12T11:38:10.953 回答