Ios 如何使用XCDYouTubeKit for Swift在播放列表中播放下一个youtube视频?
我使用的是XCDYouTubeKit,它是作为Ios 如何使用XCDYouTubeKit for Swift在播放列表中播放下一个youtube视频?,ios,swift,mpmovieplayercontroller,Ios,Swift,Mpmovieplayercontroller,我使用的是XCDYouTubeKit,它是作为mpmovieplayervewcontroller的子类编写的 播放播放列表中的下一个视频时出现问题。当我点击“前进”按钮时,播放机的状态停止,我不知道如何继续 这是我的密码: override func viewDidLoad() { super.viewDidLoad() youtubeVideoPlayer = XCDYouTubeVideoPlayerViewController(videoIdentifier: vide
mpmovieplayervewcontroller
的子类编写的
播放播放列表中的下一个视频时出现问题。当我点击“前进”按钮时,播放机的状态停止,我不知道如何继续
这是我的密码:
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
}
}
在播放列表中播放第一个视频时,顺序如下,我点击“前进”键以转到下一个视频:
电影播放器正在播放电影更改
电影播放器加载状态改变
影剧演员回放
演奏
演奏
电影播放器加载状态改变
演奏
影剧演员回放
停止
播放列表
videoIDs_数组=[“NztC0z1JNq0”,“uqRBijuCZd8”]
videoIDs_数组[VIDEOIDIDEX]=uqRBijuCZd8
电影播放器加载状态改变
停止
正如您在结尾所看到的,播放机的状态为已停止
我不知道如何恢复它,以便它将播放下一个视频。我已尝试在moviePlayer加载状态更改中添加youtubeVideoPlayer?.moviePlayer.play()
,但播放机未恢复播放
有什么想法吗
谢谢我不知道Swift的语法,所以请将objective-c翻译成Swift
我有两种解决方案:
1.您可以像objective-c一样停止当前播放视频语法
[self.youtubeVideoPlayer.moviePlayer停止];
然后你可以设置新的视频
self.youtubeVideoPlayer.videoIdentifier=videoIdentifier;
self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;
[self.youtubeVideoPlayer presentview:self.playerView];
[self.youtubeVideoPlayer.moviePlayer prepareToPlay];
self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;
2.你可以这样做
将XCDYouTubeVideoPlayerViewController引用设置为nil,然后再次设置
self.youtubeVideoPlayer=nil;
self.youtubeVideoPlayer=[[xcdyotubevideoplayervicewcontroller alloc]init];
self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;
self.youtubeVideoPlayer.moviePlayer.controlStyle=MPMovieControlStyleNone;
self.youtubeVideoPlayer.moviePlayer.scalingMode=MPMovieScalingModeFill;
然后你可以设置新的视频
self.youtubeVideoPlayer.videoIdentifier=videoIdentifier;
self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;
[self.youtubeVideoPlayer presentview:self.playerView];
[self.youtubeVideoPlayer.moviePlayer prepareToPlay];
self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;
例如:-
#导入“ViewController.h”
#导入“XCDYouTubeVideoPlayerViewController.h”
@界面视图控制器()
{
NSArray*urlArray;
综合指数;
}
@属性(弱、非原子)IBUIView*playerView;
@属性(非原子,强)XCDYouTubeVideoPlayerViewController*videoPlayerViewController;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
URLARY=[[NSArray alloc]initWithObjects:@“zVyEPNIR yk”@“r5RXlY3at-E”@“sUFKakydp2c”@“1xvLHxFUwIk”,nil];
ARR指数=0;
NSNotificationCenter*defaultCenter=[NSNotificationCenter defaultCenter];
[defaultCenter addObserver:自选择器:@selector(moviePlayerPlaybackStateDidChange1:)名称:MPMoviePlayerPlaybackStateDidChangeNotification对象:nil];
[defaultCenter addObserver:自选择器:@selector(moviePlayerLoadStateDidChange1:)名称:MPMoviePlayerLoadStateDidChangeNotification对象:nil];
[自我设置视频];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)设置视频
{
self.videoplayervewcontroller=[[xcdyoutubevideoplayervewcontroller alloc]init];
self.videoplayervewcontroller.moviePlayer.shouldAutoplay=TRUE;
self.videoplayervewcontroller.moviePlayer.controlStyle=MPMovieControlStyleNone;
self.videoplayervewcontroller.moviePlayer.scalingMode=MPMovieScalingModeFill;
[self.playerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
NSString*videoIdentifier=[URLARY objectAtIndex:ARRDINDEX];
self.videoplayervewcontroller.videoIdentifier=videoIdentifier;
self.videoplayervewcontroller.moviePlayer.shouldAutoplay=TRUE;
[self.videoplayervcontroller presentview:self.playerView];
[self.videoplayervewcontroller.moviePlayer prepareToPlay];
self.videoplayervewcontroller.moviePlayer.shouldAutoplay=TRUE;
}
-(iAction)下一步:(id)发送方{
arrindex++;
如果我不知道Swift的语法,请将objective-c翻译成Swift
我有两种解决方案:
1.您可以像objective-c一样停止当前播放视频语法
[self.youtubeVideoPlayer.moviePlayer停止];
然后你可以设置新的视频
self.youtubeVideoPlayer.videoIdentifier=videoIdentifier;
self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;
[self.youtubeVideoPlayer presentview:self.playerView];
[self.youtubeVideoPlayer.moviePlayer prepareToPlay];
self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;
2.你可以这样做
将XCDYouTubeVideoPlayerViewController引用设置为nil,然后再次设置
self.youtubeVideoPlayer=nil;
self.youtubeVideoPlayer=[[xcdyotubevideoplayervicewcontroller alloc]init];
self.youtubeVideoPlayer.moviePlayer.shouldAutoplay=TRUE;
self.youtubeVideoPlayer.moviePlayer.controlStyle=MPMovieControlStyleNone;
self.youtubeVideoPlayer.moviePlayer.scalingMode=MPMovieScalingModeFill;
然后你可以设置新的视频
self.youtubeVideoPlayer.videoIdentif
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.