Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 视频自动播放-ContainerView-在视图之间切换-Swift 3_Ios_Swift_Uicontainerview_Segmentedcontrol - Fatal编程技术网

Ios 视频自动播放-ContainerView-在视图之间切换-Swift 3

Ios 视频自动播放-ContainerView-在视图之间切换-Swift 3,ios,swift,uicontainerview,segmentedcontrol,Ios,Swift,Uicontainerview,Segmentedcontrol,我有一个用于在段之间切换的。我使用container view在这些片段之间切换,效果很好,但我的一个选项卡上有一个视频,可以在viewdideappear上自动播放。所以我的问题是,当容器视图加载所有之前的内容并根据ishiden=false显示视图时,即使未选择该片段,我的视频也会开始播放。我该如何处理这种情况 这是我在segmentedControlValueChanged事件中的代码 print("selected index \(segmentedControl.selectedSeg

我有一个用于在段之间切换的。我使用container view在这些片段之间切换,效果很好,但我的一个选项卡上有一个视频,可以在
viewdideappear
上自动播放。所以我的问题是,当容器视图加载所有之前的内容并根据
ishiden=false
显示视图时,即使未选择该片段,我的视频也会开始播放。我该如何处理这种情况

这是我在
segmentedControlValueChanged
事件中的代码

print("selected index \(segmentedControl.selectedSegmentIndex)")

    switch segmentedControl.selectedSegmentIndex {
    case 0:
        liveContainer.isHidden = true
    case 1:
        liveContainer.isHidden = true
    case 2:
        liveContainer.isHidden = false
    default:
        break
    }

请尝试
NSNotificationCenter

收到文件时:

-viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"NOTIFICATIONID" object:nil];
和创建方法以接收通知

-(void)receiveNotification:(NSNotification *) notification{
    if ([notification.name isEqualToString:@"NOTIFICATIONID"])
    {
        NSDictionary* userInfo = notification.userInfo;
        NSNumber* segmentID = (NSNumber*)userInfo[@"segmentID"];
        NSLog (@"Successfully received test notification! %i", segmentID.intValue);
    }
}
张贴通知:

NSDictionary* userInfo = @{@"segmentID": @(segmentID)}; //Used to pass Objects to Notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONID" object:self userInfo:userInfo];

禁用当前自动播放。当你收到通知时,只需播放你的视频。仅当您选择段时发布通知。

请尝试
NSNotificationCenter

收到文件时:

-viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"NOTIFICATIONID" object:nil];
和创建方法以接收通知

-(void)receiveNotification:(NSNotification *) notification{
    if ([notification.name isEqualToString:@"NOTIFICATIONID"])
    {
        NSDictionary* userInfo = notification.userInfo;
        NSNumber* segmentID = (NSNumber*)userInfo[@"segmentID"];
        NSLog (@"Successfully received test notification! %i", segmentID.intValue);
    }
}
张贴通知:

NSDictionary* userInfo = @{@"segmentID": @(segmentID)}; //Used to pass Objects to Notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONID" object:self userInfo:userInfo];

禁用当前自动播放。当你收到通知时,只需播放你的视频。仅在选择片段时发布通知。

您可以使用
NSNotificationCenter
在显示/隐藏视频时向包含视频的视图控制器发送通知,以播放/停止视频。这可以在容器视图段选择中完成。因此,您可以从
viewDidAppear
中删除autoplay,并将其添加到发送通知时调用的方法中

例如,在
segmentedControlValueChanged
事件中,您可以编写:

switch segmentedControl.selectedSegmentIndex {
case 0:
    liveContainer.isHidden = true
    NotificationCenter.default.post(name: Notification.Name("StopVideo"), object: nil)
case 1:
    liveContainer.isHidden = true
    NotificationCenter.default.post(name: Notification.Name("StopVideo"), object: nil)
case 2:
    liveContainer.isHidden = false
    NotificationCenter.default.post(name: Notification.Name("PlayVideo"), object: nil)
default:
    break
}
在视频
ViewController
中,您可以有两种方法:一种用于播放视频:

func playVideo() {
  //play video here
}
还有一个是用来阻止它的:

func stopVideo() {
  //stop video here
}
在视频
ViewController
viewDidLoad
方法中,您可以添加观察者:

NotificationCenter.default.addObserver(self, selector: #selector(playVideo), name: "PlayVideo", object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(stopVideo), name: "StopVideo", object: nil)

您可以使用
NSNotificationCenter
在显示/隐藏视频时向包含视频的视图控制器发送通知,以播放/停止视频。这可以在容器视图段选择中完成。因此,您可以从
viewDidAppear
中删除autoplay,并将其添加到发送通知时调用的方法中

例如,在
segmentedControlValueChanged
事件中,您可以编写:

switch segmentedControl.selectedSegmentIndex {
case 0:
    liveContainer.isHidden = true
    NotificationCenter.default.post(name: Notification.Name("StopVideo"), object: nil)
case 1:
    liveContainer.isHidden = true
    NotificationCenter.default.post(name: Notification.Name("StopVideo"), object: nil)
case 2:
    liveContainer.isHidden = false
    NotificationCenter.default.post(name: Notification.Name("PlayVideo"), object: nil)
default:
    break
}
在视频
ViewController
中,您可以有两种方法:一种用于播放视频:

func playVideo() {
  //play video here
}
还有一个是用来阻止它的:

func stopVideo() {
  //stop video here
}
在视频
ViewController
viewDidLoad
方法中,您可以添加观察者:

NotificationCenter.default.addObserver(self, selector: #selector(playVideo), name: "PlayVideo", object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(stopVideo), name: "StopVideo", object: nil)

你能把你的代码片段放在这里吗?你想什么时候播放视频?一旦选项卡/片段的页面可见,那你为什么要自动播放视频??暂停视频并在片段更改时播放。我已经编写了在ViewDidDisplay中加载视频的代码,但在单击相应的片段之前它才开始播放。您可以将代码片段放在这里吗?何时播放视频?一旦选项卡/片段的页面可见,为什么要自动播放视频??暂停视频并在片段更改时播放。我已经编写了在ViewDidDisplay中加载视频的代码,但它只是在单击相应的片段之前开始播放。即使我的视图为hiddenNo,也会调用这两种方法。请给我一个例子,即使我的视图是hiddenNo,也会调用这两个方法。请给我举个例子请给我举个例子