本篇文章為大家展示了怎么在Android應(yīng)用中實(shí)現(xiàn)一個(gè)語(yǔ)音消息發(fā)送功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)專注于企業(yè)成都營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、海港網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場(chǎng)景定制、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為海港等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。直接上代碼:
//語(yǔ)音操作對(duì)象 private MediaPlayer mPlayer = null; private MediaRecorder mRecorder = null; //語(yǔ)音文件保存路徑 private String FileName = null; FileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/soundrecord/amrsend.amr";
定義一個(gè)ImageView,然后給它設(shè)置監(jiān)聽(tīng)事件:
@Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: beginX = motionEvent.getX(); beginY = motionEvent.getY(); Log.e("MainActivity", "beginX:" + beginX); Log.e("MainActivity", "beginY:" + beginY); Log.e("MainActivity", "ACTION_DOWN"); mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setOutputFile(FileName); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mRecorder.prepare(); } catch (IOException e) { Log.e("MainActivity", "prepare() failed" + e); } mRecorder.start(); break; case MotionEvent.ACTION_UP: finalX = motionEvent.getX(); finalY = motionEvent.getY(); Log.e("MainActivity", "finalX:" + finalX); Log.e("MainActivity", "finalY:" + finalY); Log.e("MainActivity", "ACTION_UP"); mRecorder.stop(); mRecorder.release(); mRecorder = null; if ((beginY - finalY) > 50) { Log.e("MainActivity", "執(zhí)行刪除"); new File(FileName).delete(); Log.e("MainActivity", "執(zhí)行刪除后文件是否還存在:" + new File(FileName).exists()); } break; default: break; } return true; }
然后在清單文件中加上需要的權(quán)限就行了:
上述內(nèi)容就是怎么在Android應(yīng)用中實(shí)現(xiàn)一個(gè)語(yǔ)音消息發(fā)送功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。