Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AAC format in Android

Tags:

android

aac

I have a problem with audio on android.

Short Question:

I need to play and record audio in aac format an all Android devices. I found that it is possible starts from API 10, but on my BLU device(2.3.5) it works by using MediaRecorder and MediaPlayer. But on HTC Nexus One it doesn't work. Have you any suggestions?

Long question:

To record and play audio in AAC format I'm using following code. It pretty simple and stupid, but it works for testing.

String pathForAppFiles = getFilesDir()
                .getAbsolutePath();
        pathForAppFiles += "/bla.mp4";
        if (audioRecorder == null) {

            File file = new File(pathForAppFiles);
            if (file.exists())
                file.delete();

            audioRecorder = new MediaRecorder();

            audioRecorder
                    .setAudioSource(MediaRecorder.AudioSource.MIC);

            audioRecorder
                    .setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

            audioRecorder
                    .setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

            audioRecorder.setOutputFile(pathForAppFiles);

            try {
                audioRecorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            audioRecorder.start();

        } else {
            audioRecorder.stop();
            audioRecorder.release();
            audioRecorder = null;

            new AudioUtils().playSound(pathForAppFiles);
        }

new AudioUtils().playSound(pathForAppFiles); - creates MediaPlayer and play sound from file;

To make it work on nexus I tried aac-decoder - it doesn't play file to the end (plays only 6 second of 10 seconds file). And it doesn't plays sound recorded by code above.

Also I tried to install FFmpeg, but I haven't experience to make this library work.

So can you recommend something?

like image 344
Yanny Avatar asked Nov 13 '22 10:11

Yanny


1 Answers

I resolve this issue by changing audio type to MP3, because some devices (like Kindle Fire) do not play aac from anywhere.

My recommendation is: If you want to make cross platform sounds - use MP3. You can convert any sound to MP3 with lame encoder.

like image 60
Yanny Avatar answered Nov 15 '22 13:11

Yanny