如何使用MPMediaPickerController从iPod库录制歌曲播放?

时间:2012-04-06 09:43:02

标签: iphone objective-c avaudiorecorder mpmusicplayercontroller

我正在制作一个应用程序,我必须使用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.Please help.Thanks提前。

2 个答案:

答案 0 :(得分:1)

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

你可以从here获得此奖励,我希望这对你有所帮助。

答案 1 :(得分:0)

也许会同时看两首曲目。您可以在轨道上录制单独的声音并触发此声音,并且您的mp3可以同时开始播放。显然,这只适用于你的应用程序。