如何使用 XCDYouTubeKit for Swift 在播放列表中播放下一个 youtube 视频?

Posted

技术标签:

【中文标题】如何使用 XCDYouTubeKit for Swift 在播放列表中播放下一个 youtube 视频?【英文标题】:How to play the next youtube video in a playlist using XCDYouTubeKit for Swift? 【发布时间】:2016-02-27 05:24:47 【问题描述】:

我正在使用 XCDYouTubeKit,它是 MPMoviePlayerViewController 的子类。

我在播放播放列表中的下一个视频时遇到问题。当我点击前进按钮时,播放器的状态已停止,不知道如何恢复?

这是我的代码:

override func viewDidLoad()

    super.viewDidLoad()

    youtubeVideoPlayer = XCDYouTubeVideoPlayerViewController(videoIdentifier: videoID)

    self.view.addSubview( (youtubeVideoPlayer?.view)! )

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "moviePlayerNowPlayingMovieDidChange:",
        name: MPMoviePlayerNowPlayingMovieDidChangeNotification,
        object: youtubeVideoPlayer?.moviePlayer)

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "moviePlayerLoadStateDidChange:",
        name: MPMoviePlayerLoadStateDidChangeNotification,
        object: youtubeVideoPlayer?.moviePlayer)

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "moviePlayerPlaybackDidChange:",
        name: MPMoviePlayerPlaybackStateDidChangeNotification,
        object: youtubeVideoPlayer?.moviePlayer)

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "moviePlayerPlayBackDidFinish:",
        name: MPMoviePlayerPlaybackDidFinishNotification,
        object: youtubeVideoPlayer?.moviePlayer)


func moviePlayerPlayBackDidFinish(notification: NSNotification)

    print("moviePlayerPlayBackDidFinish")
    youtubeVideoPlayer!.view.removeFromSuperview()

    self.dismissViewControllerAnimated(true, completion: nil);


func moviePlayerNowPlayingMovieDidChange(notification: NSNotification)

    print("moviePlayerNowPlayingMovieDidChange")


func moviePlayerLoadStateDidChange(notification: NSNotification)

    print("moviePlayerLoadStateDidChange")
    youtubeVideoPlayer?.moviePlayer.play()

    let state: MPMoviePlaybackState = (youtubeVideoPlayer?.moviePlayer.playbackState)!

    switch state
    
    case MPMoviePlaybackState.Stopped:
        print("stopped")
        break

    case MPMoviePlaybackState.Interrupted:
        print("interrupted")
        break

    case MPMoviePlaybackState.SeekingForward:
        print("forward")
        break

    case MPMoviePlaybackState.SeekingBackward:
        print("backward")
        break

    case MPMoviePlaybackState.Paused:
        print("paused")
        break

    case MPMoviePlaybackState.Playing:
        print("playing")
        break
       


func moviePlayerPlaybackDidChange(notification: NSNotification)

    print("moviePlayerPlaybackDidChange")

    let state: MPMoviePlaybackState = (youtubeVideoPlayer?.moviePlayer.playbackState)!

    switch state
    
        case MPMoviePlaybackState.Stopped:
        print("stopped")
        videoIDIndex = videoIDIndex + 1
        playNextVideo()
        break

        case MPMoviePlaybackState.Interrupted:
        print("interrupted")
        break

        case MPMoviePlaybackState.SeekingForward:
        print("forward")
        break

        case MPMoviePlaybackState.SeekingBackward:
        print("backward")
        break

        case MPMoviePlaybackState.Paused:
        print("paused")
        break

        case MPMoviePlaybackState.Playing:
        print("playing")
        break
    


func playNextVideo()

    if segueIdentifer == "SegueFromPlaylistVideos"
    
        print("Playlist")
        NSNotificationCenter.defaultCenter().removeObserver(
            self,
            name: MPMoviePlayerPlaybackDidFinishNotification,
            object: youtubeVideoPlayer?.moviePlayer
        )

        print("videoIDs_ARRAY = \(videoIDs_ARRAY)")
        print("videoIDs_ARRAY[videoIDIndex] = \( (videoIDs_ARRAY[videoIDIndex] as String) )")

        youtubeVideoPlayer?.videoIdentifier = videoIDs_ARRAY[videoIDIndex] as String
    

播放列表中播放第一个视频时的顺序如下,我点击前进移动到下一个视频:

moviePlayerNowPlayingMovieDidChange

moviePlayerLoadStateDidChange

moviePlayerPlaybackDidChange

moviePlayerLoadStateDidChange

moviePlayerPlaybackDidChange

停止

播放列表

videoIDs_ARRAY = ["NztC0z1JNq0", "uqRBijuCZd8"]

videoIDs_ARRAY[videoIDIndex] = uqRBijuCZd8

moviePlayerLoadStateDidChange

停止

正如你在最后看到的,播放器的状态是stopped

我不知道如何恢复它,所以它会播放下一个视频。我尝试在moviePlayerLoadStateDidChange 中添加youtubeVideoPlayer?.moviePlayer.play(),但播放器无法继续播放。

对此有什么想法吗?

谢谢

【问题讨论】:

【参考方案1】:
I don't know syntax in Swift so please translate objective-c to swift 

I have 2 solutions for you problem :- 

    1. You can stop current playing video syntax in objective-c like 
    [self.youtubeVideoPlayer.moviePlayer stop]; 
    and then you can set new video 

    self.youtubeVideoPlayer.videoIdentifier=videoIdentifier;
            self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;
            [self.youtubeVideoPlayer presentInView:self.playerView];
            [self.youtubeVideoPlayer.moviePlayer prepareToPlay];
            self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;

     2. You can do like this 
    set nil to your XCDYouTubeVideoPlayerViewController reference and set it again 

    self.youtubeVideoPlayer = nil;
    self.youtubeVideoPlayer = [[XCDYouTubeVideoPlayerViewController alloc] init];
            self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;
            self.youtubeVideoPlayer.moviePlayer.controlStyle= MPMovieControlStyleNone;
            self.youtubeVideoPlayer.moviePlayer.scalingMode = MPMovieScalingModeFill;

     and then you can set new video 

    self.youtubeVideoPlayer.videoIdentifier=videoIdentifier;
            self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;
            [self.youtubeVideoPlayer presentInView:self.playerView];
            [self.youtubeVideoPlayer.moviePlayer prepareToPlay];
            self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;

Example: -
#import "ViewController.h"

#import "XCDYouTubeVideoPlayerViewController.h"

@interface ViewController ()

    NSArray *urlArray;
    int arrindex;

@property (weak, nonatomic) IBOutlet UIView *playerView;
@property (nonatomic, strong) XCDYouTubeVideoPlayerViewController *videoPlayerViewController;
@end

@implementation ViewController

- (void)viewDidLoad 
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    urlArray = [[NSArray alloc] initWithObjects:@"zVyEPNIR-yk",@"r5RXlY3at-E",@"sUFKakydp2c",@"1xvLHxFUwIk", nil];
    arrindex = 0;
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(moviePlayerPlaybackStateDidChange1:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    [defaultCenter addObserver:self selector:@selector(moviePlayerLoadStateDidChange1:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
    [self setVideo];


- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

- (void)setVideo

    self.videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] init];
    self.videoPlayerViewController.moviePlayer.shouldAutoplay=TRUE;
    self.videoPlayerViewController.moviePlayer.controlStyle= MPMovieControlStyleNone;
    self.videoPlayerViewController.moviePlayer.scalingMode = MPMovieScalingModeFill;

    [self.playerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    NSString *videoIdentifier =[urlArray objectAtIndex:arrindex];
    self.videoPlayerViewController.videoIdentifier=videoIdentifier;

    self.videoPlayerViewController.moviePlayer.shouldAutoplay=TRUE;
    [self.videoPlayerViewController presentInView:self.playerView];
    [self.videoPlayerViewController.moviePlayer prepareToPlay];
    self.videoPlayerViewController.moviePlayer.shouldAutoplay=TRUE;

- (IBAction)Next:(id)sender 
        arrindex++;
    if (arrindex<urlArray.count) 
        self.videoPlayerViewController=nil;
        [self setVideo];
    

- (void) moviePlayerLoadStateDidChange1:(NSNotification *)notification

    MPMoviePlayerController *moviePlayerController = notification.object;
    MPMovieLoadState state = moviePlayerController.loadState;
    if (state & MPMovieLoadStatePlayable)
    
        [self.videoPlayerViewController.moviePlayer play];
    
    else if (state & MPMovieLoadStatePlaythroughOK)
    
    
    else if (state & MPMovieLoadStateStalled)
    
    


- (void) moviePlayerPlaybackStateDidChange1:(NSNotification *)notification

    long reason=(long)self.videoPlayerViewController.moviePlayer.playbackState;
    if (reason==0)
    
        NSLog(@"I'm Not Playing");
    
    else if (reason==1)
    
        NSLog(@"I'm Playingggggggggggggggg");
    
    else if (reason ==2)
    
        NSLog(@"pause....................................");
    
    else if (reason ==3)
    
        //NSLog(@"Interrupteddddddddddddddddd");
    
    else if (reason ==4)
    
        //NSLog(@"Seeking Forward");
    
    else if (reason ==5)
    
        //NSLog(@"Seeking Backward");
    


@end

I hope this helps you.

【讨论】:

它“暂时”工作......当我点击前进按钮时,它会播放下一个视频,但它在播放下一个视频时处于 MPMoviePlaybackState.Stopped 状态,而不是处于 @987654323 @ 状态。然后当我再次点击前进按钮时,它什么也不做,只是视频停止。 您尝试过哪种解决方案 1 或 2? 我已经在我的项目中实现了保存编码,它工作正常。可能是你在某个地方犯了小错误。所以请再次检查或将您的最新代码放在这里,以便我可以帮助您。 你能把你的 Obj-C 代码贴在 github 上让我看看吗?...你不必发布所有代码,就这一部分?...我喜欢跑对其进行测试并转换为 Swift。目前,我尝试了两种解决方案,问题如我之前的声明所述。 我已经用示例编辑了我的答案。参考那个。我希望这些代码对您有所帮助。

以上是关于如何使用 XCDYouTubeKit for Swift 在播放列表中播放下一个 youtube 视频?的主要内容,如果未能解决你的问题,请参考以下文章

参考论坛:Mali kernel driver TX011-SW-99002-r5p1-00rel0 for firefly

Xcode Swift 在本机视频播放器上播放 youtube 视频?

我如何将数组中的值作为变量引用?

swaks如何不显示代发

如何在布局 activity_main(sw320dp) 中调用?

使用 sw_hide 执行外部 exe