I'm developing an Android app that record voice using mediarecorder and play music using mediaplayer.
My goal is to make possible to mix the two audio's into one file, and because Android do not offer any API for it, I am looking for a reasonable solution.
At moment, at play time I'm using a new mediarecorder with MIC source to capture the audio and save it, but this is very poor !!!
Anyway to mix the audio? including any native solution lix SOX or FFMPEG?
Or, anyway to recorder into file using as source the mediaplayer output instead to use the MIC?
Any suggestion is appreciate.
Thank you.
When I faced the same problem I was able to find a solution for mixing the files.
Since mixing of two mp3 file is not possible, you have to first convert it in wave format , then set the header value .after that add the data field. I did this in following way. Hope my code will help you.
class MixFile extends AsyncTask{
ProgressDialog dialog;
protected void onPreExecute() {
dialog= new ProgressDialog(MainActivity.this);
dialog.setCancelable(false);
dialog.setMessage("Mixing two wav files");
dialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
short[] audioData1 = null;
short[] audioData2 = null;
int n = 0;
try {
DataInputStream in1;
// in1 = new DataInputStream(new FileInputStream(Environment.getExternalStorageDirectory() + "/Soundrecpluspro/one.wav"));
in1 = new DataInputStream(new FileInputStream(path1));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while ((n = in1.read()) != -1) {
bos.write(n);
}
} catch (IOException e) {
e.printStackTrace();
}
ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer sb = bb.asShortBuffer();
audioData1 = new short[sb.capacity()];
for (int i = 0; i < sb.capacity(); i++) {
audioData1[i] = sb.get(i);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
DataInputStream in1;
in1 = new DataInputStream(new FileInputStream(path2));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while ((n = in1.read()) != -1) {
bos.write(n);
}
} catch (IOException e) {
e.printStackTrace();
}
ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer sb = bb.asShortBuffer();
audioData2= new short[sb.capacity()];
sb.get(audioData2);
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
// find the max:
float max = 0;
Log.d("File audio lenght 1 ", ""+audioData1.length);
Log.d("File audio lenght 2 ", ""+audioData2.length);
System.out.println("MainActivity.MixFile.doInBackground() 1"+audioData1.length);
System.out.println("MainActivity.MixFile.doInBackground() 2"+audioData2.length);
if(audioData1.length > audioData2.length){
for (int i = 22; i < audioData2.length; i++) {
if (Math.abs(audioData1[i] + audioData2[i]) > max)
max = Math.abs(audioData1[i] + audioData2[i]);
}
System.out.println("" + (Short.MAX_VALUE - max));
int a, b, c;
// now find the result, with scaling:
for (int i = 22; i < audioData2.length; i++) {
a = audioData1[i];
b = audioData2[i];
c = Math.round(Short.MAX_VALUE * (audioData1[i] + audioData2[i])
/ max);
if (c > Short.MAX_VALUE)
c = Short.MAX_VALUE;
if (c < Short.MIN_VALUE)
c = Short.MIN_VALUE;
audioData1[i] = (short) c;
}
// to turn shorts back to bytes.
byte[] end = new byte[audioData1.length * 2];
ByteBuffer.wrap(end).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(audioData1);
try {
OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/assets/mixer12.wav");
for (int i = 0; i < end.length; i++) {
out.write(end[i]);
out.flush();
}
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("MainActivity.MixFile.doInBackground() smaller one");
for (int i = 22; i < audioData1.length; i++) {
if (Math.abs(audioData2[i] + audioData1[i]) > max)
max = Math.abs(audioData2[i] + audioData1[i]);
}
System.out.println("" + (Short.MAX_VALUE - max));
int a, b, c;
// now find the result, with scaling:
for (int i = 22; i < audioData1.length; i++) {
a = audioData2[i];
b = audioData1[i];
c = Math.round(Short.MAX_VALUE * (audioData2[i] + audioData1[i])
/ max);
if (c > Short.MAX_VALUE)
c = Short.MAX_VALUE;
if (c < Short.MIN_VALUE)
c = Short.MIN_VALUE;
audioData2[i] = (short) c;
}
// to turn shorts back to bytes.
byte[] end = new byte[audioData2.length * 2];
ByteBuffer.wrap(end).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(audioData2);
try {
OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/Assets/mixer1.wav");
for (int i = 0; i < end.length; i++) {
out.write(end[i]);
out.flush();
}
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
and in activity i called it as below
public class MainActivity extends Activity implements OnClickListener {
new MixFile().execute();
}
here path1 and path2 is the path of wav file that you want to mix
Related
This array of questions and answers,
R.raw.ikhlas example is the question to answer R.raw.jwbalikhlas
int[] rawQuetion = {R.raw.alfalaq, R.raw.alikhlas, R.raw.alkafirun, R.raw.allahab};
int [] rawAnswer={R.raw.jwbaliklas};
This method to randomize questions
//fisher-yates Shuffle
public void playSoal() {
shuffleArray(rawQuetion);
try{
int idx = new Random().nextInt(rawQuetion.length);
mp = MediaPlayer.create(this, rawQuetion[idx]);
mp.start();
}catch(Exception e){
Log.e("ERROR", "Media Player", e);
mp = null;
mp.release();
mp.stop();
e.printStackTrace();
}
}
static void shuffleArray(int[] arr)
{
Random rnd = new Random();
for (int i = arr.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Swap
int a = arr[index];
arr[index] = arr[i];
arr[i] = a;
}
}
I want when the quiz questions selected at random, will answer here
public void audioFile() throws IOException{
InputStream is = getResources().openRawResource(R.raw.jwbaliklas);//I want this to be obtained from the above array
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in = null;
in = new BufferedInputStream(is);
int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0)
{
out.write(buff, 0, read);
}
out.flush();
byte[] audioBytes = out.toByteArray();
for (int i = 0; i < audioBytes.length; i++) {
audioBytes[i] = (byte) ((audioBytes[i]) & 0xFF); }
absNormalizedSignal = hitungFFT(audioBytes);
AppLog.logString("===== From audio File");
}
If you pass the array id for the resource you want to open, you can use the index directly from the array of integers (as long as it is visible from the audioFile scope.
public void audioFile(#RawRes int i) throws IOException {
InputStream is = getResources().openRawResource(rawQuestion[i]);
...
}
Also, you have to change include the annotation #RawRes:
#RawRes int[] rawQuestion = {R.raw.alfalaq, R.raw.alikhlas, R.raw.alkafirun, R.raw.allahab};
#RawRes int[] rawAnswer = {R.raw.jwbaliklas};
I'm creating an app to read string values over Bluetooth serial port. My data receiving but in two parts. If I send $F00,A,B,0,M# via bluetooth it only reads $ in first part and F00,A,B,0,M# in next part. I provided my code here. Please do correct me if I'm wrong.
InputStream inputStream=null;
int avilableBytes=0;
public ConnectedThread(BluetoothSocket socket){
InputStream temp=null;
try{
temp=socket.getInputStream();
}catch (IOException e){
e.printStackTrace();
}
inputStream=temp;
}
public void run() {
try{
int bytes;
while (true){
try{
avilableBytes=inputStream.available();
if (avilableBytes>0){
byte[] buffer=new byte[avilableBytes];
bytes=inputStream.read(buffer);
final String readMessage=new String(buffer,0,bytes);
bt_handler.obtainMessage(handlerState,bytes,-1,readMessage).sendToTarget();
Log.d("PRAVEEN",readMessage);
}
}catch (IOException e){
e.printStackTrace();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
Data are like stream bytes and can not be processed immediately when it comes with a few bytes. Data will not come all at once as a single packet. You have to use the other byte[] buffer (MainBuffer) in which you will gradually save incoming byte and move the index in that buffer. Then, from time to time (e.g. in the timer once per second) take data from the main buffer and processed it. By default you must implement some data frame with a separator (eg. Data * data * data * - Many ways to do it good or bad). I dealt with this in .net via Xamarin, but just as an example it may be helpfull :
update example, format
In ConnectedThread :
public override void Run()
{
while (true)
{
try
{
int readBytes = 0;
lock (InternaldataReadLock)
{
readBytes = clientSocketInStream.Read(InternaldataRead, 0, InternaldataRead.Length);
Array.Copy(InternaldataRead, TempdataRead, readBytes);
}
if (readBytes > 0)
{
lock (dataReadLock)
{
dataRead = new byte[readBytes];
for (int i = 0; i < readBytes; i++)
{
dataRead[i] = TempdataRead[i];
}
}
Bundle dataBundle = new Bundle();
dataBundle.PutByteArray("Data", dataRead);
Message message = btlManager.sourceHandler.ObtainMessage();
message.What = 1;
message.Data = dataBundle;
btlManager.sourceHandler.SendMessage(message);
}
}
catch (System.Exception e)
{
btlManager.btlState = BTLService.BTLState.Nothing;
}
}
}
In BTLHandler :
public override void HandleMessage(Message msg)
{
switch (msg.What)
{
case 1:
{
byte[] data = msg.Data != null ? msg.Data.GetByteArray("Data") : new byte[0];
btlService.BTLReceiveData(data);
}
break;
}
}
public void BTLReceiveData(byte[] data)
{
lock (dataReadLock)
{
for (int i = 0; i < data.Length; i++)
{
dataRead[dataReadWriteCursor] = data[i];
dataReadWriteCursor++;
}
}
}
In Timer :
int tmpWriteCursor = dataReadWriteCursor;
int tmpReadCursor = dataReadReadCursor;
lock (dataReadLock)
{
int newBytes = dataReadWriteCursor - dataReadReadCursor;
for (int i = 0; i < newBytes; i++)
{
dataReadMain[dataReadReadCursor] = dataRead[dataReadReadCursor++];
}
}
bool odradkovani = false;
string tmpRadek = "";
int lastLineIndex = 0;
List<string> list = new List<string>();
for (int i = LastWriteLineIndex; i < tmpWriteCursor; i++)
{
if (dataReadMain[i] >= 32 && dataReadMain[i] <= 255)
{
tmpRadek += (char)dataReadMain[i];
}
else if (dataReadMain[i] == 13) odradkovani = true;
else if (dataReadMain[i] == 10)
{
if (odradkovani)
{
odradkovani = false;
list.Add(Utils.GetFormatedDateTime(DateTime.Now) + " " + tmpRadek);
tmpRadek = "";
lastLineIndex = i + 1;
}
}
else
{
tmpRadek += "?" + dataReadMain[i].ToString() + "?";
}
}
WriteDataToLog(list);
LastWriteLineIndex = lastLineIndex;
I'm still newbie and I need help to coding my Android Studio >0<
I can't to send a long data, although I change the size of "buffer". What should I do ?
This is the receiver program :
public void run() {
InputStream inputStream;
try {
inputStream = mBTSocket.getInputStream();
while (!bStop) {
byte[] buffer = new byte[1024];
if (inputStream.available() > 0)
{
inputStream.read(buffer);
int i = 0;
/*
* This is needed because new String(buffer) is taking the entire buffer i.e. 256 chars on Android 2.3.4 http://stackoverflow.com/a/8843462/1287554
*/
for (i = 0; i < buffer.length && buffer[i] != 0; i++) {}
final String strInput = new String(buffer, 0, i);
/*
* If checked then receive text, better design would probably be to stop thread if unchecked and free resources, but this is a quick fix
*/
}
Thread.sleep(500);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This are the sender program :
public void sendStringBT(String s)
{
try {
mBTSocket.getOutputStream().write(s.getBytes());
sleep();
Toast.makeText(getApplicationContext(), "Sent...", Toast.LENGTH_SHORT).show();
mBTSocket.getOutputStream().flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is how to we call to send data :
sendStringBT(dataPage6); //send data via Bluetooth
I think the problem is in the design of the receiver (using Sleep in an endless cycle ...) I Solved BTL communication in .net Xamarin, but the principle should be the same.
Reading from btlInputStream must be quick and can not use sleep. You use an endless cycle, reading in buffer (OK). Immediately a dune bytes to an auxiliary large buffer (use read / write cursor) and then, for example, in timer treat the data (I suppose you are using some packet protocol)
while (ContinueCycle)
{
int rxlen;
lock (InternalBufferReadLock)
{//Pouze rychle prectu a schovam si do pole
rxlen = USBConnection.BulkTransfer(USBEndPointRead, InternalBufferRead, InternalBufferRead.Length, 0);
Array.Copy(InternalBufferRead, TempBufferRead, rxlen);
}
lock (BufferReadLock)
{
for (int i = 2; i < rxlen; i++)
{
BufferRead[BufferReadWriteCursor] = TempBufferRead[i];
BufferReadWriteCursor++;
}
}
}
and in timer save it to MainBuffer from which the data is processing
if (tmpWriteCursor > tmpReadCursor)
{
lock (BufferReadLock)
{
int newBytes = tmpWriteCursor - tmpReadCursor;
for (int i = 0; i < newBytes; i++)
{
BufferReadMain[BufferReadReadCursor] = BufferRead[BufferReadReadCursor++];
}
}
}
...
bool newline = false;
string tmpRadek = "";
int lastLineIndex = 0;
List<string> list = new List<string>();
for (int i = LastWriteLineIndex; i < tmpWriteCursor; i++)
{
if (BufferReadMain[i] >= 32 && BufferReadMain[i] <= 255)
{
tmpRadek += (char)BufferReadMain[i];
}
else if (BufferReadMain[i] == 13) newline = true;
else if (BufferReadMain[i] == 10)
{
if (newline)
{
newline = false;
list.Add(Utils.GetFormatedDateTime(DateTime.Now) + " " + tmpRadek);
tmpRadek = "";
lastLineIndex = i + 1;
}
}
else
{
tmpRadek += "?" + BufferReadMain[i].ToString() + "?";
}
}
I received a crash report, which is about java.lang.StringIndexOutOfBoundsException in ZhuangDictActivity$SearchDicAsyncTask.doInBackground
Here is the ZhuangDictActivity$SearchDicAsyncTask.doInBackground:
private class SearchDicAsyncTask extends AsyncTask<String, Integer, String> {
private byte searchStatus;
#Override
protected String doInBackground(String... params) {
if (params[0].length() > 0) {
word = params[0].trim();
long[] index = null;
FileAccessor in = null;
DictZipInputStream din = null;
try {
char key = GB2Alpha.Char2Alpha(word.charAt(0));
tableName = DatabaseHelper.transTableName(key);
index = databaseHelper.queryTable(tableName, word);
if (index != null) {
in = new FileAccessor(new File(dictFileName), "r");
byte[] bytes = new byte[(int) index[1]];
if (isDZDict) {
din = new DictZipInputStream(in);
DictZipHeader h = din.readHeader();
int idx = (int) index[0] / h.getChunkLength();
int off = (int) index[0] % h.getChunkLength();
long pos = h.getOffsets()[idx];
in.seek(pos);
byte[] b = new byte[off + (int) index[1]];
din.readFully(b);
System.arraycopy(b, off, bytes, 0, (int) index[1]);
} else {
in.seek(index[0]);
in.read(bytes);
}
wordDefinition = new String(bytes, "UTF-8");
} else {
searchStatus = 0;
return null;
}
} catch (FileNotFoundException ffe) {
searchStatus = 1;
return null;
} catch (IOException ex) {
ex.printStackTrace();
searchStatus = 2;
return null;
} finally {
try {
if (din != null)
din.close();
if (in != null)
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
return wordDefinition;
}
}
The complete code is available here.
I have limited knowledge in Java and Android development. How should I solve this? I intended to post the complete stack traces but stackoverflow do not allow me to do so because it stated my question has too many code. Anyway, the line which is causing the problem is char key = GB2Alpha.Char2Alpha(word.charAt(0));.
It is possible that your string contains only white spaces. meaning it passed the condition:
if (params[0].length() > 0)
But when you call trim(), these are removed, resulting in an empty stream and an "IndexOutOfBoundsException" exception being thrown when you execute:
word.charAt(0)
EDIT
This is not the reason. After a test, when trim is called on a String with only whitespaces, the String remains unchanged.
Now I am developing a reader. If the txt file is too big, it will spend a long time to read and no response. So I want to set a method like it can read the txt file in bathes.
I write the below code. It can turn the page back. But it can not page up continuous. How should I do?
Vector string;
int begin = 0;
public void readTxtByPage(String fileName) {
string.clear();
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(filePath + fileName);
br = new BufferedReader(fr);
br.skip(begin);
String content = "";
char ch;
int line = 0;
int w;
int len;
int start;
FontMetrics fm = paint.getFontMetrics();
fontHeight = (int) Math.ceil(fm.descent - fm.top) + 2;
pageLineNum = textHeight / fontHeight;
float[] widths = new float[1];
while ((content = br.readLine()) != null) {
len = content.length();
w = 0;
start = 0;
for (int i = 0; i < len; i++) {
ch = content.charAt(i);
paint.getTextWidths(String.valueOf(ch), widths);
w += Math.ceil(widths[0]);
if (w > textWidth) {
string.addElement(content.substring(start, i));
begin += (i - start);
start = i;
w = 0;
line++;
if (line >= pageLineNum) {
System.out.println("begin===>"+begin);
return;
}
}
}
string.addElement(content.substring(start));
begin += (len + 2 - start);
line++;
if (line >= pageLineNum) {
return;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return;
}
Thanks in advance!
Sounds like it would be best if you did this work in a background thread then, you can tell the reader to sleep, and then notify it when you want the thread to start reading again. That way it wont freeze your UI and it can read the bits in batches. Maybe have the thread be notified during an onTouchEvent.