How to join two audio files?

May 11, 2013 | Rishabh Agrawal | General

By joining here I mean concatenating (Joining files end to end).

After searching for a week and reading about different articles I came up with this :

Here I am joining two audio files, you can do this for as many audio files or video files.

I am hoping that you have your sound/audio file stored in memory/sdcard.

Step 1.

Getting the sound/audio files exact location.
Here I am storing that location on file itself.

String path_of_song1, path_of_song2;
path_of_song1 =  "/sdcard/RingCut/sounds/audio1.mp3" ;
path_of_song2 =  "/sdcard/RingCut/sounds/audio2.mp3"
File fileSong1 = new File(path_of_song1);
File fileSong2 = new File(path_of_song2);

Here fileSong1 and fileSong2 are the two files which contains the exact location of my audio. It could be of any format here .mp3 is used just as example.

Step 2.

Converting files into byteArray:

We cannot join these sounds directly , we have to decompose these files into smallest available streams.
I have converted these files into byte[] .
Converting
fileSong1 into byteArray;

byte[] data1;
ByteArrayOutputStream buffer1 = new ByteArrayOutputStream();
		InputStream is;
		try {
			is = new FileInputStream(fileSong1);

			byte[] temp = new byte[1024];
			int read;
			Log.i("hemant", "value in temp " + temp);
			while ((read = is.read(temp)) >= 0) {
				buffer.write(temp, 0, read);
			}
		} catch (FileNotFoundException e1) {

			e1.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();
		}
data1 = buffer1.toByteArray();

Similarly you can get data2 from second file path(fileSong2).

Step 3

Joining these byte[];

Now you have two byte[] in form data1, and data2.
Now you have to join these byte[]. For doing that, there are many ways.
you can use anyone of that ,

Here I am using a simple way to join two bytearrays.

byte[] combined;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			outputStream.write(data1);
			outputStream.write(data2);
			combined = outputStream.toByteArray();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 combined = outputStream.toByteArray();

Step 4 :

Saving that byteArray to the sdcard and make it playable.
Here is the last step of joining two sounds, till now we got joined bytearray, (
combined) .
The question is how to make it playable, for that we have save that byte[] in form of file to our memory/sdcard with preferred extension for example I am using .mp3.
You can do this by:

try {
			FileOutputStream fos;
			String Song = "Song";

			File outputFile = null;

			// create a File object for the parent directory
			File SongDirectory = new File("/sdcard/RingCut/sounds");

			// have the object build the directory structure, if needed.
			SongDirectory.mkdirs();
			// create a File object for the output file

			outputFile = new File(SongDirectory, Song
					+ android.text.format.DateFormat.format("yyyyMMdd_kkmmss",
							new Date()) + ".mp3");
			fos = new FileOutputStream(outputFile);

			Log.i("hemant", "value inn fos2" + fos);
			fos.write(combined);
			fos.close();

		} catch (Exception e) {

		}

Now you can get your mixed song at “/sdcard/RingCut/sounds/”datetime”.mp3

Enjoy your song now.

Post By: Hemant Bisht


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal