Monday, April 13, 2015

how to convert bytes(or String) to 3gp file??


hello.


i'm coding an App. which records the voice(voice recorder) , converts it to array of bytes then Encodes it to base64 .


so i decode the base64 file to normal String or bytes.


now i need to convert normal String(or bytes) variable to a voice file.


summary :


1_record a voice


2_convert to arrays of byte


3_encoded to base64


4_decode the arrays of byte


5_convert it to a playable sound file


here is my code :



PHP Code:



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

text = (TextView) findViewById(R.id.text1);
// store it to sd card
outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/recordfile.3gpp";

myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myRecorder.setOutputFile(outputFile);

startBtn = (Button)findViewById(R.id.start);
startBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
start(v);
}
});

stopBtn = (Button)findViewById(R.id.stop);
stopBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
stop(v);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

playBtn = (Button)findViewById(R.id.play);
playBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
play(v);
}
});

stopPlayBtn = (Button)findViewById(R.id.stopPlay);
stopPlayBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopPlay(v);
}
});
}

public void start(View view){
try {
myRecorder.prepare();
myRecorder.start();
} catch (IllegalStateException e) {
// start:it is called before prepare()
// prepare: it is called after start() or before setOutputFormat()
e.printStackTrace();
} catch (IOException e) {
// prepare() fails
e.printStackTrace();
}

text.setText("Recording Point: Recording");
startBtn.setEnabled(false);
stopBtn.setEnabled(true);

Toast.makeText(getApplicationContext(), "Start recording...",
Toast.LENGTH_SHORT).show();
}
String encoded;
File file ;
public void stop(View view) throws IOException{

try {
myRecorder.stop();
myRecorder.release();
myRecorder = null;
// file = new File(Environment.getExternalStorageDirectory() + "/recordfile.3gpp");


FileInputStream in=new FileInputStream(file=new File(outputFile+"/recordfile.3gp"));
byte fileContent[] = new byte[(int)file.length()];

in.read(fileContent,0,fileContent.length);

encoded = Base64.encodeToString(fileContent,0);
// Utilities.log("~~~~~~~~ Encoded: ", encoded);


stopBtn.setEnabled(false);
playBtn.setEnabled(true);
text.setText("Recording Point: Stop recording");

Toast.makeText(getApplicationContext(), "Stop recording...",
Toast.LENGTH_SHORT).show();
} catch (IllegalStateException e) {
// it is called before start()
e.printStackTrace();
} catch (RuntimeException e) {
// no valid audio/video data has been received
e.printStackTrace();
}
}

public void play(View view) {
try{
myPlayer = new MediaPlayer();

FileOutputStream out=new FileOutputStream(outputFile+"/decoded.3gp");
byte[] decoded = Base64.decode(encoded, 0);

out.write(decoded);
out.close();
myPlayer.setDataSource(outputFile+"/decoded.3gp");
myPlayer.prepare();
myPlayer.start();

playBtn.setEnabled(false);
stopPlayBtn.setEnabled(true);
text.setText("Recording Point: Playing");

Toast.makeText(getApplicationContext(), "Start play the recording...",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

the output: records the voice but not playing. and it makes a file recordfile.3gp on SDcard which it play from sdcard corectly....

the output should be :


records the voice and playes it. make two files :recordfile.3gp and decoded.3gp


dosen't make the secode file??!!!



No comments:

Post a Comment