今天项目用到ios上的音视频播放,使用framework中的mediaplayer即可。
附上部分代码:
- (void)playVideo
{
NSURL *url = [NSURL fileURLWithPath:self.filePath];
player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player.view setFrame:CGRectMake(0, 0, 480, 300)];
player.scalingMode = MPMovieScalingModeAspectFit;
player.controlStyle = MPMovieControlStyleFullscreen;
player.view.transform = CGAffineTransformMakeRotation(M_PI / 2);
[player.view setFrame:self.view.bounds];
[self.view addSubview:player.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(MediaFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[player play];
}
- (void)playMusic
{
UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[returnBtn setTitle:@"返回" forState:UIControlStateNormal];
[returnBtn setFrame:CGRectMake(250, 5, 60, 30)];
[returnBtn addTarget:self
action:@selector(MediaFinishedCallback:)
forControlEvents:UIControlEventTouchUpInside];
NSURL *url = [NSURL fileURLWithPath:self.filePath];
self.labelLyric = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 200)];
labelLyric.backgroundColor = [UIColor clearColor];
// labelLyric.textColor = [UIColor blackColor];
labelLyric.textAlignment = UITextAlignmentCenter;
if (![[NSFileManager defaultManager] contentsAtPath:self.lyricFilePath])
{
labelLyric.text = @"暂无歌词";
}
labelLyric.numberOfLines = 0;
self.player = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.player.controlStyle = MPMovieControlStyleFullscreen;
LyricParse *lyricParse = [[[LyricParse alloc] init] autorelease];
NSArray *lyricArray = [lyricParse getLyricArray:self.lyricFilePath];
self.lyTimeArray = [lyricArray objectAtIndex:0];
self.lyTextArray = [lyricArray objectAtIndex:1];
player.view.backgroundColor = [UIColor colorWithRed:0.859f green:0.886f blue:0.929f alpha:1.0f];
[player.view addSubview:labelLyric];
[player.view addSubview:returnBtn];
[self.view addSubview:player.view];
player.view.frame = CGRectMake(0, 0, 320, 760);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MediaFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player play];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showLyric) userInfo:nil repeats:YES];
}
- (void)MediaFinishedCallback:(NSNotification *)Notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];
[self.player stop];
// [self.player release];
[self viewDidDisappear:NO];
[self viewDidUnload];
[self.view removeFromSuperview];
// [[ReadManage shareInstance] setInterfaceOrientation:UIInterfaceOrientationPortrait];
// [ReadManage shareInstance].m_Orientation = UIInterfaceOrientationPortrait;
}