2011-11-25 33 views
0

我有一些代码在iOS 4.3下工作正常。我看了一下互联网,发现其他人遇到了同样的问题,但没有为我效力。我认为我可以录制一些东西,但我无法播放它。这里是我的代码:iOS 5 - AVAPlayer不工作了

DetailViewController.h

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 
#import <CoreAudio/CoreAudioTypes.h> 
#import <AudioToolbox/AudioServices.h> 

@interface DetailViewController : UIViewController <UISplitViewControllerDelegate, AVAudioRecorderDelegate> { 

id detailItem; 
UILabel *detailDescriptionLabel; 

IBOutlet UIButton *btnStart; 
IBOutlet UIButton *btnPlay; 

//Variables setup for access in the class: 
NSURL * recordedTmpFile; 
AVAudioRecorder * recorder; 
BOOL toggle; 
} 

// Needed properties 
@property (nonatomic, retain) IBOutlet UIButton *btnStart; 
@property (nonatomic, retain) IBOutlet UIButton *btnPlay; 
@property (strong, nonatomic) id detailItem; 
@property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel; 

-(IBAction) start_button_pressed; 
-(IBAction) play_button_pressed; 
@end 

DetailViewController.m

- (void)viewDidLoad { 
[super viewDidLoad]; 
toggle = YES; 
btnPlay.hidden = YES; 
NSError *error; 

// Create the Audio Session 
AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 

// Set up the type of session 
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; 

// Activate the session. 
[audioSession setActive:YES error:&error]; 

[self configureView];  
} 

-(IBAction) start_button_pressed{ 
if (toggle) { 
    toggle = NO; 
    [btnStart setTitle:@"Press to stop recording" forState:UIControlStateNormal]; 
    btnPlay.enabled = toggle; 
    btnPlay.hidden = !toggle; 
      NSError *error; 

    NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init]; 

    [recordSettings setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey]; 

    [recordSettings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 

    [recordSettings setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; 

    // Create a temporary files to save the recording. 
    recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]]; 

    NSLog(@"The temporary file used is: %@", recordedTmpFile); 

    recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSettings error:&error]; 

    [recorder setDelegate:self]; 

    [recorder prepareToRecord]; 
    [recorder record]; 
} 
else { 
    toggle = YES; 
    [btnStart setTitle:@"Start recording" forState:UIControlStateNormal]; 
    btnPlay.hidden = !toggle; 
    btnPlay.enabled = toggle; 

    NSLog(@"Recording stopped and saved in file: %@", recordedTmpFile); 
    [recorder stop]; 
} 
} 

-(IBAction) play_button_pressed{ 

NSError *error; 
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error]; 

    if (!error) 
    { 
     [avPlayer prepareToPlay]; 
     [avPlayer play]; 

     NSLog(@"File is playing"); 
    } 

} 

- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player 
        successfully: (BOOL) flag { 
    NSLog (@"audioPlayerDidFinishPlaying:successfully:"); 
} 

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully: (BOOL)flag 
{ 
    NSLog (@"audioRecorderDidFinishRecording:successfully:"); 
} 

这里是我的程序运行:

2011-11-25 11:58: 02.005 Bluetooth1 [897:707]使用的临时文件是:file://localhost/private/var/mobile/Applications/D81023F8-C53D-4AC4-B1F7-14D66EB4844A/tmp/343915082005.caf 2011-11-25 11: 58:05.95 6 Bluetooth1 [897:707]录制停止并保存在文件中:file://localhost/private/var/mobile/Applications/D81023F8-C53D-4AC4-B1F7-14D66EB4844A/tmp/343915082005.caf 2011-11-25 11 :58:05.998蓝牙1 [897:707] audioRecorderDidFinishRecording:成功: 2011-11-25 11:58:11.785蓝牙1 [897:707]文件播放

出于某种原因,该功能audioPlayerDidFinishPlaying永远不会被调用。但似乎有些事情已经被记录下来。现在我不知道哪个部分不工作,但我想这与AVAudioPlayer有关。

[编辑]它越来越怪异和怪异。我想确保记录的东西,所以我寻找记录的持续时间。这是新的播放功能:

-(IBAction) play_button_pressed{ 

    NSError *error; 
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: recordedTmpFile error:&error]; 

    if (!error) 
    { 
     AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:recordedTmpFile options:nil]; 
     CMTime audioDuration = audioAsset.duration; 
     float audioDurationSeconds = CMTimeGetSeconds(audioDuration); 

     [avPlayer prepareToPlay]; 
     [avPlayer play]; 

     NSString *something = [NSString stringWithFormat:@"%f",audioDurationSeconds]; 
     NSLog(@"File is playing: %@", something); 
    } 
    else 
    { 
     NSLog(@"Error playing."); 
    } 
} 

现在,记录的长度被记录下来,很有意义(如果我录制10秒它显示10秒左右的东西)。但是,当我第一次放置这些代码行时,我忘记了将转换float转换为NSString。所以它崩溃了...和应用程序播放声音...经过不同的测试,我可以得出结论:我的应用程序可以录制和播放声音,但是会发生崩溃以播放录制的声音。我不知道可能是什么问题。我发现AVPlayer是异步的,是他们的事情吗?我完全失去了...

回答

0

好了,这是不是真的很酷回答你自己的问题。此外,当回答是不干净的,但它是工作......为了玩什么我都记录我用下面的代码块:

AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:recordedTmpFile options:nil]; 
    CMTime audioDuration = audioAsset.duration; 
    float audioDurationSeconds = CMTimeGetSeconds(audioDuration); 

    [avPlayer prepareToPlay]; 
    [avPlayer play]; 

    // Block for audioDurationSeconds seconds 
    [NSThread sleepForTimeInterval:audioDurationSeconds]; 

我计算的记录文件的长度和我等待这段时间...这是肮脏的,但它是在伎俩。另外,如果它在另一个线程中启动,它不会阻止应用程序。

我有任何人有我愿意接受它的东西!

0

这里尝试的解决方案:

Recording and playback

+0

我会尝试,但它看起来很奇怪。会话类别没有设置在需要的地方(如果我理解正确)[链接](http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Configuration/Configuration.html) – Flanfl

+0

我今天试过了,它不工作。这并不令人惊讶,因为本教程并未显示有关AudioSession的任何信息。我虽然也许这不是iOS 5需要,但它似乎是。我发现了一些我今天晚些时候会发布的解决方案。 – Flanfl

1

用下面的代码替换urlpath:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(
     NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *filepath = [documentsDirectory stringByAppendingPathComponent:@"urfile.xxx"]; 

NSURL *url = [NSURL fileURLWithPath:filepath]; 
+0

谢谢,但是我不能测试你的代码我的项目已经完成,我没有iPhone/Mac,所以我无法测试它。非常感谢! – Flanfl

相关问题