gpt4 book ai didi

ios - MPMoviePlayerController:停止 MPMoviePlayerController 时没有声音

转载 作者:行者123 更新时间:2023-11-29 11:50:00 26 4
gpt4 key购买 nike

我正在使用 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 并将其从 View 中删除。 但是我的默认音乐播放器正在运行但没有声音。当我关闭我的应用程序时,它会。

-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[moviePlayer stop];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;

[_viewMoviePlayer removeFromSuperview];
_viewMoviePlayer = nil;
}

谁能告诉我代码中哪里出了问题?所以我发布了 moviePlayer 然后它会播放默认音乐。 当我检查音乐的当前声音时,它的哦,而且它的表演歌曲正在音乐应用程序中运行。但是没有听到声音。

最佳答案

我遇到了同样的问题。我刚刚创建了一个单独的 Controller 。下面是我的代码:

-(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]);
}
}

希望对您有所帮助...:)

关于ios - MPMoviePlayerController:停止 MPMoviePlayerController 时没有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41542278/

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