【发布时间】:2011-11-21 09:25:53
【问题描述】:
我正在使用下面的代码来录制声音。
但是,当我在 pc 中播放录制的文件时。没有音乐播放器可以播放。在手机上玩得很好。
任何人都可以说我如何在台式机或笔记本电脑上播放 android 录制的文件。
我正在使用的代码。
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class AudioRecorder extends Activity {
AudioRecorder ar;
String FILENAME="/compare1.wav";
final MediaRecorder recorder = new MediaRecorder();
String path;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("GPS","kartik");
LinearLayout layout=new LinearLayout(this);
Button btnStart = new Button(this);
Button btnStop=new Button(this);
btnStart.setText("START");
btnStop.setText("STOP");
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
GetPath(FILENAME);
start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
stop();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
layout.addView(btnStart);
layout.addView(btnStop);
setContentView(layout);
}catch (Exception e) {
e.printStackTrace();
}
}
public String GetPath(String path) {
return this.path = sanitizePath(path);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
/**
* Starts a new recording.
*/
public void start() throws IOException {
try{
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
try{
recorder.stop();
recorder.release();
}catch (Exception e) {
e.printStackTrace();
}
}
}
谢谢。
【问题讨论】:
标签: android android-2.2-froyo android-sdk-2.1