1

我正在制作一个应用程序,我必须使用 MPMediaPickerController 从 iPod 音乐库播放音乐。播放歌曲后,我想用一些外部声音录制歌曲(例如:- 用户的声音)。我正在尝试做同样的事情,但是遇到问题,即当应用程序首次启动时,我从 iPod 音乐库中选择一首歌曲,然后单击开始录制按钮。当我单击开始录制按钮时,我之前播放的歌曲停止但录制工作正常。录制用户的声音工作正常,但歌曲没有被录制,因为我告诉点击“开始录制”按钮时歌曲停止。我正在使用 AVAudioRecorder 进行录制。这是我正在使用的代码。

-(Void)ViewDidLoad
{
self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
}
- (void)playOrPauseMusic:(id)sender {
MPMusicPlaybackState playbackState = self.musicPlayer.playbackState;
if (playbackState == MPMusicPlaybackStateStopped || playbackState == MPMusicPlaybackStatePaused) {
    [self.musicPlayer play];
} else if (playbackState == MPMusicPlaybackStatePlaying) {
    [self.musicPlayer pause];
}
}

- (void)openMediaPicker:(id)sender {
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = NO; // this is the default   
[mediaPicker shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
[self presentModalViewController:mediaPicker animated:YES];
}

- (void) startRecording
{   
[self.musicPlayer play];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err){
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}
[audioSession setActive:YES error:&err];
err = nil;
if(err){
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}

recordSetting = [[NSMutableDictionary alloc] init];

// We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];

// We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];

// We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

[recordSetting setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSetting setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setObject:[NSNumber numberWithInt: AVAudioQualityMax] forKey: AVEncoderAudioQualityKey];

recorderFilePath = [NSString stringWithFormat:@"%@/MySound.caf", DOCUMENTS_FOLDER] ;

NSLog(@"recorderFilePath: %@",recorderFilePath);

NSURL *url = [NSURL fileURLWithPath:recorderFilePath];

err = nil;

NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
if(audioData)
{
    NSFileManager *fm = [NSFileManager defaultManager];
    [fm removeItemAtPath:[url path] error:&err];
}

err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder){
    NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    UIAlertView *alert =
    [[UIAlertView alloc] initWithTitle: @"Warning"
                               message: [err localizedDescription]
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [alert show];

    return;
}

//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;

BOOL audioHWAvailable = audioSession.inputIsAvailable;
if (! audioHWAvailable) {
    UIAlertView *cantRecordAlert =
    [[UIAlertView alloc] initWithTitle: @"Warning"
                               message: @"Audio input hardware not available"
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [cantRecordAlert show];

    return;
}

// start recording
[recorder recordForDuration:(NSTimeInterval) 2];

lblStatusMsg.text = @"Recording...";
selector:@selector(handleTimer) userInfo:nil repeats:YES];
}

这是我的 MPMusicPlayer 和 AVAudioRecorder 的代码。请帮助。在此先感谢。

4

2 回答 2

1

您不能录制从 iPod 库导入的歌曲,只能录制 mp3 文件,为此您需要将 Mediaitem 转换为 mp3 格式。

你可以从这里得到参考,我希望这可以帮助你。

于 2012-04-06T15:18:25.127 回答
0

也许考虑同时播放两首曲目。您可以录制一个单独的配音轨道并触发它和您的 mp3 同时开始播放。显然,这只适用于您的应用程序。

于 2012-04-07T09:02:17.883 回答