MPMoviePlayerController 行为不正常

MPMoviePlayerController not behaving properly

我正在使用 MPMoviePlayerController 播放一个小视频作为我的应用程序的介绍视频。我使用了以下代码,效果很好。但是视频继续播放。结束并重新开始。即使我点击完成按钮,它也不会删除超级视图。我也希望我的状态栏可见并且我保持半透明仍然没有结果。我搜索了 google 和堆栈溢出,但仍然没有帮助。任何帮助将不胜感激。

我在viewDidAppear

中调用了[self playMovie]方法
-(void)playMovie
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"avc_intro" ofType:@"mp4"]];
moviePlayer =  [[MPMoviePlayerController alloc]
                initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayer];
         name:MPMoviePlayerWillExitFullscreenNotification
                                          object:moviePlayer];

moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *videoplayer = [notification object];
[[NSNotificationCenter defaultCenter]
 removeObserver:self
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:videoplayer];

if ([videoplayer
     respondsToSelector:@selector(setFullscreen:animated:)])
{
    [videoplayer.view removeFromSuperview];
}
}

更好用this. MPMoviePlayerController 已在 iOS 9.

中正式弃用

viewDidload 调用 [self playMovie] 因为 viewDidAppear 在您删除 MPMoviePlayerController 时再次调用,所以它将再次启动。

更新:

您应该展示 MPMusicPlayerController 而不是将其视图添加到超级视图并在完成时关闭它

希望这会有所帮助:)

使用 AVPlayerViewController,我的代码如下,

objective-C代码: 声明 AVPlayerViewController *playerViewController;

    playerViewController = [[AVPlayerViewController alloc]init];
    playerViewController.view.frame = videoView.bounds;
    [playerViewController.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
    playerViewController.player = [AVPlayer playerWithURL:[NSURL URLWithString:viewModel.videoUrl]];
    [videoView addSubview:playerViewController.view];
    [playerViewController.player play];
    playerViewController.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

不要忘记下面给出的框架,

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import <MediaPlayer/MediaPlayer.h>

swift代码:

        let type : String! = "mp4"
        let targetURL : String? = NSBundle.mainBundle().pathForResource("Official Apple MacBook Air Video   YouTube", ofType: "mp4")

        let videoURL = NSURL(fileURLWithPath:targetURL!)


        let player = AVPlayer(URL: videoURL)
        let playerController = AVPlayerViewController()

        playerController.player = player
        self.addChildViewController(playerController)
        self.playView.addSubview(playerController.view)
        playerController.view.frame = playView.bounds

        player.play()

不要忘记下面给出的框架,

import UIKit
import AVKit
import AVFoundation
import MediaPlayer

它对我有用,希望它有用