Android編程實現(xiàn)錄音及保存播放功能的方法【附demo源碼下載】
本文實例講述了Android編程實現(xiàn)錄音及保存播放功能的方法。分享給大家供大家參考,具體如下:
在android中進行錄音相對來說是比較簡單的,使用系統(tǒng)提供的MediaRecorder類進行錄音并保存,然后調(diào)用MediaPlayer進行播放。以下為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中進行錄音,代碼如下:
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();
// 判斷,若當前文件已存在,則刪除
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 {
// 準備好開始錄音
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();
}
}
同時,新建一個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),把播放進度還原到最開始
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
mediaPlayer.seekTo(0);
Log.e("TAG", "停止播放");
}
}
}
此時,功能代碼都已實現(xiàn),但是運行時會報錯!為什么呢,這個是被很多初學者會忘記的,那就是android開發(fā)中調(diào)用相應(yīng)的功能時,必須在主配置文件中授予相應(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)限控制的詳細說明可以參考Android Manifest功能與權(quán)限描述大全
附:Demo源碼點擊此處本站下載。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
這篇文章主要為大家詳細介紹了Android中Handler機制的使用,文中的示例代碼講解詳細,有需要的朋友可以借鑒參考下,希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2022-11-11
Android 根據(jù)手勢頂部View自動展示與隱藏效果
這篇文章主要介紹了Android 根據(jù)手勢頂部View自動展示與隱藏效果,本文給大家介紹非常詳細包括實現(xiàn)原理和實例代碼,需要的朋友參考下吧2017-08-08
Flutter構(gòu)建自定義Widgets的全過程記錄
在Flutter實際開發(fā)中,大家可能會遇到flutter框架中提供的widget達不到我們想要的效果,這時就需要我們?nèi)プ远xwidget,下面這篇文章主要給大家介紹了關(guān)于Flutter構(gòu)建自定義Widgets的相關(guān)資料,需要的朋友可以參考下2022-01-01
Android IPC機制綁定Service實現(xiàn)本地通信
本文主要介紹Android IPC機制綁定Service 實現(xiàn)本地通信,通過圖解,代碼等方式給大家解釋Android IPC機制,需要參考的同學可以看一下2016-07-07
Android開發(fā)筆記之:用Enum(枚舉類型)取代整數(shù)集的應(yīng)用詳解
本篇文章是對Android中用Enum(枚舉類型)取代整數(shù)集的應(yīng)用進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Android 實現(xiàn)銀聯(lián)刷卡機消費后手動簽名的功能(示例代碼)
在一些商場購物時,不需要用筆在銀聯(lián)機上簽名了,直接用手指觸摸實現(xiàn)消費簽名,非常方便,下面小編給大家分享Android 實現(xiàn)銀聯(lián)刷卡機消費后手動簽名的功能,需要的朋友參考下吧2017-12-12最新評論

