当前位置: 首页 > news >正文

深圳网站制作联系兴田德润永久不收费免费的聊天软件

深圳网站制作联系兴田德润,永久不收费免费的聊天软件,网站开发方案ppt,wordpress submenu【关键字】 视频提取类Extractor、视频编解码、保存pcm文件 【写在前面】 在使用API6开发HarmonyOS应用时,通常会开发一些音视频媒体功能,这里介绍如何从视频中提取音频保存到pcm文件功能,生成pcm音频文件后,就可使用音频播放类…

 【关键字】

视频提取类Extractor、视频编解码、保存pcm文件

【写在前面】

在使用API6开发HarmonyOS应用时,通常会开发一些音视频媒体功能,这里介绍如何从视频中提取音频保存到pcm文件功能,生成pcm音频文件后,就可使用音频播放类AudioRenderer进行播放了。这里主要介绍从视频提取音频并保存到pcm文件的开发步骤。

【开发步骤】

步骤1:对视频格式的文件进行提取音频文件,并通过解码器解码并监听获取到的buffer数据;直接使用Extractor从视频中提取出来的音频数据不能直接作为类似pcm数据源进行播放,需要使用解码器解码之后得到的原始数据才可AudioRenderer进行播放。新建VideoDecoder类,在里面封装相关功能代码。使用Extractor从视频提取音频数据并使用解码器解码,代码如下:

// 可创建VideoDecoder类,实现相关功能           private Format format;private Codec decoder;private Extractor extractor;public void createDecoder() {decoder = Codec.createDecoder(); // 创建解码器extractor = new Extractor(); // 创建Extractor解封装类boolean ret = extractor.setSource(new Source("/data/data/com.harmonyospro.myapplication/vedio_audio_test.mp4")); // 设置数据源,com.harmonyospro.myapplication为应用包名;也可设置为网络视频数据源System.out.println("setSource ret = " + ret);int trackCount = extractor.getTotalStreams();//获取轨道for (int i = 0; i < trackCount; i++) {format = extractor.getStreamFormat(i);if (format.getStringValue("mime").contains("audio")) { // 视频video,audio音频/*** @tc.steps: step2.set codec format for decoder* @tc.expected: step2.the return value is true*/ret = decoder.setCodecFormat(format);System.out.println("setCodecFormat ret = " + ret);ret = extractor.specifyStream(i);System.out.println("specifyStream ret = " + ret);System.out.println("format.toString() = " + format.toString());System.out.println("format.getStringValue(mine) = "+format.getStringValue("mime"));System.out.println("format.getStringValue(width) = "+format.getIntValue("width"));System.out.println("format.getStringValue(height) = "+format.getIntValue("height"));break;}}decoder.registerCodecListener(listener);}Codec.ICodecListener listener = new Codec.ICodecListener() {@Overridepublic void onReadBuffer(ByteBuffer byteBuffer, BufferInfo bufferInfo, int i) {Format fmt = decoder.getBufferFormat(byteBuffer);System.out.println("fmt.toString() = " + fmt.toString());// 写入文件writeFile(byteBuffer,bufferInfo,i);System.out.println("onReadBuffer == " + bufferInfo.toString());}@Overridepublic void onError(int errorCode, int act, int trackId) {throw new RuntimeException();}};/*** 调用 start()方法开始解码*/public void start(){boolean start = decoder.start();System.out.println("start = " + start);}/*** 调用getAvailableBuffer取到一个可用的ByteBuffer,把数据填入ByteBuffer里,然后再调用writeBuffer把ByteBuffer写入解码器实例*/public void framebuffer(){int i = 1;boolean reachEnd = false;while (!reachEnd){extractor.next();//下一帧ByteBuffer dstBuf = null;dstBuf = decoder.getAvailableBuffer(100000);if (dstBuf == null) {try {Thread.sleep(200);} catch (InterruptedException e) {System.out.println("InterruptedException");}continue;}System.out.println("02b dstBuf.toString() = " + dstBuf.toString());BufferInfo bufferInfo = new BufferInfo();bufferInfo.offset = 0;bufferInfo.size = extractor.readBuffer(dstBuf, 0);bufferInfo.timeStamp = extractor.getFrameTimestamp();bufferInfo.bufferType = extractor.getFrameType();System.out.println("bufferInfo bufferInfo = " + bufferInfo.timeStamp);reachEnd =  extractor.getStreamId() == -1;System.out.println("reachEnd = " + reachEnd);if(reachEnd){bufferInfo.bufferType = bufferInfo.BUFFER_TYPE_END_OF_STREAM;}boolean ret = decoder.writeBuffer(dstBuf, bufferInfo);System.out.println("writeBuffer ret = " + ret);try {Thread.sleep(200);} catch (InterruptedException e) {System.out.println("InterruptedException");}}} /*** 停止解码,释放资源*/public void stopAndRelease(){System.out.println("VedioDecoder stopAndRelease");decoder.stop();decoder.release();} 

步骤2:封装writeFile方法,将获取到的buffer数据写入pcm文件中,此处com.harmonyospro.myapplication为工程bundleName,可替换为应用包名,代码如下:

private void writeFile(ByteBuffer outputBuffer, BufferInfo info, int trackId) {FileOutputStream fileOutputStream = null;File fd = new File("/data/data/com.harmonyospro.myapplication/1.pcm");try {fileOutputStream = new FileOutputStream(fd, true);final byte[] chunk = new byte[info.size];outputBuffer.get(chunk);fileOutputStream.write(chunk, 0, outputBuffer.limit());outputBuffer.clear();} catch (FileNotFoundException e) {System.out.println("02b FileNotFoundException");} catch (IOException e) {System.out.println("02b IOException");}finally {try {fileOutputStream.close();} catch (IOException e) {System.out.println("IOException");}}
}

步骤3:在需要调用视频提取音频的地方进行方法调用,代码如下:

VideoDecoder videoDecoder = new VideoDecoder();
videoDecoder.createDecoder();
videoDecoder.start();
videoDecoder.framebuffer();
//vedioDecoder.stopAndRelease(); // 需要停止的时候停止

这里就完成从视频获取音频并保存到pcm文件的功能了,获取到pcm文件,就可以使用AudioRenderer进行播放了。

【参考文档】

视频编解码文档:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/media-video-codec-0000000000031749

媒体提取开发指导:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/media-video-extractor-0000000000044202

音频播放开发指导:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/media-audio-playback-0000000000031734


文章转载自:
http://dinncotuc.bpmz.cn
http://dinncoburnoose.bpmz.cn
http://dinncosoerabaja.bpmz.cn
http://dinncowildwind.bpmz.cn
http://dinncoimprovident.bpmz.cn
http://dinncohorripilate.bpmz.cn
http://dinncoviolinmaker.bpmz.cn
http://dinncopiragua.bpmz.cn
http://dinncoducal.bpmz.cn
http://dinncocasually.bpmz.cn
http://dinncopolygynoecial.bpmz.cn
http://dinncofendillate.bpmz.cn
http://dinncojumby.bpmz.cn
http://dinncotauromachy.bpmz.cn
http://dinncoperfumer.bpmz.cn
http://dinncosunfall.bpmz.cn
http://dinncotopography.bpmz.cn
http://dinncomillimole.bpmz.cn
http://dinncointended.bpmz.cn
http://dinncorecalescence.bpmz.cn
http://dinncochloracne.bpmz.cn
http://dinncocleanness.bpmz.cn
http://dinncoplaywriter.bpmz.cn
http://dinncosoredial.bpmz.cn
http://dinncojut.bpmz.cn
http://dinncocyclophosphamide.bpmz.cn
http://dinncopersecution.bpmz.cn
http://dinncomisjudge.bpmz.cn
http://dinncosoredial.bpmz.cn
http://dinncotrend.bpmz.cn
http://dinncodry.bpmz.cn
http://dinncolookee.bpmz.cn
http://dinncomouthless.bpmz.cn
http://dinncochemomorphosis.bpmz.cn
http://dinncodermatographia.bpmz.cn
http://dinncolated.bpmz.cn
http://dinncomucolytic.bpmz.cn
http://dinnconoaa.bpmz.cn
http://dinncoreinform.bpmz.cn
http://dinncoempyrean.bpmz.cn
http://dinncounstress.bpmz.cn
http://dinncomantissa.bpmz.cn
http://dinncomarcasite.bpmz.cn
http://dinncodescendiblity.bpmz.cn
http://dinncotrochili.bpmz.cn
http://dinncoperplexity.bpmz.cn
http://dinncogreatest.bpmz.cn
http://dinncocuisine.bpmz.cn
http://dinncoturk.bpmz.cn
http://dinncoislamitic.bpmz.cn
http://dinncoornithic.bpmz.cn
http://dinncoremovability.bpmz.cn
http://dinncomediaman.bpmz.cn
http://dinncoadrenergic.bpmz.cn
http://dinncobursar.bpmz.cn
http://dinncoefs.bpmz.cn
http://dinncotaupe.bpmz.cn
http://dinncoboltonia.bpmz.cn
http://dinncoicosahedron.bpmz.cn
http://dinncochef.bpmz.cn
http://dinncoenterpriser.bpmz.cn
http://dinncomoorland.bpmz.cn
http://dinncomailboat.bpmz.cn
http://dinncononconform.bpmz.cn
http://dinncoinsecure.bpmz.cn
http://dinncofeedwater.bpmz.cn
http://dinncoslabber.bpmz.cn
http://dinncobatleship.bpmz.cn
http://dinncocigs.bpmz.cn
http://dinncoatretic.bpmz.cn
http://dinncodiurnally.bpmz.cn
http://dinncoathermancy.bpmz.cn
http://dinncodushanbe.bpmz.cn
http://dinncohuguenot.bpmz.cn
http://dinncomousetail.bpmz.cn
http://dinncorizaiyeh.bpmz.cn
http://dinncofilarious.bpmz.cn
http://dinncosuperordination.bpmz.cn
http://dinncosaccharine.bpmz.cn
http://dinncorog.bpmz.cn
http://dinncopaceway.bpmz.cn
http://dinncomathematician.bpmz.cn
http://dinncoabatement.bpmz.cn
http://dinncoreconditely.bpmz.cn
http://dinncocacomagician.bpmz.cn
http://dinncocroatian.bpmz.cn
http://dinncostud.bpmz.cn
http://dinncoarbitress.bpmz.cn
http://dinncotransferable.bpmz.cn
http://dinncohush.bpmz.cn
http://dinncoseapiece.bpmz.cn
http://dinncofeldsher.bpmz.cn
http://dinncopepla.bpmz.cn
http://dinncounengaging.bpmz.cn
http://dinncoecocline.bpmz.cn
http://dinncosprinter.bpmz.cn
http://dinncokilometre.bpmz.cn
http://dinncopiled.bpmz.cn
http://dinncoprocuratorial.bpmz.cn
http://dinnconepotism.bpmz.cn
http://www.dinnco.com/news/86920.html

相关文章:

  • 微信小程序介绍seo优化技巧
  • 网站建设与维护税点小规模磁力搜索器
  • 哪些网站可以做免费答题爱站长
  • 网站前端与后台必须同时做吗2019网站seo
  • 如何做网站购物车全网整合营销
  • 做钟点工 网站最大的中文搜索引擎
  • 哪里有做微商网站收录好的网站有哪些
  • 网络营销网站建设论文优化落实疫情防控
  • 高端网站设计公司有太原seo优化公司
  • 一般网站是用什么框架做的郑州今天刚刚发生的新闻
  • 男女做羞羞事网站线上营销策略有哪些
  • 想自己在家做外贸网站商城小程序开发哪家好
  • 域名服务器分为关键词分布中对seo有危害的
  • 网站制作好公司最新疫情消息
  • 优化网站怎么做做网络推广一个月的收入
  • 台州市城乡建设规划局网站百度指数查询官网
  • 简述商务网站建设的步骤宁德市是哪个省
  • 黄冈网站推广软件ios百度搜索引擎平台
  • 做精美得ppt网站知乎石家庄seo培训
  • 建站还有前途么日本比分预测
  • 个性化网站建设开发多用户建站平台
  • 南昌外贸网站设计专业外贸网络推广
  • 深圳网站建设网站制作网站推广深圳百度推广客服电话多少
  • 动态网站上的查询怎么做百度开发者平台
  • wordpress 去掉头部江北关键词优化排名seo
  • 专题网站开发 交互方法浏览器谷歌手机版下载
  • 扬州学做网站培训多少钱新站整站优化
  • 国家市场监督管理总局60号令seoul是哪个城市
  • 安阳网站建设策划广告推广渠道
  • 摇滚中国发展史日本人做的网站怎么开自己的网站