我正在使用 MPMoviePlayerController 在我的应用中播放视频。以下是我播放视频的代码。
**NSURL *url=[[NSURL alloc] initFileURLWithPath:urlPath];
if(moviePlayer)
{
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;
}
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.repeatMode = MPMovieRepeatModeOne;
moviePlayer.currentPlaybackTime = 0.0;
moviePlayer.view.frame = _viewMoviePlayer.frame;
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.shouldAutoplay = YES;
[_viewMoviePlayer addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
[self.view sendSubviewToBack:_viewMoviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];**
当 viewWillDisappear 被调用时,我只是停止moviePlayer 并将其从视图中删除。 但是我的默认音乐播放器正在运行,但听起来并不合适。当我关闭我的申请时,它会。
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[moviePlayer stop];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;
[_viewMoviePlayer removeFromSuperview];
_viewMoviePlayer = nil;
}
任何人都可以告诉我的代码出了什么问题吗?所以我发布了moviePlayer然后它将播放默认音乐。 当我检查音乐的当前声音然后它的ohk以及它的节目歌曲正在音乐应用程序中运行。但没有发出声音。
答案 0 :(得分:1)
我遇到了同样的问题。我刚创建了一个单独的控制器。以下是我的相同代码:
-(void)playVideo
{
isPlay = FALSE;
if([[MPMusicPlayerController systemMusicPlayer] playbackState] == MPMoviePlaybackStatePlaying)
isPlay = TRUE;
NSString *urlPath = [[NSBundle mainBundle]
pathForResource:@"all"
ofType:@"mp4"];
NSURL *url=[[NSURL alloc] initFileURLWithPath:urlPath];
if(player)
[player pause];
player = nil;
if(avPlayerLayer)
[avPlayerLayer removeFromSuperlayer];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
avPlayerLayer.frame = self.view.frame;
[self.view.layer addSublayer:avPlayerLayer];
[player play];
}
-(void)removeMoviePlayer
{
if(isPlay)
[[MPMusicPlayerController systemMusicPlayer] play];
NSError *deactivationError = nil;
BOOL success = [[AVAudioSession sharedInstance] setActive:NO error:&deactivationError];
if (!success) {
NSLog(@"%@", [deactivationError localizedDescription]);
}
}
希望它对你有帮助......:)