-(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];
}
It think it ll helps you.