2011-11-25 57 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

相關問題