MPMoviePlayerController:停止 MPMoviePlayerController 时没有声音
MPMoviePlayerController: Didn't get sound when we stop MPMoviePlayerController
我正在使用 MPMoviePlayerController 在我的应用程序中播放 video。下面是我播放视频的代码。
**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 时,我只是 stop moviePlayer 并将其从视图中删除。 但我的默认音乐播放器是 运行 但没有声音。当我关闭我的应用程序时,它会。
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[moviePlayer stop];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;
[_viewMoviePlayer removeFromSuperview];
_viewMoviePlayer = nil;
}
谁能告诉我我的代码哪里出了问题?所以我发布了 moviePlayer 然后它会播放默认音乐。 当我检查音乐的当前声音时,它的哦,而且它的节目歌曲在音乐应用程序中是 运行。但是没有声音。
我遇到了同样的问题。我刚刚创建了一个单独的控制器。下面是我的代码:
-(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]);
}
}
希望对你有所帮助...:)
我正在使用 MPMoviePlayerController 在我的应用程序中播放 video。下面是我播放视频的代码。
**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 时,我只是 stop moviePlayer 并将其从视图中删除。 但我的默认音乐播放器是 运行 但没有声音。当我关闭我的应用程序时,它会。
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[moviePlayer stop];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;
[_viewMoviePlayer removeFromSuperview];
_viewMoviePlayer = nil;
}
谁能告诉我我的代码哪里出了问题?所以我发布了 moviePlayer 然后它会播放默认音乐。 当我检查音乐的当前声音时,它的哦,而且它的节目歌曲在音乐应用程序中是 运行。但是没有声音。
我遇到了同样的问题。我刚刚创建了一个单独的控制器。下面是我的代码:
-(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]);
}
}
希望对你有所帮助...:)