Android一個(gè)類實(shí)現(xiàn)錄音與播放實(shí)例
前言
最近混合開發(fā)的項(xiàng)目 在做語音識別時(shí) h5拿不到麥克風(fēng)的權(quán)限
幾經(jīng)周折 開發(fā)任務(wù)又落到了原生開發(fā)這里
先寫一個(gè)demo實(shí)現(xiàn)錄音和播放功能 然后由web端同事調(diào)用交互方法
實(shí)現(xiàn)效果

代碼實(shí)現(xiàn)
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = "MainActivity";
//語音文件保存路徑
private String FileName = null;
//界面控件
private Button startRecord;
private Button startPlay;
private Button stopRecord;
private Button stopPlay;
//語音操作對象
private MediaPlayer mPlayer = null;
private MediaRecorder mRecorder = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();//請求麥克風(fēng)權(quán)限
//開始錄音
startRecord = findViewById(R.id.startRecord);
//綁定監(jiān)聽器
startRecord.setOnClickListener(new startRecordListener());
//結(jié)束錄音
stopRecord = findViewById(R.id.stopRecord);
stopRecord.setOnClickListener(new stopRecordListener());
//開始播放
startPlay = findViewById(R.id.startPlay);
//綁定監(jiān)聽器
startPlay.setOnClickListener(new startPlayListener());
//結(jié)束播放
stopPlay = findViewById(R.id.stopPlay);
stopPlay.setOnClickListener(new stopPlayListener());
//設(shè)置sdcard的路徑
FileName = Environment.getExternalStorageDirectory().getAbsolutePath();
FileName += "/test.mp3";
}
private void requestPermission() {
PermissionGen.with(this)
.addRequestCode(100)
.permissions(Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.WAKE_LOCK)
.request();
}
//開始錄音
class startRecordListener implements View.OnClickListener {
@Override
public void onClick(View v) {
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();
mRecorder.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed ---" + e.getMessage());
}
}
}
//停止錄音
class stopRecordListener implements View.OnClickListener {
@Override
public void onClick(View v) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
//播放錄音
class startPlayListener implements View.OnClickListener {
@Override
public void onClick(View v) {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(FileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "播放失敗");
}
}
}
//停止播放錄音
class stopPlayListener implements View.OnClickListener {
@Override
public void onClick(View v) {
mPlayer.release();
mPlayer = null;
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/startRecord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:text="開始錄音"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/stopRecord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="結(jié)束錄音"
app:layout_constraintBottom_toTopOf="@id/startPlay"
app:layout_constraintTop_toBottomOf="@id/startRecord" />
<Button
android:id="@+id/startPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開始播放"
app:layout_constraintBottom_toTopOf="@id/stopPlay"
app:layout_constraintTop_toBottomOf="@id/stopRecord" />
<Button
android:id="@+id/stopPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="結(jié)束播放"
app:layout_constraintTop_toBottomOf="@id/startPlay" />
</androidx.constraintlayout.widget.ConstraintLayout>
靜態(tài)權(quán)限
<!--錄音和寫磁盤權(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.READ_EXTERNAL_STORAGE" />
動態(tài)權(quán)限
PermissionGen.with(this)
.addRequestCode(100)
.permissions(Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.WAKE_LOCK)
.request();
總結(jié)
到此這篇關(guān)于Android一個(gè)類實(shí)現(xiàn)錄音與播放實(shí)例的文章就介紹到這了,更多相關(guān)Android實(shí)現(xiàn)錄音與播放內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解Android開發(fā)錄音和播放音頻的步驟(動態(tài)獲取權(quán)限)
- Android實(shí)現(xiàn)語音播放與錄音功能
- Android編程實(shí)現(xiàn)錄音及保存播放功能的方法【附demo源碼下載】
- android語音即時(shí)通訊之錄音、播放功能實(shí)現(xiàn)代碼
- Android 錄音與播放功能的簡單實(shí)例
- Android使用MediaRecorder實(shí)現(xiàn)錄音及播放
- Android錄音播放管理工具
- Android實(shí)現(xiàn)自制和播放錄音程序
- Android編程開發(fā)錄音和播放錄音簡單示例
- Android實(shí)現(xiàn)音頻錄音與播放
相關(guān)文章
Android短信備份及數(shù)據(jù)插入實(shí)現(xiàn)代碼解析
這篇文章主要介紹了Android短信備份及數(shù)據(jù)插入實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Android使用GridView實(shí)現(xiàn)橫向滾動效果
這篇文章主要為大家詳細(xì)介紹了Android使用GridView實(shí)現(xiàn)橫向滾動效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Flutter之自定義Dialog實(shí)現(xiàn)版本更新彈窗功能的實(shí)現(xiàn)
這篇文章主要介紹了Flutter之自定義Dialog實(shí)現(xiàn)版本更新彈窗功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Android實(shí)現(xiàn)3D翻轉(zhuǎn)動畫效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)3D翻轉(zhuǎn)動畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android ActionBarActivity設(shè)置全屏無標(biāo)題實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android ActionBarActivity設(shè)置全屏無標(biāo)題實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android 7.0以上版本實(shí)現(xiàn)應(yīng)用內(nèi)語言切換的方法
本篇文章主要介紹了Android 7.0以上版本實(shí)現(xiàn)應(yīng)用內(nèi)語言切換的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
完美解決Android Studio集成crashlytics后無法編譯的問題
下面小編就為大家?guī)硪黄昝澜鉀QAndroid Studio集成crashlytics后無法編譯的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05

