HarmonyOS 鸿蒙Next应用内录制视频无法在相册及文件夹内播放
HarmonyOS 鸿蒙Next应用内录制视频无法在相册及文件夹内播放
private videoProfile: media.AVRecorderProfile = {
fileFormat: media.ContainerFormatType.CFT_MPEG_4,
videoBitrate: 200000,
videoCodec: media.CodecMimeType.VIDEO_MPEG4,
videoFrameWidth: 1920,
videoFrameHeight: 1080,
videoFrameRate: 30,
audioBitrate: <span class="hljs-number">48000</span>,
audioChannels: <span class="hljs-number">1</span>,
audioCodec: media.CodecMimeType.AUDIO_AAC,
audioSampleRate: <span class="hljs-number">48000</span>,
}
let currConfig: media.AVRecorderConfig = {
audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC,
videoSourceType: media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV,
profile: this.videoProfile,
url: fd:<span class="hljs-comment">//${this.Mp4fd}
,
rotation: 90,
location: {
latitude: 30, longitude: 130
}
}
其中,videoCodec和videoSourceType都尝试过各个枚举值。在录制视频结束保存到相册后,在相册播放会提示:当前格式暂不支持播放,请问怎么解决这个问题
根据prepare报错5400106 当前使用的格式规格不支持,建议检查下video的url,尝试按这种方式修改下再试试:
private async createVideoOutput() {
let {width, height} = this.selVideoProfile.size
let audioRoutingManager = audio.getAudioManager().getRoutingManager()
let deviceDesArray = audioRoutingManager.getDevicesSync(audio.DeviceFlag.INPUT_DEVICES_FLAG)
let micDeviceDes = deviceDesArray.find((value: audio.AudioDeviceDescriptor) => {
<span class="hljs-keyword">let</span> dt = value.deviceType
<span class="hljs-keyword">return</span> dt == audio.DeviceType.MIC || dt == audio.DeviceType.DEFAULT
}) || {sampleRates: [audio.AudioSamplingRate.SAMPLE_RATE_12000], channelCounts: [audio.AudioChannel.CHANNEL_2]}
// AVRecorderProfile
// let aVRecorderProfile: media.AVRecorderProfile = {
// audioBitrate: 100000,
// audioChannels: 2,
// audioCodec: media.CodecMimeType.AUDIO_AAC,
// audioSampleRate: 48000,
// fileFormat: media.ContainerFormatType.CFT_MPEG_4, // 视频文件封装格式,只支持MP4 (这个有误)
// videoBitrate: 100000, // 视频比特率
// videoCodec: media.CodecMimeType.VIDEO_MPEG4, // 视频文件编码格式,支持mpeg4和avc两种格式
// videoFrameWidth: 640, // 视频分辨率的宽
// videoFrameHeight: 480, // 视频分辨率的高
// videoFrameRate: 30 // 视频帧率
// };
// 这套vac在mate40pro可以支持
// let aVRecorderProfile: media.AVRecorderProfile = {
// audioBitrate: 48000,
// audioChannels: 2,
// audioCodec: media.CodecMimeType.AUDIO_AAC,
// audioSampleRate: 48000,
// fileFormat: media.ContainerFormatType.CFT_MPEG_4, // 视频文件封装格式,只支持MP4 (这个有误)
// videoBitrate: 2000000, // 视频比特率
// videoCodec: media.CodecMimeType.VIDEO_AVC, // 视频文件编码格式,支持mpeg4和avc两种格式
// videoFrameWidth: 640, // 视频分辨率的宽
// videoFrameHeight: 480, // 视频分辨率的高
// videoFrameRate: 30 // 视频帧率
// };
//
let aVRecorderProfile: media.AVRecorderProfile = {
audioBitrate: <span class="hljs-number">48000</span>,
audioChannels: <span class="hljs-number">1</span>,
audioCodec: media.CodecMimeType.AUDIO_AAC,
audioSampleRate: <span class="hljs-number">48000</span>,
fileFormat: media.ContainerFormatType.CFT_MPEG_4, <span class="hljs-comment">//</span>
videoBitrate:<span class="hljs-number">30000000</span>, <span class="hljs-comment">// 视频比特率</span>
videoCodec: media.CodecMimeType.VIDEO_AVC, <span class="hljs-comment">// 视频文件编码格式,支持mpeg4和avc两种格式</span>
videoFrameWidth: <span class="hljs-number">1920</span>, <span class="hljs-comment">// 视频分辨率的宽</span>
videoFrameHeight: <span class="hljs-number">1080</span>, <span class="hljs-comment">// 视频分辨率的高</span>
videoFrameRate: <span class="hljs-number">30</span>, <span class="hljs-comment">// 视频帧率</span>
};
console.info("++++++++++++++++++++++++++++++++++")
// console.info(’’ + width)
// console.info(’’ + height)
console.info(’’ + this.selVideoProfile.frameRateRange.min)
let filePath = this.context.filesDir + ‘/’ + await systemDateTime.getCurrentTime() + ‘.mp4’
let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE)
this.videoFile = file
let fd = file.fd
console.info(‘fd=’ + fd + ‘; filePath=’ + filePath + ‘; file=’ + file.path)
let {openFront} = this.initConfig
// 创建视频录制的参数,预览流与录像输出流的分辨率的宽(videoFrameWidth)高(videoFrameHeight)比要保持一致
let aVRecorderConfig: media.AVRecorderConfig = {
audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC,
videoSourceType: media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV,
profile: aVRecorderProfile,
url: <span class="hljs-string">'fd://'</span> + file.fd,
rotation:<span class="hljs-number">0</span>, <span class="hljs-comment">// 90°为默认竖屏显示角度,如果由于设备原因或应用期望以其他方式显示等原因,请根据实际情况调整该参数</span>
location: { latitude: <span class="hljs-number">30</span>, longitude: <span class="hljs-number">130</span> }
};
let avRecorder: media.AVRecorder | undefined = this.avRecorder;
if(!avRecorder){
<span class="hljs-comment">// 创建avRecorder</span>
<span class="hljs-keyword">try</span> {
avRecorder = await media.createAVRecorder();
} <span class="hljs-keyword">catch</span> (error) {
<span class="hljs-keyword">let</span> err = error as BusinessError;
console.error(`createAVRecorder call failed. error code: ${err.code}`);
}
<span class="hljs-keyword">if</span> (avRecorder === <span class="hljs-literal">undefined</span>) {
console.error(`createAVRecorder call failed. avRecorder == <span class="hljs-literal">undefined</span>`);
<span class="hljs-keyword">return</span> <span class="hljs-literal">undefined</span>;
}
console.info(`createAVRecorder call succ`);
<span class="hljs-comment">// 状态机变化回调函数</span>
avRecorder.on(<span class="hljs-string">'stateChange'</span>, (state: media.AVRecorderState, reason: media.StateChangeReason) => {
console.info(<span class="hljs-string">'avRecorder current state is: '</span> + state);
})
<span class="hljs-comment">// 错误上报回调函数</span>
avRecorder.on(<span class="hljs-string">'error'</span>, (err: BusinessError) => {
console.error(<span class="hljs-string">'error ocConstantSourceNode, error message is '</span> + err);
})
}else{
await avRecorder.reset()
}
// 设置视频录制的参数
try{
await avRecorder.prepare(aVRecorderConfig);
} catch (err) {
console.error(<span class="hljs-string">'Failed to avRecorder prepare. errorCode = '</span> + err);
}
this.avRecorder = avRecorder
let videoSurfaceId = await avRecorder.getInputSurface()
// 创建VideoOutput对象
let videoOutput: camera.VideoOutput | undefined = undefined;
try {
videoOutput = <span class="hljs-keyword">this</span>.cameraManager.createVideoOutput(<span class="hljs-keyword">this</span>.selVideoProfile, videoSurfaceId);
} catch (error) {
<span class="hljs-keyword">let</span> err = error as BusinessError;
console.error(<span class="hljs-string">'Failed to create the videoOutput instance. errorCode = '</span> + err.code);
}
this.videoOutput = videoOutput
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
针对HarmonyOS 鸿蒙Next应用内录制视频无法在相册及文件夹内播放的问题,以下是一些可能的解决方案:
- 检查视频格式:确保录制的视频格式是HarmonyOS设备支持的格式,如MP4等。
- 文件完整性:验证视频文件是否完整且未损坏,可以通过在其他设备上尝试播放该视频来验证。
- 存储路径:检查视频文件是否被正确保存到设备的相册或指定文件夹中,确保路径正确且文件可读。
- 系统更新:检查HarmonyOS系统是否有更新,有时系统更新能解决视频播放相关的兼容性问题。
- 应用权限:确保应用具有读写存储空间的权限,以便能够访问和播放录制的视频。
- 第三方播放器:尝试使用第三方播放器打开视频,以排除设备自带播放器的问题。
如果以上方法都无法解决问题,可能是视频录制或保存过程中存在特定的技术障碍。建议直接联系官网客服,以获得更专业的技术支持。官网地址是:https://www.itying.com/category-93-b0.html。