HarmonyOS鸿蒙Next相机开发报错 prepare call failed. error code: 5400102
相机开发:先打开拍照然后转录制时,报错:prepare call failed. error code: 5400102。从哪里可以查到状态机是哪里不对
5 回复
更多关于HarmonyOS鸿蒙Next相机开发报错 prepare call failed. error code: 5400102的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
### 问题描述
先打开拍照然后转录制时,抛出错误信息
### 问题现象
prepare call failed. error code: 5400102。
### 版本信息
API 15
### 复现代码
```typescript
if (!this.cameraManager) {
console.error('CameraDemo cameraManager is undefined.')
return;
}
// 获取支持指定的相机设备对象
let cameraDevices: Array<camera.CameraDevice> = [];
try {
cameraDevices = this.cameraManager.getSupportedCameras();
} catch (error) {
let err = error as BusinessError;
console.error(`CameraDemo The getSupportedCameras call failed. error: ${JSON.stringify(err)}`)
}
cameraDevices.forEach((cameraDevice: camera.CameraDevice) => {
console.info(`CameraDemo cameraId: ${cameraDevice.cameraId}, cameraPosition: ${cameraDevice.cameraPosition.toString()}, cameraType: ${cameraDevice.cameraType.toString()}, connectionType: ${cameraDevice.connectionType.toString()}`)
})
// 创建相机输入流
if (this.cameraInput) {
await this.cameraInput.close();
}
try {
this.cameraInput = this.cameraManager.createCameraInput(this.camaraFlip ? cameraDevices[0] : cameraDevices[1]);
} catch (error) {
let err = error as BusinessError;
console.error(`CameraDemo createCaptureSession error. error: 1${JSON.stringify(err)}`);
return
}
// 监听cameraInput错误信息
this.cameraInput.on('error', this.camaraFlip ? cameraDevices[0] : cameraDevices[1], (error: BusinessError) => {
console.error(`CameraDemo Camera input error: ${JSON.stringify(error)}`);
});
// 打开相机
try {
await this.cameraInput.open();
} catch (error) {
let err = error as BusinessError;
console.error(`CameraDemo cameraInput open error. error: ${JSON.stringify(err)}`);
}
// 获取指定的相机设备对象支持的模式
let cameraSceneModes: Array<camera.SceneMode> = [];
try {
cameraSceneModes = this.cameraManager.getSupportedSceneModes(this.camaraFlip ? cameraDevices[0] : cameraDevices[1]);
cameraSceneModes.forEach((cameraSceneMode: camera.SceneMode) => {
console.info(`CameraDemo cameraSceneMode: ${cameraSceneMode.toString()}`)
})
} catch (error) {
let err = error as BusinessError;
console.error(`CameraDemo The getSupportedSceneModes call failed. error: ${JSON.stringify(err)}`)
}
// 获取相机设备支持的输出流能力
let cameraOutputCapability: camera.CameraOutputCapability | undefined
let arr = this.getPhotoSize(cameraDevices[0], cameraOutputCapability) // 后置
let arr1 = this.getPhotoSize(cameraDevices[1], cameraOutputCapability)
this.sizeCamera.push(arr, arr1)
cameraOutputCapability = this.cameraManager.getSupportedOutputCapability(this.camaraFlip ? cameraDevices[0] : cameraDevices[1], camera.SceneMode.NORMAL_PHOTO)
if (!cameraOutputCapability) {
console.error('CameraDemo cameraManager.getSupportedOutputCapability error');
return;
}
this.printCameraOutputCapability(cameraOutputCapability);
let previewProfile = cameraOutputCapability.previewProfiles[0];
cameraOutputCapability.previewProfiles.find((profile) => {
// 筛选预期宽高及NV21的格式对应的profile
if (profile.size.width == this.imageSize.width && profile.size.height == this.imageSize.height) {
previewProfile = profile;
return;
}
})
this.imageSize = previewProfile.size;
if (this.isVideo) {
this.initVideo(this.cameraManager, cameraDevices)
}
async initVideo(cameraManager: camera.CameraManager, cameraDevices: Array<camera.CameraDevice>) {
let cameraOutputCap: camera.CameraOutputCapability =
cameraManager.getSupportedOutputCapability(cameraDevices[0], camera.SceneMode.NORMAL_VIDEO);
if (!cameraOutputCap) {
console.error('cameraManager.getSupportedOutputCapability error');
return;
}
console.info('outputCapability: ' + JSON.stringify(cameraOutputCap));
let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles;
if (!previewProfilesArray) {
console.error('createOutput previewProfilesArray === null || undefined');
}
let photoProfilesArray: Array<camera.Profile> = cameraOutputCap.photoProfiles;
if (!photoProfilesArray) {
console.error('createOutput photoProfilesArray === null || undefined');
}
let videoProfilesArray: Array<camera.VideoProfile> = cameraOutputCap.videoProfiles;
if (!videoProfilesArray) {
console.error('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) {
console.error('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: 'fd://35',
rotation: 0,
location: {
latitude: 30,
longitude: 130
}
};
try {
this.avRecorder = await media.createAVRecorder();
} catch (error) {
let err = error as BusinessError;
console.error(`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;
console.error(`prepare call failed. error code: ${err.code}`);
}
try {
this.videoOutput = cameraManager.createVideoOutput(videoProfile, this.surfaceId);
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to create the videoOutput instance. error: ${JSON.stringify(err)}`);
}
if (this.videoOutput === undefined) {
return;
}
this.videoOutput.on('frameStart', () => {
console.info('Video frame started');
});
this.videoOutput.on('error', (error: BusinessError) => {
console.info(`Preview output error code: ${error.code}`);
});
// 创建录像会话
try {
this.VideoSession = cameraManager.createSession(camera.SceneMode.NORMAL_VIDEO) as camera.VideoSession;
} catch (error) {
let err = error as BusinessError;
console.error('Failed to create the VideoSession instance. errorCode = ' + err.code);
}
if (this.VideoSession === undefined) {
return;
}
try {
this.VideoSession.beginConfig();
} catch (error) {
let err = error as BusinessError;
console.error('Failed to beginConfig. errorCode = ' + err.code);
}
let cameraInput: camera.CameraInput | undefined = undefined;
try {
cameraInput = cameraManager.createCameraInput(this.camaraFlip ? cameraDevices[0] : cameraDevices[1]);
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to createCameraInput. error: ${JSON.stringify(err)}`);
}
if (cameraInput === undefined) {
return;
}
let cameraDevice: camera.CameraDevice = this.camaraFlip ? cameraDevices[0] : cameraDevices[1];
cameraInput.on('error', cameraDevice, (error: BusinessError) => {
console.info(`Camera input error code: ${error.code}`);
});
try {
await cameraInput.open();
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to open cameraInput. error: ${JSON.stringify(err)}`);
}
try {
this.VideoSession.addInput(cameraInput);
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to add cameraInput. error: ${JSON.stringify(err)}`);
}
let previewOutput: camera.PreviewOutput | undefined = undefined;
try {
previewOutput = cameraManager.createPreviewOutput(videoProfile, this.surfaceId);
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to create the PreviewOutput instance. error: ${JSON.stringify(err)}`);
}
if (previewOutput === undefined) {
return;
}
try {
this.VideoSession.addOutput(previewOutput);
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to add previewOutput. error: ${JSON.stringify(err)}`);
}
try {
await this.VideoSession.commitConfig();
} catch (error) {
let err = error as BusinessError;
console.error(`captureSession commitConfig error: ${JSON.stringify(err)}`);
}
try {
await this.VideoSession.start();
} catch (error) {
let err = error as BusinessError;
console.error(`captureSession start error: ${JSON.stringify(err)}`);
}
this.videoOutput.start((err: BusinessError) => {
if (err) {
console.error(`Failed to start the video output. error: ${JSON.stringify(err)}`);
return;
}
console.info('Callback invoked to indicate the video output start success.');
});
}
## 5400102 当前状态机不支持此操作
**错误信息**
Operation not allowed.
**错误描述**
当前操作不允许。
**可能原因**
当前状态机不支持此操作。
**处理步骤**
确认当前状态是否支持当前操作,把实例切换到正确的状态进行正确的操作。
[文档链接](https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V2/errorcode-media-0000001478341485-V2#ZH-CN_TOPIC_0000001523968938__5400102-)
报错代码 5400102
表示在HarmonyOS鸿蒙Next相机开发中,prepare
调用失败。可能的原因包括:
- 权限问题:确保应用已获取相机权限。
- 资源冲突:其他应用可能正在占用相机资源。
- 相机配置错误:检查相机参数配置是否正确。
- 系统兼容性:确保设备支持当前API版本。
建议检查日志以获取更多详细信息,并参考官方文档进行调试。