Android編程實(shí)現(xiàn)錄音及保存播放功能的方法【附demo源碼下載】
本文實(shí)例講述了Android編程實(shí)現(xiàn)錄音及保存播放功能的方法。分享給大家供大家參考,具體如下:
在android中進(jìn)行錄音相對(duì)來說是比較簡(jiǎn)單的,使用系統(tǒng)提供的MediaRecorder類進(jìn)行錄音并保存,然后調(diào)用MediaPlayer進(jìn)行播放。以下為xml配置文件代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.kk.soundrecording.MainActivity" > <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:text="@string/start" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/start" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:text="@string/stop" /> <Button android:id="@+id/paly" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/stop" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:text="@string/paly" /> <Button android:id="@+id/pause_paly" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/paly" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:text="@string/pause_paly" /> <Button android:id="@+id/stop_paly" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/pause_paly" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:text="@string/stop_paly" /> </RelativeLayout>
在MainActivity中進(jìn)行錄音,代碼如下:
package com.example.kk.soundrecording; import java.io.File; import java.io.IOException; import com.example.kk.util.RecordPlayer; import android.app.Activity; import android.media.MediaRecorder; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * * @author kk * */ public class MainActivity extends Activity implements OnClickListener { // 開始錄音 private Button start; // 停止按鈕 private Button stop; // 播放按鈕 private Button paly; // 暫停播放 private Button pause_paly; // 停止播放 private Button stop_paly; // 錄音類 private MediaRecorder mediaRecorder; // 以文件的形式保存 private File recordFile; private RecordPlayer player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recordFile = new File("/mnt/sdcard", "kk.amr"); initView(); Listener(); } private void initView() { start = (Button) findViewById(R.id.start); stop = (Button) findViewById(R.id.stop); paly = (Button) findViewById(R.id.paly); pause_paly = (Button) findViewById(R.id.pause_paly); stop_paly = (Button) findViewById(R.id.stop_paly); } private void Listener() { start.setOnClickListener(this); stop.setOnClickListener(this); paly.setOnClickListener(this); pause_paly.setOnClickListener(this); stop_paly.setOnClickListener(this); } @Override public void onClick(View v) { player = new RecordPlayer(MainActivity.this); int Id = v.getId(); switch (Id) { case R.id.start: startRecording(); break; case R.id.stop: stopRecording(); break; case R.id.paly: playRecording(); break; case R.id.pause_paly: pauseplayer(); break; case R.id.stop_paly: stopplayer(); break; } } private void startRecording() { mediaRecorder = new MediaRecorder(); // 判斷,若當(dāng)前文件已存在,則刪除 if (recordFile.exists()) { recordFile.delete(); } mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mediaRecorder.setOutputFile(recordFile.getAbsolutePath()); try { // 準(zhǔn)備好開始錄音 mediaRecorder.prepare(); mediaRecorder.start(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void stopRecording() { if (recordFile != null) { mediaRecorder.stop(); mediaRecorder.release(); } } private void playRecording() { player.playRecordFile(recordFile); } private void pauseplayer() { player.pausePalyer(); } private void stopplayer() { // TODO Auto-generated method stub player.stopPalyer(); } }
同時(shí),新建一個(gè)RecordPlayer類,用來播放保存好的錄音,如下:
package com.example.kk.util; import java.io.File; import android.content.Context; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.net.Uri; import android.util.Log; import android.widget.Toast; import com.example.kk.soundrecording.R; /** * * * @author kk 錄音播放類 * */ public class RecordPlayer { private static MediaPlayer mediaPlayer; private Context mcontext; public RecordPlayer(Context context) { this.mcontext = context; } // 播放錄音文件 public void playRecordFile(File file) { if (file.exists() && file != null) { if (mediaPlayer == null) { Uri uri = Uri.fromFile(file); mediaPlayer = MediaPlayer.create(mcontext, uri); } mediaPlayer.start(); //監(jiān)聽MediaPlayer播放完成 mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer paramMediaPlayer) { // TODO Auto-generated method stub //彈窗提示 Toast.makeText(mcontext, mcontext.getResources().getString(R.string.ok), Toast.LENGTH_SHORT).show(); } }); } } // 暫停播放錄音 public void pausePalyer() { if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); Log.e("TAG", "暫停播放"); } } // 停止播放錄音 public void stopPalyer() { // 這里不調(diào)用stop(),調(diào)用seekto(0),把播放進(jìn)度還原到最開始 if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); mediaPlayer.seekTo(0); Log.e("TAG", "停止播放"); } } }
此時(shí),功能代碼都已實(shí)現(xiàn),但是運(yùn)行時(shí)會(huì)報(bào)錯(cuò)!為什么呢,這個(gè)是被很多初學(xué)者會(huì)忘記的,那就是android開發(fā)中調(diào)用相應(yīng)的功能時(shí),必須在主配置文件中授予相應(yīng)的權(quán)限,在配置文件中添加如下代碼:
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
另外,Android權(quán)限控制的詳細(xì)說明可以參考Android Manifest功能與權(quán)限描述大全
附:Demo源碼點(diǎn)擊此處本站下載。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android一個(gè)類實(shí)現(xiàn)錄音與播放實(shí)例
- 詳解Android開發(fā)錄音和播放音頻的步驟(動(dòng)態(tài)獲取權(quán)限)
- Android實(shí)現(xiàn)語音播放與錄音功能
- android語音即時(shí)通訊之錄音、播放功能實(shí)現(xiàn)代碼
- Android 錄音與播放功能的簡(jiǎn)單實(shí)例
- Android使用MediaRecorder實(shí)現(xiàn)錄音及播放
- Android錄音播放管理工具
- Android實(shí)現(xiàn)自制和播放錄音程序
- Android編程開發(fā)錄音和播放錄音簡(jiǎn)單示例
- Android實(shí)現(xiàn)音頻錄音與播放
相關(guān)文章
基于android實(shí)現(xiàn)五子棋開發(fā)
這篇文章主要為大家詳細(xì)介紹了基于android實(shí)現(xiàn)五子棋開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02- 這篇文章主要為大家詳細(xì)介紹了Android中Handler機(jī)制的使用,文中的示例代碼講解詳細(xì),有需要的朋友可以借鑒參考下,希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2022-11-11
Android 根據(jù)手勢(shì)頂部View自動(dòng)展示與隱藏效果
這篇文章主要介紹了Android 根據(jù)手勢(shì)頂部View自動(dòng)展示與隱藏效果,本文給大家介紹非常詳細(xì)包括實(shí)現(xiàn)原理和實(shí)例代碼,需要的朋友參考下吧2017-08-08Flutter構(gòu)建自定義Widgets的全過程記錄
在Flutter實(shí)際開發(fā)中,大家可能會(huì)遇到flutter框架中提供的widget達(dá)不到我們想要的效果,這時(shí)就需要我們?nèi)プ远xwidget,下面這篇文章主要給大家介紹了關(guān)于Flutter構(gòu)建自定義Widgets的相關(guān)資料,需要的朋友可以參考下2022-01-01Android IPC機(jī)制綁定Service實(shí)現(xiàn)本地通信
本文主要介紹Android IPC機(jī)制綁定Service 實(shí)現(xiàn)本地通信,通過圖解,代碼等方式給大家解釋Android IPC機(jī)制,需要參考的同學(xué)可以看一下2016-07-07Android開發(fā)筆記之:用Enum(枚舉類型)取代整數(shù)集的應(yīng)用詳解
本篇文章是對(duì)Android中用Enum(枚舉類型)取代整數(shù)集的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android 實(shí)現(xiàn)銀聯(lián)刷卡機(jī)消費(fèi)后手動(dòng)簽名的功能(示例代碼)
在一些商場(chǎng)購(gòu)物時(shí),不需要用筆在銀聯(lián)機(jī)上簽名了,直接用手指觸摸實(shí)現(xiàn)消費(fèi)簽名,非常方便,下面小編給大家分享Android 實(shí)現(xiàn)銀聯(lián)刷卡機(jī)消費(fèi)后手動(dòng)簽名的功能,需要的朋友參考下吧2017-12-12Android列表實(shí)現(xiàn)單選點(diǎn)擊縮放動(dòng)畫效果
在android開發(fā),我們會(huì)常常使用到縮放動(dòng)畫,這篇文章主要給大家介紹了關(guān)于Android列表實(shí)現(xiàn)單選點(diǎn)擊縮放動(dòng)畫效果的相關(guān)資料,需要的朋友可以參考下2021-08-08最新評(píng)論