摇晃时播放视频,再次摇晃时停止视频-iOS



我是iOS的新手。我正在尝试使用加速度计检测进行采样。每当我摇晃设备时,视频应该能够"播放",每当我再次摇晃时,它应该能够"停止"播放媒体。我在谷歌上搜索,但找不到任何样本。请帮助示例代码。

提前致谢

以下方法可以做到。

    - (BOOL)canBecomeFirstResponder 
    { 
      return YES;
    }

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
    { 
      if (motion == UIEventSubtypeMotionShake) 
      { 
    if(VideoPlaying)
    {
    //Stop video here
    }
    else
    {
    //PLay video here
    }
    } 
    }

另请参阅此处的 github 代码。

- (void)viewDidAppear:(BOOL)animated {
    [self becomeFirstResponder];
     VideoPlay=NO;//take it BOOL global.
}
- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
  // for UIEventTypeMotion, available in iPhone OS 3.0
  // UIEventSubtypeMotionShake = 1,
  if (motion == UIEventSubtypeMotionShake) 
  { 
    if(!VideoPlay)
    {
      VideoPlay=YES; 
      //Play yourvideo 
    }
    else
    {
       VideoPlay=NO;
      //Stop your video 
    }
  }
}

对于播放视频自定义,请检查它。

愿它会帮助你。

快乐编码..:)

最新更新