Ios AVAudioPlayer,已检查所有错误,没有声音
我的AVAudioPlayer播放无声音,但有零错误。花了近两个小时查看文档、stackoverflow和其他论坛 我把录音的持续时间从1秒改为3秒,再改为10秒。录制的音频文件大小记录为27K、33K、54K。有道理。但是没有声音 在我的iphone6plus和ipadpro上进行了测试,这两款手机都运行ios9.2。不确定是否相关,但我在XCode:audiotoolbox,AVFoundation中添加了这些框架 我的代码:Ios AVAudioPlayer,已检查所有错误,没有声音,ios,objective-c,avaudioplayer,avaudiorecorder,avaudiosession,Ios,Objective C,Avaudioplayer,Avaudiorecorder,Avaudiosession,我的AVAudioPlayer播放无声音,但有零错误。花了近两个小时查看文档、stackoverflow和其他论坛 我把录音的持续时间从1秒改为3秒,再改为10秒。录制的音频文件大小记录为27K、33K、54K。有道理。但是没有声音 在我的iphone6plus和ipadpro上进行了测试,这两款手机都运行ios9.2。不确定是否相关,但我在XCode:audiotoolbox,AVFoundation中添加了这些框架 我的代码: // Declared Global/Static Variab
// Declared Global/Static Variables:
// Don't really need to be global, as I always code with ARC disabled,
// and I manually call retain later. Many SO posts had AVAudio problems
// due to ARC issues.
AVAudioPlayer* avap;
AVAudioSession* avas;
AVAudioRecorder* avar;
- (void)viewDidLoad {
// request permission (should be already granted)
avas = [AVAudioSession sharedInstance];
[avas requestRecordPermission: ^(BOOL granted) {
NSLog(@"audio record perm1 %s", granted ? "T" : "F"); } ];
// check again using a second way
NSLog(@"audio record perm2 %s", (avas.recordPermission
== AVAudioSessionRecordPermissionGranted) ? "T" : "F");
NSError* nse1 = nil;
[avas setCategory:AVAudioSessionCategoryPlayAndRecord
// just in case something is wrong with my options,
// I commented these out for now
//withOptions:(AVAudioSessionCategoryOptionMixWithOthers
//|AVAudioSessionCategoryOptionDefaultToSpeaker)
error:&nse1];
NSLog(@"audio category error: %@", (id)nse1);
NSError* nse2 = nil;
[avas setMode:AVAudioSessionModeVoiceChat error:&nse2];
NSLog(@"audio mode error: %@", (id)nse2);
NSError* nse25 = nil;
[avas setActive:true error:&nse25];
NSLog(@"audio active error: %@", (id)nse25);
// took this line below from another stackoverflow post
NSURL *furl = [NSURL fileURLWithPath:[NSTemporaryDirectory()
stringByAppendingPathComponent:@"test.m4a"]];
// commenting above line for "furl = ..", and
// uncommenting these below (taken from another post) did not help.
//NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//NSString *docsDir = [dirPaths objectAtIndex:0];
//NSURL *tmpFileUrl = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"tmp.m4a"]];
//NSURL* furl = tmpFileUrl ;
// using a null record setting did not help
//NSDictionary<NSString *, id> * recordSettings;
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
[NSNumber numberWithFloat:16000.0], AVSampleRateKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
nil];
NSError* nse3 = nil;
avar = [[AVAudioRecorder alloc]
initWithURL:furl settings:recordSettings error:&nse3];
avar.delegate = self;
NSLog(@"audio record error: %@", (id)nse3);
const bool success = [avar recordForDuration:10.0];
NSLog(@"audio record succ: %d", success);
[avas retain]; // avoid any dealloc issues
[avar retain]; // avoid any dealloc issues
} // end of "void viewDidLoad"
- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avar
successfully:(BOOL)flag {
NSLog(@"recording done");
[avar stop];
// uncommenting these did not help
//NSError* nse5 = nil;
//AVAudioSession *session = [AVAudioSession sharedInstance];
//[session setActive:NO error:&nse5];
//NSLog(@"audio deactive error: %@", [(id)nse5 localizedDescription]);
NSNumber *fileSizeValue = nil;
NSError *fileSizeError = nil;
[avar.url getResourceValue:&fileSizeValue
forKey:NSURLFileSizeKey
error:&fileSizeError];
NSLog(@"filesize %@ error %@", fileSizeValue, fileSizeError);
NSError* nse4 = nil;
avap = [[AVAudioPlayer alloc] initWithContentsOfURL:avar.url
error:&nse4];
avap.delegate = self;
NSLog(@"audio play error1: %@", [(id)nse4 localizedDescription]);
NSLog(@"volume %f len %f play %d", avap.volume,
avap.duration, (int)[avap play]);
[avap retain];
}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player
error:(NSError *)error {
// never triggered, as you can see from the log output
NSLog(@"audio play error2: %@", [(id)error localizedDescription]);
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
successfully:(BOOL)flag {
NSLog(@"playing done %d", (int)flag);
}
咳嗽。电弧禁用?认真地咳嗽全局变量?你试过播放预先录制的文件吗?你检查过录音的内容了吗?我如何获得预录文件的URL?使用录音机?你是什么意思?检查内容?我记录了文件大小(如上所示)。将任何声音文件添加到项目中,包括到目标中,然后加载,以查看播放是否正常。要检查录制,请从设备获取录制的文件,并尝试在Mac上播放。。。
2015-12-15 01:37:01.972 sprite[916:327632] audio record perm1 T
2015-12-15 01:37:01.972 sprite[916:327632] audio record perm2 T
2015-12-15 01:37:01.972 sprite[916:327632] audio category error: (null)
2015-12-15 01:37:01.973 sprite[916:327632] audio mode error: (null)
2015-12-15 01:37:01.988 sprite[916:327632] audio active error: (null)
2015-12-15 01:37:02.014 sprite[916:327632] audio record error: (null)
2015-12-15 01:37:02.087 sprite[916:327632] audio record succ: 1
2015-12-15 01:37:14.499 sprite[916:327632] recording done
2015-12-15 01:37:14.555 sprite[916:327632] filesize 54354 error (null)
2015-12-15 01:37:14.558 sprite[916:327632] audio play error1: (null)
2015-12-15 01:37:14.590 sprite[916:327632] volume 1.000000 len 9.916000 play 1
2015-12-15 01:37:24.564 sprite[916:327632] playing done 1