在我的Android应用中,我记录了一个文件。 WAV在这种模式下:
public class MainActivity extends ActionBarActivity {
private static final int RECORDER_BPP = 16;
private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
private static final String AUDIO_RECORDER_FOLDER = "Audio";
private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
private static final int RECORDER_SAMPLERATE = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private static final String String = null;
short[] audioData;
private static AudioRecord recorder = null;
private static int bufferSize = 0;
private Thread recordingThread = null;
private boolean isRecording = false;
/*Complex[] fftTempArray;
Complex[] fftArray;*/
int[] bufferData;
int bytesRecorded;
TextView tv;
private Button ca;
File f2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_audio);
setButtonHandlers();
enableButtons(false);
bufferSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
audioData = new short [bufferSize]; //short array that pcm data is put into.
tv = (TextView)findViewById(R.id.textView1);
ca = (Button)findViewById(R.id.button2);
}
private void setButtonHandlers() {
((Button)findViewById(R.id.button1)).setOnClickListener(btnClick);
((Button)findViewById(R.id.btStop)).setOnClickListener(btnClick);
}
private void enableButton(int id,boolean isEnable){
((Button)findViewById(id)).setEnabled(isEnable);
}
private void enableButtons(boolean isRecording) {
enableButton(R.id.button1,!isRecording);
enableButton(R.id.btStop,isRecording);
}
private String getFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmm");
String strDate = sdf.format(c.getTime());
if(!file.exists()){
file.mkdirs();
}
String fileaudio= new String("record");
f2= new File(file.getAbsolutePath() + "/" + fileaudio + "-" +strDate +AUDIO_RECORDER_FILE_EXT_WAV);
return (file.getAbsolutePath() + "/" + fileaudio + "-" +strDate +AUDIO_RECORDER_FILE_EXT_WAV);
}
private String getTempFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
if(!file.exists()){
file.mkdirs();
}
File tempFile = new File(filepath,AUDIO_RECORDER_TEMP_FILE);
if(tempFile.exists())
tempFile.delete();
return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
}
private void startRecording(){
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
writeAudioDataToFile();
}
},"Audio Thread");
recordingThread.start();
}
private void writeAudioDataToFile(){
......}
private void stopRecording(){
if(null != recorder){
isRecording = false;
recorder.stop();
recorder.release();
recorder = null;
recordingThread = null;
}
copyWaveFile(getTempFilename(),getFilename());
}
private void deleteTempFile() {
File file = new File(getTempFilename());
file.delete();
}
private void copyWaveFile(String inFilename,String outFilename){
......
}
private void WriteWaveFileHeader(
......
}
private View.OnClickListener btnClick = new View.OnClickListener() {
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:{
enableButtons(true);
startRecording();
.......
}
};
};
现在,我想在单击按钮时打开的另一个活动中使用此记录的文件。因此,我创建了“更改”方法
我记得在botton的onclick()中。在这种方法中,我想更改“活动”并传递文件路径。这是我的代码:
public void change (View view){
Intent changeActivity;
changeActivity = new Intent (this, SecondActivity.class);
startActivity(changeActivity);
String filepath = Environment.getExternalStorageDirectory().getPath();
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmm");
String strDate = sdf.format(c.getTime());
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
File f2= new File(file.getAbsolutePath() + "/" + "record" + "-" +strDate +AUDIO_RECORDER_FILE_EXT_WAV);
String path = f2.getAbsolutePath();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("lname", path);
startActivity(intent);
}
}
在第二个活动中,我以这种方式调用文件:
Intent intent = getIntent();
lName = intent.getStringExtra("lname");
File storage = Environment.getExternalStorageDirectory();
File file = new File(storage,lName);
这段代码无法正常工作,因为没有文件路径通过。为什么?有人能帮我吗?
参考方案
因为您是从上一个活动发送文件的绝对路径,所以lName
包含包含文件而不是仅文件名的完整路径,因此,请使用File类构造函数(将一个参数作为绝对文件路径)创建文件:
File file = new File(lName);
android-android studio模拟器中的SSL问题,在手机上可以正常工作 - java我有一个可以通过https调用网络服务的应用程序。当我在手机上运行apk时,效果很好。但是,在模拟器中,所有通过POST的SSL请求均失败,并显示以下信息: 读取错误:ssl = 0xb402be00:SSL库失败,通常是一个协议 错误 错误:100c50bf:SSL例程:ssl3_read_bytes:NO_RENEGOTIATION(外部/无聊的sl /…
Android:如何为wrap_content设置高度动画? - java我需要使用ValueAnimator来使用户拖动特定视图时出现自定义“放置字段”。 (我想将字段从gone, height = 0更改为visible, height = wrap_content)。我已经尝试过以下问题的解决方案:How to animate to wrap_content?当我在单个TextView上使用它时,答案就起作用了,但是当我尝试…
从服务器加载XML布局(android) - java我对此问题进行了大量研究,但没有得到想要的答案。所以我确实有一个应用程序从服务器获取字符串。字符串系列采用XML格式。这是我将从服务器获得的示例(如您所见,它是一个布局):<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to…
在Android Studio中设计可滚动内容时应遵循的方法是什么? - java我正在约束布局中设计我的应用程序,并将其放置在“滚动视图”下。我想放置更多的Card Views,但是在xml文件的预览中没有放置空间。当布局已满时,我应该在哪里拖放Buttons,TextViews等?我已经处理了文本的滚动视图,但这是另外一回事。我知道我只能输入代码,但是当内容超出定义的屏幕尺寸时,我将无法看到我正在设计的内容时,我将很难这样做。(如果您…
android eclipse按钮的OnClick事件 - java我有2个文件:main_activity.xml和home.xml。我在main_activity.xml中做了一个按钮这是代码片段:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://…