CreateCamera.ets 13.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
import camera from '@ohos.multimedia.camera';
import media from '@ohos.multimedia.media';
import { BusinessError } from '@ohos.base';
import { promptAction, router } from '@kit.ArkUI'
import { common } from '@kit.AbilityKit';
import { FileUtil, CommonConstants } from '../utils/FileUtil';
// import { uploadFileByTask } from '../utils/uploadCloud'
import { commonRouterParams } from '../utils/UserAuth'

/**
 * 视频录制
 */

let context = getContext(this) as common.UIAbilityContext;
let routerParamsData: commonRouterParams = router.getParams() as commonRouterParams;
@Entry
@Component
struct CreateCamera {
  @State xComponentWidth: number = 0;
  @State xComponentHeight: number = 0;
  @State recording: boolean = false;
  @State path: string = '';
  @State countTime: number = 0;
  @State cameraManager: camera.CameraManager | undefined = undefined;
  @State videoOutput: camera.VideoOutput | undefined = undefined;
  @State captureSession: camera.Session | undefined = undefined;
  @State cameraInput: camera.CameraInput | undefined = undefined;
  @State previewOutput: camera.PreviewOutput | undefined = undefined;
  @State avRecorder: media.AVRecorder | undefined = undefined;
  private mXComponentController: XComponentController = new XComponentController;
  private surfaceId: string = '';
  url: string = '';
  timer: number = 0;

  aboutToAppear() {
    // 创建存放视频文件
    this.path = context.filesDir + '/' + 'VIDEO_' + Date.parse(new Date().toString()) + '.mp4';
    let file = FileUtil.createOrOpen(this.path);
    this.url = 'fd://' + file.fd;
  }

  build() {
    Stack({ alignContent: Alignment.Top }) {
      XComponent({
        id: 'componentId',
        type: XComponentType.SURFACE,
        controller: this.mXComponentController,
      })
        .onLoad(async () => {
          this.surfaceId = this.mXComponentController.getXComponentSurfaceId();
          await this.initCamera(getContext(this), this.surfaceId);
        })
        .height(CommonConstants.SEVENTY_PERCENT)
        .margin({
          top: CommonConstants.FIFTEEN_PERCENT
        })
      Column() {
        Text(`录制时长:${this.countTime}s`).fontSize(16).fontColor(Color.White)
        Blank()
        Row() {
          Image(this.recording ? $r('app.media.camera_pause_video_4x') : $r('app.media.camera_take_video_4x'))
            .width(px2vp(CommonConstants.IMAGE_SIZE))
            .height(px2vp(CommonConstants.IMAGE_SIZE))
            .onClick(async () => {
              if (this.recording) {
                clearInterval(this.timer);
                this.timer = 0
                promptAction.showToast({ message: '已完成录制,开始上传' })
                await this.stopRecord();
                routerParamsData.videoPath = this.path;
                router.back({
                  url:'',
                  params: routerParamsData
                })
              } else {
                promptAction.showToast({ message: '已开始录制,再次点击完成录制' })
                this.timer = setInterval(async () => {
                  this.countTime++;
                  if(this.countTime >= 120){
                    clearInterval(this.timer);
                    this.timer = 0
                    promptAction.showToast({ message: '已完成录制,开始上传' })
                    await this.stopRecord();
                    routerParamsData.videoPath = this.path;
                    router.back({
                      url:'',
                      params: routerParamsData
                    })
                  }
                }, 1000);
                await this.startRecord();
              }
              this.recording = !this.recording;
            })
        }
        .width(CommonConstants.FULL_PERCENT)
        .height(120)
        .margin({ bottom: 60 })
        .justifyContent(FlexAlign.Center)
        .alignItems(VerticalAlign.Center)
      }
      .width(CommonConstants.FULL_PERCENT)
      .height(CommonConstants.FULL_PERCENT)
      .justifyContent(FlexAlign.Start)
      .alignItems(HorizontalAlign.Start)
    }
    .backgroundColor(Color.Black)
    .width(CommonConstants.FULL_PERCENT)
    .height(CommonConstants.FULL_PERCENT)
  }

  // 初始化相机
  async initCamera(context: common.Context, surfaceId: string) {
    this.cameraManager = camera.getCameraManager(context);
    if (!this.cameraManager) {
      promptAction.showToast({ message: 'camera.getCameraManager error' })
      return;
    }

    this.cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => {
      promptAction.showToast({ message: `camera : ${cameraStatusInfo.camera.cameraId},status:  ${cameraStatusInfo.status}` })
    });

    let cameraArray: Array<camera.CameraDevice> = [];
    try {
      cameraArray = this.cameraManager.getSupportedCameras();
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `getSupportedCameras call failed. error code: ${err.code}` })
    }

    if (cameraArray.length <= 0) {
      promptAction.showToast({ message: 'cameraManager.getSupportedCameras error' })
      return;
    }

    let cameraOutputCap: camera.CameraOutputCapability =
      this.cameraManager.getSupportedOutputCapability(cameraArray[0], camera.SceneMode.NORMAL_VIDEO);
    if (!cameraOutputCap) {
      promptAction.showToast({ message: 'cameraManager.getSupportedOutputCapability error' })
      return;
    }
    promptAction.showToast({ message: 'outputCapability: ' + JSON.stringify(cameraOutputCap) })

    let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles;
    if (!previewProfilesArray) {
      promptAction.showToast({ message: 'createOutput previewProfilesArray === null || undefined' })
    }

    let photoProfilesArray: Array<camera.Profile> = cameraOutputCap.photoProfiles;
    if (!photoProfilesArray) {
      promptAction.showToast({ message: 'createOutput photoProfilesArray === null || undefined' })
    }

    let videoProfilesArray: Array<camera.VideoProfile> = cameraOutputCap.videoProfiles;
    if (!videoProfilesArray) {
      promptAction.showToast({ message: 'createOutput videoProfilesArray === null || undefined' })
    }

    let videoSize: camera.Size = {
      width: 640,
      height: 480
    }
    let videoProfile: undefined | camera.VideoProfile = videoProfilesArray.find((profile: camera.VideoProfile) => {
      return profile.size.width === videoSize.width && profile.size.height === videoSize.height;
    });

    if (!videoProfile) {
      promptAction.showToast({ message: 'videoProfile is not found' })
      return;
    }

    let aVRecorderProfile: media.AVRecorderProfile = {
      audioBitrate: 48000,
      audioChannels: 2,
      audioCodec: media.CodecMimeType.AUDIO_AAC,
      audioSampleRate: 48000,
      fileFormat: media.ContainerFormatType.CFT_MPEG_4,
      videoBitrate: 2000000,
      videoCodec: media.CodecMimeType.VIDEO_AVC,
      videoFrameWidth: videoSize.width,
      videoFrameHeight: videoSize.height,
      videoFrameRate: 30
    };

    let aVRecorderConfig: media.AVRecorderConfig = {
      audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC,
      videoSourceType: media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV,
      profile: aVRecorderProfile,
      url: this.url,
      rotation: 0,
      location: {
        latitude: 30,
        longitude: 130
      }
    };

    try {
      this.avRecorder = await media.createAVRecorder();
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `createAVRecorder call failed. error code: ${err.code}` })
    }

    if (this.avRecorder === undefined) {
      return;
    }

    try {
      await this.avRecorder.prepare(aVRecorderConfig);
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `prepare call failed. error code: ${err.code}` })
    }

    let videoSurfaceId: string | undefined = undefined;
    try {
      videoSurfaceId = await this.avRecorder.getInputSurface();
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `getInputSurface call failed. error code: ${err.code}` })
    }
    if (videoSurfaceId === undefined) {
      return;
    }

    try {
      this.videoOutput = this.cameraManager.createVideoOutput(videoProfile, videoSurfaceId);
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `Failed to create the videoOutput instance. error: ${JSON.stringify(err)}` })
    }
    if (this.videoOutput === undefined) {
      return;
    }
    this.videoOutput.on('frameStart', () => {
      promptAction.showToast({ message: 'Video frame started' })
    });

    this.videoOutput.on('error', (error: BusinessError) => {
      promptAction.showToast({ message: `Video frame error code: ${error.code}` })
    });

    try {
      this.captureSession = this.cameraManager.createSession(camera.SceneMode.NORMAL_VIDEO) as camera.VideoSession;
      ;
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `Failed to create the CaptureSession instance. errorCode = ${err.code}` })
    }
    if (this.captureSession === undefined) {
      return;
    }

    try {
      this.captureSession.beginConfig();
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `Failed to beginConfig. errorCode = ${err.code}` })
    }

    let cameraInput: camera.CameraInput | undefined = undefined;
    try {
      cameraInput = this.cameraManager.createCameraInput(cameraArray[0]);
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `Failed to createCameraInput. errorCode = ${err.code}` })
    }
    if (cameraInput === undefined) {
      return;
    }

    let cameraDevice: camera.CameraDevice = cameraArray[0];
    cameraInput.on('error', cameraDevice, (error: BusinessError) => {
      promptAction.showToast({ message: `Camera input error code: ${error.code}` })
    });

    try {
      await cameraInput.open();
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `Failed to open cameraInput. errorCode = ${err.code}` })
    }

    try {
      this.captureSession.addInput(cameraInput);
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `Failed to add cameraInput. errorCode = ${err.code}` })
    }

    let previewOutput: camera.PreviewOutput | undefined = undefined;
    try {
      previewOutput = this.cameraManager.createPreviewOutput(videoProfile, surfaceId);
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `Failed to create the previewOutput instance. error: ${JSON.stringify(err)}` })
    }

    if (previewOutput === undefined) {
      return;
    }

    try {
      this.captureSession.addOutput(previewOutput);
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `Failed to add previewOutput. errorCode = ${err.code}` })
    }

    try {
      this.captureSession.addOutput(this.videoOutput);
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `Failed to add videoOutput. errorCode = ${err.code}` })
    }

    try {
      await this.captureSession.commitConfig();
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `Failed to commitConfig. errorCode = ${err.code}` })
    }

    try {
      await this.captureSession.start();
    } catch (error) {
      let err = error as BusinessError;
      promptAction.showToast({ message: `Failed to start captureSession. errorCode = ${err.code}` })
    }

    this.videoOutput.start((err: BusinessError) => {
      if (err) {
        promptAction.showToast({ message: `Failed to start the video output. error: ${JSON.stringify(err)}` })
        return;
      }
      promptAction.showToast({message: 'Callback invoked to indicate the video output start success.'})
    });
  }

  // 开始录像
  async startRecord() {
    if (this.avRecorder) {
      try {
        await this.avRecorder.start();
      } catch (error) {
        let err = error as BusinessError;
        promptAction.showToast({ message: `avRecorder start error: ${JSON.stringify(err)}` })
      }
    }
  }

  // 停止录像
  async stopRecord() {
    if (this.avRecorder) {
      try {
        if (this.videoOutput) {
          this.videoOutput.stop((err: BusinessError) => {
            if (err) {
              promptAction.showToast({ message: `Failed to stop the video output. error: ${JSON.stringify(err)}` })
              return;
            }
            promptAction.showToast({message: 'Callback invoked to indicate the video output stop success.'})
          });
        }
        try {
          await this.avRecorder.stop();
          await this.avRecorder.release();
        } catch (error) {
          let err = error as BusinessError;
          promptAction.showToast({ message: `avRecorder stop error: ${JSON.stringify(err)}` })
        }
      } catch (error) {
        let err = error as BusinessError;
        promptAction.showToast({ message: `avRecorder stop error: ${JSON.stringify(err)}` })
      }
      try {
        if (this.captureSession) {
          this.captureSession.stop();
        }
        if (this.cameraInput) {
          this.cameraInput.close();
        }
        if (this.previewOutput) {
          this.previewOutput.release();
        }
        if (this.videoOutput) {
          this.videoOutput.release();
        }
        if (this.captureSession) {
          this.captureSession.release();
        }
        if (this.captureSession) {
          this.captureSession = undefined;
        }
      } catch (error) {
        let err = error as BusinessError;
        promptAction.showToast({ message: `avRecorder stop error: ${JSON.stringify(err)}` })
      }
    }
  }
}