8

I just convert a mp3 file to byte code and reconvert that byte code to mp3 and saved to sdcard, all the process are going successfully but problem is that the saved mp3 file is not playing on device mp3 player it showing unsupported format.

Is there any problem in my below code

  private void convertBytesToFile(byte[] bytearray) {


    byte[] bytes = bytearray;

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

    byte[] decoded = Base64.decode(encoded, 0);
    //Utilities.log("~~~~~~~~ Decoded: ", Arrays.toString(decoded));

    try
    {
        File file2 = new File(Environment.getExternalStorageDirectory() + "/hello-2.mp3");
        FileOutputStream os = new FileOutputStream(file2, true);

        os.write(decoded);
        os.close();
    }
    catch (Exception e)
    {
        Toast.makeText(this, "Somthing wrong", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

}

or anything am I missing from. Please help friends.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Bikash
  • 477
  • 1
  • 4
  • 16
  • 1
    Why do you encode and decode in `Base64`? How `Base64` is anyhow related to `mp3` encoding? – Nicolas Filotto Nov 14 '16 at 08:55
  • Yeah I think my process is wrong.Is there any another way ? Actually I just want to join two mp3 file and make another mp3 file that's why I just try to encode and decode using Base64. Any other way to make this ? Plz help. – Bikash Nov 14 '16 at 09:02
  • You could check [this](http://stackoverflow.com/questions/21381585/concatenate-mp3-files-in-java) – Nicolas Filotto Nov 14 '16 at 09:04
  • consider reading [this](http://stackoverflow.com/questions/15088806/appending-two-mp3-files-in-android) too – Nicolas Filotto Nov 14 '16 at 09:06
  • Let me check.. Thank you.. :D – Bikash Nov 14 '16 at 09:11
  • @NicolasFilotto Thank you so much, But that was not my answer, Any example regarding joining two mp3 files through coding or any library ? Please help. – Bikash Nov 14 '16 at 09:34
  • Well, If I were you I would rather do it with FFmpeg https://trac.ffmpeg.org/wiki/Concatenate – Nicolas Filotto Nov 14 '16 at 09:37
  • Hi as you are refer I am trying to use FFmpeg on my android studio. Now after compiling in terminal its giving me following errors..arm-linux- If you think configure made a mistake, make sure you are using the latest version from Git. If the latest version fails, report the problem to the ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net. Include the log file "config.log" produced by configure as this will help solve the problem. what to do now – Bikash Nov 15 '16 at 07:37
  • It showing C compiler test failed. I am install gcc and g++ and my ndk path is NDK=D:/Users/BIP042.BIPADBBSR/AppData/Local/Android/Sdk/ndk-bundle whats the wrong any idea.. Please help.. – Bikash Nov 15 '16 at 09:02
  • Have you read [this](http://stackoverflow.com/questions/4725773/ffmpeg-on-android)? – Nicolas Filotto Nov 15 '16 at 11:34
  • It is not possible to just join two mp3 files. The beginning of the second file will habe meta-data attached that is not allowed to be in the middle of your newly generated file. Is your example a minimal test, without joining two files? – Jonas Köritz Nov 21 '16 at 08:23
  • Yeah its a minimal test.. – Bikash Nov 21 '16 at 09:37
  • you have to do signal processing , convert both files to same frequency rate and channel , and then combine these files , you can find more information about signal processing in google – Zulqurnain Jutt Nov 21 '16 at 09:43
  • Thanks let me check about signal processing.. :) – Bikash Nov 21 '16 at 09:47

6 Answers6

0

Why did you convert that mp3 to Base64? If you want to save to sd card do below code

private void convertBytesToFile(byte[] bytearray) {

    FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/hello-2.mp3");
    fos.write(byteArray);
    fos.close()
    }
noobEinstien
  • 3,147
  • 4
  • 24
  • 42
0

For playing MP3 ( In your scenario )

private void playMp3(byte[] mp3SoundByteArray) {
        try {
            // create temp file that will hold byte array
            File tempMp3 = File.createTempFile("fmtemp", "mp3", getCacheDir());
            tempMp3.deleteOnExit();
            FileOutputStream fos = new FileOutputStream(tempMp3);
            fos.write(mp3SoundByteArray);
            fos.close();
            mediaPlayer.reset();
            FileInputStream fis = new FileInputStream(tempMp3);
            mediaPlayer.setDataSource(fis.getFD());
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
AndroidHacker
  • 3,596
  • 1
  • 25
  • 45
0

Android SDK doesn't have mp3 encoder. If you want to save byte[] to .mp3 file,Use native library as below.

https://github.com/naman14/TAndroidLame https://github.com/yhirano/SimpleLameLibForAndroid

suresh
  • 414
  • 3
  • 11
0

As Jonas metioned, merging two mp3 files is not as easy as merging their bytes as the files contain meta-data. There is a solution though, you can use FFmpeg. You can either use the native build or a wrapper such as JavaCV. Here is sample of how to merge two Mp3 files using JavaCV

      FrameGrabber grabber = new FFmpegFrameGrabber("input.mp3"); 
             grabber.start(); 

  FrameGrabber grabber2 = new FFmpegFrameGrabber("input2.mp3"); 
             grabber2.start(); 

             FrameRecorder recorder = new FFmpegFrameRecorder("output.mp3", 
    grabber.getAudioChannels()); 
             recorder.setSampleFormat(grabber.getSampleFormat()); 
             recorder.setSampleRate(grabber.getSampleRate()); 
             recorder.start(); 
             Frame frame; 
             while ((frame = grabber.grabFrame()) != null) { 
                 recorder.record(frame); 
             }
            while ((frame = grabber2.grabFrame()) != null) { 
                 recorder.record(frame); 
             } 
             recorder.stop(); 
             grabber.stop(); 
            grabber2.stop(); 

PS Im assuming that the sample rate for both files is the same

Community
  • 1
  • 1
Niza Siwale
  • 2,390
  • 1
  • 18
  • 20
0

https://github.com/adrielcafe/AndroidAudioConverter

(Convert audio files inside your Android app easily. Supported formats: AAC, MP3, M4A, WMA, WAV and FLAC)

Misagh
  • 3,403
  • 1
  • 20
  • 17
-1

Try this code may resolve your issue,

private void convertBytesToFile(byte[] bytearray) {
    try {

        File outputFile = File.createTempFile("MediaFile", "mp3", getCacheDir());
        outputFile.deleteOnExit();
        FileOutputStream fileoutputstream = new FileOutputStream(outputmediafile);
        fileoutputstream.write(bytearray);
        fileoutputstream.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

Play the media file in any media player.