gpt4 book ai didi

iphone - 无法同时播放 MPMoviePlayerController 和通过 AVAudioRecorder 进行录制?

转载 作者:行者123 更新时间:2023-12-03 20:51:19 25 4
gpt4 key购买 nike

我正在尝试在屏幕的一小块区域中播放视频并同时录制用户的歌曲。但似乎录制不成功。我添加了一些代码供您指出可能的错误。

- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"sound.caf"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMedium],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatLinearPCM],
AVFormatIDKey,
nil];

NSError *error = nil;
[audioRecorder setDelegate:self];

audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];


if (error)
{
NSLog(@"error: %@", [error localizedDescription]);

} else {
[audioRecorder prepareToRecord];
}


[self initialiseVideo];
}


- (void) initialiseVideo
{
[audioRecorder record];

NSString *videoName = @"Medium";
NSString *path = [[NSBundle mainBundle] pathForResource:videoName ofType:@"mp4"];

if (path) {
NSURL *url = [NSURL fileURLWithPath:path] ;
moviePlayer =[[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.scalingMode = MPMovieScalingModeFill;
[moviePlayer setControlStyle:MPMovieControlStyleNone];


[moviePlayer setFullscreen:NO];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];

[[moviePlayer view] setFrame:CGRectMake(58, 166, 197, 124)];
}
[moviePlayer play];

}


#pragma mark -
#pragma mark MPMoviePlayerController Delegate
#pragma mark -


- (void) moviePlayBackDidFinish:(NSNotification*)aNotification
{
[submitButton setUserInteractionEnabled:YES];
[audioRecorder stop] ;
[moviePlayer.view removeFromSuperview];

}


- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag
{

NSLog (@"audioRecorderDidFinishRecording:successfully:");
NSLog(@"%@", aRecorder.url);
NSLog(@"%@", aRecorder.description);
// your actions here

}
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder
error:(NSError *)error
{
NSLog(@"Encode Error occurred");
}



-(IBAction)playAudio:(id)sender
{
if (!audioPlayer.playing)
{
if (audioPlayer)
[audioPlayer release];
NSError *error;

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

audioPlayer.delegate = self;

if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
[audioPlayer play];
}
else
{



}
}


#pragma mark -AV delegate methods

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{

}
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
NSLog(@"Decode Error occurred");
}

我希望这是有道理的。流量好像没有进入

audioRecorderDidFinishRecording 

即使之后

[录音机停止];

请帮忙。我从过去三天开始就在寻找这个。但运气不好:(

最佳答案

-(void)ViewDidLoad{

NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"KARAOKE neele neele ambar pe Karaoke With Lyrics.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(10, 10, 200, 200);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playbackStateChanged)
name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];


NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.m4a",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];

}

-(IBAction)ButtonPlayClicked:(id)sender{
if (player.playing) {
[player stop];
}

[moviePlayer play];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];

if (!recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];

// Start recording
[recorder record];
}

}

-(IBAction)ButtonStopClicked:(id)sender{
[moviePlayer stop];
[recorder stop];
if(player)
[player stop];
}

- (void) playbackStateChanged {
MPMoviePlaybackState playbackState = moviePlayer.playbackState;
if(playbackState == MPMoviePlaybackStateStopped) {
NSLog(@"MPMoviePlaybackStateStopped");
[recorder stop];

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];

} else if(playbackState == MPMoviePlaybackStatePlaying) {
NSLog(@"MPMoviePlaybackStatePlaying");
[recorder record];
} else if(playbackState == MPMoviePlaybackStatePaused) {
NSLog(@"MPMoviePlaybackStatePaused");
[recorder pause];
} else if(playbackState == MPMoviePlaybackStateInterrupted) {
NSLog(@"MPMoviePlaybackStateInterrupted");
} else if(playbackState == MPMoviePlaybackStateSeekingForward) {
NSLog(@"MPMoviePlaybackStateSeekingForward");
} else if(playbackState == MPMoviePlaybackStateSeekingBackward) {
NSLog(@"MPMoviePlaybackStateSeekingBackward");
}
}

-(IBAction)ButtonRecordPlayClicked:(id)sender{
NSLog(@"%@",recorder.url);

if (!recorder.recording){
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
[player setDelegate:self];
[player setVolume:10.0];
[player play];
}

MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSMutableArray *array_mp3 = [[NSMutableArray alloc] init];
NSArray *itemsFromGenericQuery = [everything items];

for (MPMediaItem *song in itemsFromGenericQuery)
{
NSString *title = [NSString stringWithFormat:@"%@",[song valueForProperty:MPMediaItemPropertyAlbumTitle]];
if(title)
{
if(![array_mp3 containsObject:title])
[array_mp3 addObject:title];
}
}
MPMediaItem *nowPlayingMediaItem = [[MPMusicPlayerController iPodMusicPlayer] nowPlayingItem];
NSURL* songURL = [nowPlayingMediaItem valueForProperty:MPMediaItemPropertyAssetURL];
AVAsset* songAsset = [AVURLAsset URLAssetWithURL:songURL options:nil];
NSString* lyrics = [songAsset lyrics];
NSLog(@"%@",lyrics);
}

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Done" message: @"Finish playing the recording!"delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}

它认为它会对你有帮助。

关于iphone - 无法同时播放 MPMoviePlayerController 和通过 AVAudioRecorder 进行录制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16523748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com