AVAudioPlayer,检查所有错误,没有声音

时间:2015-12-15 07:58:22

标签: ios objective-c avaudioplayer avaudiorecorder avaudiosession

我的AVAudioPlayer播放没有声音,但零错误。花了将近两个小时来查看docs,stackoverflow和其他论坛。

我将录音持续时间从1秒改为3秒到10秒。记录的音频文件大小记录为27K,33K,54K。说得通。但没有声音。

在我的iPhone 6 Plus和iPad Pro上测试,两者都运行iOS 9.2。不确定是否相关,但我在XCode中添加了这些框架:Audio Toolbox,AVFoundation。

我的代码:

// 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);
}

我的输出日志:

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

0 个答案:

没有答案