Tuesday, May 29, 2012

How to convert and Java project to Android Library project ?

Today, I had to add  reference to a Java project. So I right clicked on the project and checked Properties but there was no "Android" in the treeview in the properties. How to fix this ?

1. Right click and close the project.
2. Open the .project file in notepad.
3. Search for <natures> </natures> tag and change to

<natures>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
4. Save the file and open again in Eclipse.
5. Now you need to create "project.properties" and "AndroidManifest.xml" manually. (I just copied these two files from another project :D)
6. Right click on the project -> Android Tools -> Fix project properties.

After that when I right click and go to properties I can see "Android" in the listview :)



Friday, May 25, 2012

How to change screen timeout to never programmatically?



Settings.System.putString(cr,                     Settings.System.SCREEN_OFF_TIMEOUT, "-1");

Wednesday, May 23, 2012

How to capture sound from microphone and play it on speakers in c#

I have been using NAudio for one of our applications to record sound. Today, I wanted to play back the live recording over the speakers while doing the live record. After few mins of Googling, hammering and fixing. I managed to get something working.


private IWavePlayer waveOut;
private BufferedWaveProvider waveProvider;
private WaveIn m_WaveIn;


private void StartRecording() {
       // Setup Incoming
m_WaveIn = new WaveIn();
m_WaveIn.BufferMilliseconds = 50; // This is very very important.
m_WaveIn.DeviceNumber = set your device here;
m_WaveIn.DataAvailable += WaveIn_DataAvailable;
m_WaveIn.WaveFormat = m_waveFormat;
m_WaveIn.StartRecording();

       // Setup Output


waveOut = new WaveOut();
waveProvider = new BufferedWaveProvider(m_waveFormat);
waveOut.Init(waveProvider);
waveOut.Play();


}


public void WaveIn_DataAvailable(object sender, WaveInEventArgs e) {
    byte[] buffer = e.Buffer;
    waveProvider.AddSamples(buffer, 0, buffer.Length);
}

Thats it. This works like a charm. Hope this will be useful for someone else as well.


Wednesday, May 9, 2012

Your own CountDownTimer in Android


Android CountDownTimer sucks. Why ? because in CountDownTimer.java (Android source code) it creates a mHandler inside the code which requires to call Looper.prepare() and Looper.loop() before calling new CountDownTimer(), You are likely to get Can't create handler inside thread that has not called Looper.prepare() because of this.

  So I wrote my own version of  CountDownTimer using Thread.

Here is it.


abstract class CountDownTimer extends Thread {
    public abstract void onFinish();
    private boolean mCancelled = false;
 
    public void run() {
    mCancelled = false;
    start(60);
    }
 
    private final void start(long secs) {
    long delay = secs * 1000;
do {
delay = delay - 1000;
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
if(mCancelled) {
return;
}
} while (delay > 0);
onFinish();
    }
        public final void cancel() {
    mCancelled = true;
    }
}

How to use
=========


CountDownTimer countDownTimer = new CountDownTimer()  {
@Override
public void onFinish() {

}
};

countDownTimer.start();