查看: 149|回复: 0

【若何快速的开辟一个完整的iOS直播app】(采集篇)

[复制链接]
发表于 2016-9-9 16:22:52 |未经授权,严禁转载,违者必究... | |阅读模式
若何 开辟 采集 直播 完整 火车头采集数据不完整 织梦采集 图片不完整 饥荒快速采集mod 饥荒快速采集 饥荒联机快速采集mod 饥荒海难快速采集mod 快速采集mod 饥荒联机版快速采集 饥荒快速采集修改
媒介
在看这篇之前,假如您还不领会直播道理,请查看这篇文章

304825-0bc60fa1df16fd70.png

304825-0bc60fa1df16fd70.png

忽略本人.png
根基常识介绍

  • AVFoundation: 音视频数据采集需要用AVFoundation框架.
    // 捕捉音视频
    - (void)setupCaputureVideo
    {
        // 1.建立捕捉会话,必需要强引用,不然会被释放
        AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
        _captureSession = captureSession;
        // 2.获取摄像头设备,默认是后置摄像头
        AVCaptureDevice *videoDevice = [self getVideoDevice:AVCaptureDevicePositionFront];
        // 3.获取声音设备
        AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
        // 4.建立对应视频设备输入对象
        AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
        _currentVideoDeviceInput = videoDeviceInput;
        // 5.建立对应音频设备输入对象
        AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
        // 6.添加到会话中
        // 留意“最好要判定是否能添加输入,会话不克不及添加空的
        // 6.1 添加视频
        if ([captureSession canAddInput:videoDeviceInput]) {
            [captureSession addInput:videoDeviceInput];
        }
        // 6.2 添加音频
        if ([captureSession canAddInput:audioDeviceInput]) {
            [captureSession addInput:audioDeviceInput];
        }
        // 7.获取视频数据输出设备
        AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];
        // 7.1 设置代办署理,捕捉视频样品数据
        // 留意:队列必需是串行队列,才能获取到数据,并且不克不及为空
        dispatch_queue_t videoQueue = dispatch_queue_create("Video Capture Queue", DISPATCH_QUEUE_SERIAL);
        [videoOutput setSampleBufferDelegate:self queue:videoQueue];
        if ([captureSession canAddOutput:videoOutput]) {
            [captureSession addOutput:videoOutput];
        }
        // 8.获取音频数据输出设备
        AVCaptureAudioDataOutput *audioOutput = [[AVCaptureAudioDataOutput alloc] init];
        // 8.2 设置代办署理,捕捉视频样品数据
        // 留意:队列必需是串行队列,才能获取到数据,并且不克不及为空
        dispatch_queue_t audioQueue = dispatch_queue_create("Audio Capture Queue", DISPATCH_QUEUE_SERIAL);
        [audioOutput setSampleBufferDelegate:self queue:audioQueue];
        if ([captureSession canAddOutput:audioOutput]) {
            [captureSession addOutput:audioOutput];
        }
        // 9.获取视频输入与输出毗连,用于分辩音视频数据
        _videoConnection = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
        // 10.添加视频预览图层
        AVCaptureVideoPreviewLayer *previedLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
        previedLayer.frame = [UIScreen mainScreen].bounds;
        [self.view.layer insertSublayer:previedLayer atIndex:0];
        _previedLayer = previedLayer;
        // 11.启动会话
        [captureSession startRunning];
    }
    // 指定摄像头标的目的获取摄像头
    - (AVCaptureDevice *)getVideoDevice:(AVCaptureDevicePosition)position
    {
        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        for (AVCaptureDevice *device in devices) {
            if (device.position == position) {
                return device;
            }
        }
        return nil;
    }
    #pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
    // 获取输入设备数据,有可能是音频有可能是视频
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
    {
        if (_videoConnection == connection) {
            NSLog(@"采集到视频数据");
        } else {
            NSLog(@"采集到音频数据");
        }
    }
    视频采集额外功能一(切换摄像头)
  • 切换摄像头步调

  • 1.获取当前视频设备输入对象

  • 2.判定当前视频设备是前置仍是后置

  • 3.确定切换摄像头的标的目的

  • 4.按照摄像头标的目的获取对应的摄像头设备

  • 5.建立对应的摄像头输入对象

  • 6.从会话中移除之前的视频输入对象

  • 7.添加新的视频输入对象到会话中


    // 切换摄像头
    - (IBAction)toggleCapture:(id)sender {
        // 获取当前设备标的目的
        AVCaptureDevicePosition curPosition = _currentVideoDeviceInput.device.position;
        // 获取需要改变的标的目的
        AVCaptureDevicePosition togglePosition = curPosition == AVCaptureDevicePositionFront?AVCaptureDevicePositionBack:AVCaptureDevicePositionFront;
        // 获取改变的摄像头设备
        AVCaptureDevice *toggleDevice = [self getVideoDevice:togglePosition];
        // 获取改变的摄像头输入设备
        AVCaptureDeviceInput *toggleDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:toggleDevice error:nil];
        // 移除之前摄像头输入设备
        [_captureSession removeInput:_currentVideoDeviceInput];
        // 添加新的摄像头输入设备
        [_captureSession addInput:toggleDeviceInput];
        // 记实当前摄像头输入设备
        _currentVideoDeviceInput = toggleDeviceInput;
    }
    视频采集额外功能二(聚焦光标)
  • 聚焦光标步调

  • 1.监听屏幕的点击

  • 2.获取点击的点位置,转换为摄像头上的点,必需经由过程视频预览图层(AVCaptureVideoPreviewLayer)转

  • 3.设置聚焦光标图片的位置,并做动画

  • 4.设置摄像头设备聚焦模式和曝光模式(留意:这里设置必然要锁定设置装备摆设lockForConfiguration,不然报错)


    // 点击屏幕,呈现聚焦视图
    - (void)touchesBegan:(NSSet[U] *)touches withEvent:(UIEvent *)event
    {
        // 获取点击位置
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.view];
        // 把当前位置转换为摄像头点上的位置
        CGPoint cameraPoint = [_previedLayer captureDevicePointOfInterestForPoint:point];
        // 设置聚核心光标位置
        [self setFocusCursorWithPoint:point];
        // 设置聚焦
        [self focusWithMode:AVCaptureFocusModeAutoFocus exposureMode:AVCaptureExposureModeAutoExpose atPoint:cameraPoint];
    }
    /**
    *  设置聚焦光标位置
    *
    *  @param point 光标位置
    */
    -(void)setFocusCursorWithPoint:(CGPoint)point{
        self.focusCursorImageView.center=point;
        self.focusCursorImageView.transform=CGAffineTransformMakeScale(1.5, 1.5);
        self.focusCursorImageView.alpha=1.0;
        [UIView animateWithDuration:1.0 animations:^{
            self.focusCursorImageView.transform=CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            self.focusCursorImageView.alpha=0;
        }];
    }
    /**
    *  设置聚焦
    */
    -(void)focusWithMode:(AVCaptureFocusMode)focusMode exposureMode:(AVCaptureExposureMode)exposureMode atPoint:(CGPoint)point{
        AVCaptureDevice *captureDevice = _currentVideoDeviceInput.device;
        // 锁定设置装备摆设
        [captureDevice lockForConfiguration:nil];
        // 设置聚焦
        if ([captureDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
            [captureDevice setFocusMode:AVCaptureFocusModeAutoFocus];
        }
        if ([captureDevice isFocusPointOfInterestSupported]) {
            [captureDevice setFocusPointOfInterest:point];
        }
        // 设置曝光
        if ([captureDevice isExposureModeSupported:AVCaptureExposureModeAutoExpose]) {
            [captureDevice setExposureMode:AVCaptureExposureModeAutoExpose];
        }
        if ([captureDevice isExposurePointOfInterestSupported]) {
            [captureDevice setExposurePointOfInterest:point];
        }
        // 解锁设置装备摆设
        [captureDevice unlockForConfiguration];
    }
    竣事语
    后续还会更新更多有关直播的资料,但愿做到教会每一个伴侣从零最先做一款直播app,而且Demo也会慢慢完美.
    Demo

    304825-7304194c56933d4b.png

    304825-7304194c56933d4b.png

    打开YZLiveApp.xcworkspace问题
  • pod install就能解决

    304825-02afefb2bf824f80.png

    304825-02afefb2bf824f80.png

    Snip20160830_12.png
  • 下载jkplayer库,点击下载
  • 把jkplayer直接拖入到与Classes统一级目次下,直接运行法式,就能成功了

    304825-72d50e8fd24385ea.png

    304825-72d50e8fd24385ea.png

    拖入ijkplayer到与Classes统一级目次下.png
  • 留意不需要打开工程,把jkplayer拖入到工程中,而是直接把jkplayer库拷贝到与Classes统一级目次下就可以了
  • 错误示范:不要向下面如许操作

    304825-71f908317cd243a9.png

    304825-71f908317cd243a9.png

    Snip20160830_14.png
  • 返回顶部快速回复上一主题下一主题返回列表

    声远论坛|联系电话:0537-2311005|Archiver|手机版|小黑屋|Sitemap|声远网 |网站地图|网站地图

    鲁公网安备 37089702000485号 | 鲁ICP备 18028751号 | 互联网药品信息服务资格证:(鲁)-经营性-2022-0209 | 增值电信业务经营许可证:鲁B2-20230761号 | (鲁)职介证字[223]:第08120014号

    中国互联网违法和不良信息举报中心 | 山东省互联网违法和不良信息举报中心 | 人工智能生成合成内容标识办法

    GMT+8, 2026-5-8 04:58, Processed in 0.089108 second(s), 32 queries , Gzip On, APCu On.

    Powered by Discuz! X3.5© 2001-2026 SYUAN.COM

    快速回复 返回顶部 返回列表