我有一些代碼在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是異步的,是他們的事情嗎?我完全失去了...
我會嘗試,但它看起來很奇怪。會話類別沒有設置在需要的地方(如果我理解正確)[鏈接](http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Configuration/Configuration.html) – Flanfl
我今天試過了,它不工作。這並不令人驚訝,因爲本教程並未顯示有關AudioSession的任何信息。我雖然也許這不是iOS 5需要,但它似乎是。我發現了一些我今天晚些時候會發布的解決方案。 – Flanfl