package com.example.qundui.recordlearn;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import java.io.File;
import java.io.IOException;
/**
* Created by qundui on 2018/3/5.
*/
public class AudioRecordTest extends AppCompatActivity {
//设置音频数据格式:PCM 16位每个样本,保证设备支持。PCM 8位每个样本,不一定能得到设备的支持。
private int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
//设置音频的录制声道,CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道
private static int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
private int frequence = 8000; //录制频率,单位hz.这里的值注意了,写的不好,
//设置音频的采样率,44100是目前的标准,但是某些设备仍然支持22050,16000,11025
private static int sampleRateInHz = 44100;
private boolean isRecord = false;
private AudioRecord audioRecord;
private int bufferSizeInBytes = 0;//缓冲区大小
//AudioName裸音频数据文件
String AudioName ;
//NewAudioName可播放的音频文件
String NewAudioName ;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
//创建播放实例
String path = getExternalCacheDir().getPath();
mediaPlayer = new MediaPlayer();
AudioName = path + "/reav.wav";
NewAudioName = path + "/yinyue.raw";
}
public void record(View view) {
bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioEncoding, bufferSizeInBytes);
audioRecord.startRecording();
isRecord = true;
new MyTread().start();//开始录制声音
Log.i("gsc","开始录制声音");
}
/**
* @param view 停止播放
*/
public void stop(View view) {
if (audioRecord != null) {
audioRecord.stop();
audioRecord.release();
//停止写入音频文件
isRecord = false;
audioRecord = null;
}
Log.i("gsc","停止录制声音");
}
class MyTread extends Thread {
@Override
public void run() {
super.run();
SoundUtils.writeDataToFile(bufferSizeInBytes, AudioName, isRecord, audioRecord);//把录制的音频裸数据写入到文件中去
SoundUtils.copyWaveFile(AudioName, NewAudioName, sampleRateInHz, bufferSizeInBytes);//给裸数据加上头文件
}
}
private final class PrepareListener implements MediaPlayer.OnPreparedListener {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();//开始播放
Log.i("gsc","开始播放录制声音");
}
}
/**
* @param view 开始播放
*/
public void play(View view) {
System.out.println("----开始播放000000----");
System.out.println("----NewAudioName-----"+NewAudioName);
File file = new File(NewAudioName);
System.out.println("--------"+file.exists());
mediaPlayer.reset();
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(NewAudioName);//问题就在这里![这里写图片描述](https://img-blog.csdn.net/20180306212251513?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzA1MTkzNjU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
// mediaPlayer.setDataSource(this, Uri.parse(NewAudioName));
mediaPlayer.prepare();//进行数据缓冲
mediaPlayer.setOnPreparedListener(new PrepareListener());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("----开始播放0000008899----");
}
@Override
protected void onDestroy() {
super.onDestroy();
stop(null);
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
log 日志:
```![这里写图片描述](http://img.blog.csdn.net/20180306212310647?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzA1MTkzNjU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
修改后的代码:
<div class="se-preview-section-delimiter"></div>
```package com.example.qundui.recordlearn;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import java.io.File;
import java.io.IOException;
/**
* Created by qundui on 2018/3/5.
*/
public class AudioRecordTest extends AppCompatActivity {
//设置音频数据格式:PCM 16位每个样本,保证设备支持。PCM 8位每个样本,不一定能得到设备的支持。
private int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
//设置音频的录制声道,CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道
private static int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
private int frequence = 8000; //录制频率,单位hz.这里的值注意了,写的不好,
//设置音频的采样率,44100是目前的标准,但是某些设备仍然支持22050,16000,11025
private static int sampleRateInHz = 44100;
private boolean isRecord = false;
private AudioRecord audioRecord;
private int bufferSizeInBytes = 0;//缓冲区大小
**//AudioName裸音频数据文件 ## 标题 ##
private static final String AudioName = "/sdcard/love.raw";
//NewAudioName可播放的音频文件
private static final String NewAudioName = "/sdcard/new.wav";**
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
//创建播放实例
String path = getExternalCacheDir().getPath();
mediaPlayer = new MediaPlayer();
// AudioName = path + "/reav.wav";
// NewAudioName = path + "/yinyue.raw";
}
public void record(View view) {
bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioEncoding, bufferSizeInBytes);
audioRecord.startRecording();
isRecord = true;
new MyTread().start();//开始录制声音
Log.i("gsc","开始录制声音");
}
/**
* @param view 停止播放
*/
public void stop(View view) {
if (audioRecord != null) {
audioRecord.stop();
audioRecord.release();
//停止写入音频文件
isRecord = false;
audioRecord = null;
}
Log.i("gsc","停止录制声音");
}
class MyTread extends Thread {
@Override
public void run() {
super.run();
SoundUtils.writeDataToFile(bufferSizeInBytes, AudioName, isRecord, audioRecord);//把录制的音频裸数据写入到文件中去
SoundUtils.copyWaveFile(AudioName, NewAudioName, sampleRateInHz, bufferSizeInBytes);//给裸数据加上头文件
}
}
private final class PrepareListener implements MediaPlayer.OnPreparedListener {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();//开始播放
Log.i("gsc","开始播放录制声音");
}
}
/**
* @param view 开始播放
*/
public void play(View view) {
System.out.println("----开始播放000000----");
System.out.println("----NewAudioName-----"+NewAudioName);
File file = new File(NewAudioName);
System.out.println("--------"+file.exists());
mediaPlayer.reset();
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(NewAudioName);
// mediaPlayer.setDataSource(this, Uri.parse(NewAudioName));
mediaPlayer.prepare();//进行数据缓冲
mediaPlayer.setOnPreparedListener(new PrepareListener());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("----开始播放0000008899----");
}
@Override
protected void onDestroy() {
super.onDestroy();
stop(null);
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
这里写代码片
修改后运行的LOg日志:
跪求大佬解释为啥
mediaPlayer.setDataSource(NewAudioName);
无法读取getExternalCacheDir().getPath(); 路径
权限都加了
mediaPlayer.setDataSource(NewAudioName);
无法读取getExternalCacheDir().getPath(); 路径
权限都加了
错误的LOG日志为: