2012-08-17 49 views
2

我想在iOS中录制语音&播放它。另外我想将录制的文件发送到服务器。我有一个扭曲的录制文件必须在ANDROID设备上播放。所以我尝试使用MP3。但AVAudioRecorder不允许用于录制的MP3 Folrmat。所以我使用了AAC。但它不工作。这是我的代码以AAC格式录制和播放音频iOS 5 +

-(void)initializeAudioRecorder { 

    self.btnPlayRecording.enabled = NO; 
    self.btnStopRecording.enabled = NO; 

    // Get sound file URL in which recording is saved 
    NSURL *soundFileURL = [NSURL fileURLWithPath:[self soundFilePath]]; 

    // Set settings for audio recording 
    NSDictionary *audioRecordSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
     [NSNumber numberWithInt:AVAudioQualityMin],AVEncoderAudioQualityKey, 
     [NSNumber numberWithInt:16],AVEncoderBitRateKey, 
     [NSNumber numberWithInt:2], AVNumberOfChannelsKey, 
     [NSNumber numberWithFloat:44100.0],AVSampleRateKey, 
     [NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey, nil]; 

    NSError *error = nil; 
    self.audioRecorder = [[AVAudioRecorder alloc] 
        initWithURL:soundFileURL 
        settings:audioRecordSettings 
        error:&error]; 

    if (error) 
    { 
     NSLog(@"error: %@", [error localizedDescription]); 
    } 
    else { 
     if (![self.audioRecorder prepareToRecord]) { 
      NSLog(@"Failed to prepare recording"); 
     } 
    } 
} 

    #pragma mark - Get Sound File Path 

-(NSString*)soundFilePath { 

    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docsDirPath = [dirPaths objectAtIndex:0]; 
    NSString *soundFilePath = [docsDirPath stringByAppendingPathComponent:@"testSound.aac"]; 
    return soundFilePath; 
} 


#pragma mark - IBAction Methods 

- (IBAction)btnStartRecordingTapped:(id)sender { 

    if (!self.audioRecorder.recording) { 
     self.btnPlayRecording.enabled = NO; 
     self.btnStopRecording.enabled = YES; 
     [self.audioRecorder record]; 
    } 
} 

- (IBAction)btnStopRecordingTapped:(id)sender { 

    self.btnStopRecording.enabled = NO; 
    self.btnPlayRecording.enabled = YES; 
    self.btnStartRecording.enabled = YES; 
} 

- (IBAction)btnPlayRecordingTapped:(id)sender { 

    if (!audioRecorder.recording) { 
     self.btnStopRecording.enabled = YES; 
     self.btnStartRecording.enabled = NO; 
     NSError *error = nil; 

     self.audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:self.audioRecorder.url error:&error]; 
     self.audioPlayer.delegate = self; 

     if (error) { 
      NSLog(@"Error occured while playing recorded audio.\nError:- %@",[error localizedDescription]); 
     } 
     else { 
      [self.audioPlayer play]; 
     } 

    } 
} 

#pragma mark - AVAudioPlayer Delegate MEthods 

/* audioPlayerDidFinishPlaying:successfully: is called when a sound has finished playing. This method is NOT called if the player is stopped due to an interruption. */ 
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
{ 
    NSLog(@"In %s",__PRETTY_FUNCTION__); 
    self.btnStartRecording.enabled = YES; 
    self.btnStopRecording.enabled = NO; 
} 

/* if an error occurs while decoding it will be reported to the delegate. */ 
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error 
{ 
    NSLog(@"In %s",__PRETTY_FUNCTION__); 
    NSLog(@"audioPlayerDecodeError:- %@",[error localizedDescription]); 
} 

#pragma mark - AVAudioRecorder Delegate MEthods 

/* audioRecorderDidFinishRecording:successfully: is called when a recording has been finished or stopped. This method is NOT called if the recorder is stopped due to an interruption. */ 
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag 
{ 
    NSLog(@"In %s",__PRETTY_FUNCTION__); 
} 

/* if an error occurs while encoding it will be reported to the delegate. */ 
- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error 
{ 
    NSLog(@"In %s",__PRETTY_FUNCTION__); 
    NSLog(@"audioRecorderEncodeError:- %@",[error localizedDescription]); 
} 

但上面的代码没有录制音频。文件在DOCS目录中创建。但是当我尝试播放它时,它不起作用。我什么都听不到。

我的代码有什么问题? 还有什么库可以录制&播放格式的音频吗?可以在iOS + Android上播放吗?谢谢

+0

嗨@IOSAppDev我需要音频记录,它应该同时播放ios和android。你有什么想法 – sabeer 2015-12-10 13:26:46

+0

嗨朋友,我正在尝试使用以下[代码](http://stackoverflow.com/questions/34201702/android-recorded-aac-mp3-file-not-playing-in-ios )从Android录制语音并在iOS中播放,但无法使用。你能让我知道我错过了什么吗? – Hemant 2015-12-10 13:32:57

回答

2

这为我工作:

  1. 确保设备音量足够大:

    MPMusicPlayerController * musicPlayer = [MPMusicPlayerController iPodMusicPlayer]; 
    if (musicPlayer.volume < 1.0) { 
         musicPlayer.volume = 1; // device volume will be changed to maximum value 
    } 
    musicPlayer = nil; 
    
  2. 使用AVAudioSession:

    AVAudioPlayer * meinPlayer = [[AVAudioPlayer alloc] 
            initWithContentsOfURL:soundFileURL 
            error:&error]; 
    meinPlayer.delegate = self; 
    
    [meinPlayer prepareToPlay] 
    AVAudioSession *session = [AVAudioSession sharedInstance]; 
    [session setCategory:AVAudioSessionCategoryPlayback error:nil]; 
    [session setActive:YES error:nil]; 
    [meinPlayer play]; 
    
+0

嗨,朋友,我试图使用下面的[代码](http://stackoverflow.com/questions/34201702/android-recorded-aac-mp3-file-not-playing-in-ios)来记录声音从Android并在iOS上玩,但不起作用。你能让我知道我错过了什么吗? – Hemant 2015-12-10 13:35:13

0

尝试在实际播放声音之前调用AVAudioPlayer的prepareToPlay方法。同时检查您的设备音量。

0
// check audio permission 
    AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in 
     print(" check result: \(granted)") 
     do{ 
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord) 
      try AVAudioSession.sharedInstance().setActive(true) 
     }catch{ 
     } 

    }) 
+0

您必须将这些代码添加到您的DidLoad函数中 – tinkl 2015-11-27 09:16:47