Can't play .m4a or .3gpp audio files in my website - java

Basically, I built an app in android that records my message and saves it as .m4a or .3gpp format.
When I plays the records in my app it works fine, but when I'm trying to play it on my website it doesnt work...
Android(Java)
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
Website(HTML)
<audio controls="controls" preload="none">
<source src="my_record.m4a" type="audio/mp4"/>
</audio>
P.S: When I tried to open some other m4a audio files(files that i found online), I succeded.

The audio tag is quite sensitive about this. Anything above 128mbps it will not play. A lot of encoders automatically choose the highest quality bit rate (usually around 320mbps) and the audio tag won't play them. Sample rate should be 44100hz.
the sampling rate supported by AAC audio coding standard ranges from 8 to 96 kHz, the sampling rate supported by AMRNB is 8kHz, and the sampling rate supported by AMRWB is 16kHz.
Hence change Audioencoder to AAC in your code
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
to
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
and then set filename extension to .mp3
Hope this works for you.:)

Related

Google speech to text with java and twilio

I am having problem while converting audio file to text using google speech to text. I am able to download the file from Twilio but when I supply that audio file to google speech then it gives me 0 length response. But if I convert this downloaded file using vlc media player and then supply it to google speech then it gives me right output. Please help me on this I am stuck for about a week now.
After getting response from Twilio I save it in a file with .wav extension
InputStream in = new URL(jsonObject.get("redirect_to").toString()).openStream();
Files.copy(in, Paths.get("src/main/resources/mp.wav"), StandardCopyOption.REPLACE_EXISTING);
Below is the google speech to text code.
Path path = Paths.get("src/main/resources/mp.wav");
byte[] content = Files.readAllBytes(path);
ByteString audioBytes = ByteString.copyFrom(content);
try (SpeechClient speech = SpeechClient.create()) {
RecognitionConfig recConfig =
RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setLanguageCode("en-US")
.setSampleRateHertz(44100)
.setModel("default")
.setAudioChannelCount(2)
.build();
RecognitionAudio recognitionAudio = RecognitionAudio.newBuilder().setContent(audioBytes).build();
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response =
speech.longRunningRecognizeAsync(recConfig, recognitionAudio);
while (!response.isDone()) {
System.out.println("Waiting for response...");
Thread.sleep(10000);
}
List<SpeechRecognitionResult> results = response.get().getResultsList();
for (SpeechRecognitionResult result : results) {
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcription: %s%n", alternative.getTranscript());
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
As #philnash has suggested, by appending a .mp3 extension to the recording URL, the MP3 version of the recording can be downloaded from Twilio. The same applies to the '.wav' extension as well.
InputStream in = new URL(jsonObject.get(“redirect_to”).toString()+”.mp3”).openStream(); // or “.wav”
Files.copy(in, Paths.get(“src/main/resources/mp.wav”), StandardCopyOption.REPLACE_EXISTING);
I tested this out with a sample Twilio recording and the ffprobe results are below.
Downloaded .wav file
Input #0, **wav**, from 'from-twilio-change-extension.wav':
Duration: 00:00:14.60, bitrate: 128 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, 1 channels, s16, 128 kb/s
Downloaded .mp3 file
Input #0, **mp3**, from 'from-twilio-change-extension.mp3':
Duration: 00:00:14.68, start: 0.000000, bitrate: 32 kb/s
Stream #0:0: Audio: mp3, 22050 Hz, mono, fltp, 32 kb/s
As for audio encodings supported by the Speech-to-Text API, both WAV and MP3 are supported but MP3 is a Beta feature available only in the version v1p1beta1. So, the client library imports will look like com.google.cloud.speech.v1p1beta1.Packages.... The audio encoding in RecognitionConfig has to be changed according to the encoding of the audio file used. For a .wav file, RecognitionConfig.AudioEncoding.LINEAR16 has to be used, and for a .mp3 file, RecognitionConfig.AudioEncoding.MP3 has to be used.
An alternative would be to use the FFMPEG tool to convert audio files into one of the recognized codecs by Speech-to-Text. More information about usage of the tool can be found here. In your scenario, the .mka to .wav/.mp3 conversion can be done from the Java code as shown below.
String[] ffmpegCommand = {"ffmpeg", "-i", "/full/path/to/inputFile.mka", "/full/path/to/outputFile.wav"};
ProcessBuilder pb = new ProcessBuilder(ffmpegCommand);
pb.inheritIO();
pb.start();

Getting recent Bytes while recording Audio Android

I am working on an Audio recording function. I want the recorded Audio to be saved into the internal cache directory of my app so that I can later process it and send it to my server. I have taken the RECORD_AUDIO_PERMISSION in my Android Manifest.
Below is the code I plan to use for recording audio and save it to a file.
String uuid = UUID.randomUUID().toString();
fileName = getExternalCacheDir().getAbsolutePath() + "/" + uuid + ".3gp";
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(fileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
recorder.start();
} catch (IOException e) {}
I expect the above code to work fine but I am facing another issue. I want to create a Waveform effect for my app for which I am using this library. This library works with the below code:
//get a reference to the visualizer
mVisualizer = findViewById(R.id.blast);
//TODO: get the raw audio bytes
//pass the bytes to visualizer
mVisualizer.setRawAudioBytes(bytes);
Now, my question is how can I get the Bytes in real-time of the Audio which is being recorded and being saved? Should I read the file and extract recent bytes from it at regular intervals or is there any other method to achieve this.
Any help would be appreciated.
Thanks.
As per the logic what you can do is take input of small intervals, say 1 second(1000 ms), and then show the waveform of that and after that save the data you got. Now after saving take new input and then add that new data in the previous data after forming a waveform (or doing any operation) of new data.
Just do these things on separate Threads.

How to read audio file in dart flutter?

In Python, It's possible to read an audio file as an array of samples and receive its sample rate with:
array_of_samples, samplerate = librosa.load('filename.mp3')
also:
array_of_samples, samplerate = sf.read('existing_file.wav')
Is there any similar way to do it in Dart code?

Java: playing audio from a youtube video

I'm thinking about coding a Java applet that would take in the top 100 or so songs, find their samples (music that appears within the songs) off WhoSampled.com and then playing those samples off of YouTube.
My problem is the playing part, let's say I have the URL. What's the best way to deal with that in Java, do you think ripping the audio off and playing the audio from there would be best, or should I try to control a sentient YouTube player.
I'm leaning towards extracting the audio, and this: thread mentions a way to extract that audio, however the code:
wget http://www.youtube.com/get_video.php?video_id=...
ffmpeg -i - audio.mp3
Is not written in Java. How do I, if possible convert this to run in a Java program? Or does anyone know a good way in Java
Thank you for your suggestions.
You can use an FFMPEG Java wrapper like this one https://github.com/bramp/ffmpeg-cli-wrapper/
An example can be found in Readme. Converting MP4 to mp3 should be like this:
FFmpeg ffmpeg = new FFmpeg("/path/to/ffmpeg");
FFprobe ffprobe = new FFprobe("/path/to/ffprobe");
FFmpegBuilder builder = new FFmpegBuilder()
.setInput("input.mp4") // Filename, or a FFmpegProbeResult
.overrideOutputFiles(true) // Override the output if it exists
.addOutput("output.mp3") // Filename for the destination
.setFormat("mp3") // Format is inferred from filename, or can be set
.setAudioCodec("aac") // using the aac codec
.setAudioSampleRate(48_000) // at 48KHz
.setAudioBitRate(32768) // at 32 kbit/s
.done();
FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
// Run a one-pass encode
executor.createJob(builder).run();

Android record in FLAC file

How can I make a record with a microphone in FLAC file?
I have tried this:
import javaFlacEncoder.FLAC_FileEncoder;
FLAC_FileEncoder flacEncoder = new FLAC_FileEncoder();
File outputFile = new File(dir + "/flac1.flac");
flacEncoder.encode(file, outputFile);
Error:
E/AndroidRuntime(5891): java.lang.VerifyError: javaFlacEncoder/FLAC_FileEncoder
Whether it is possible to record sound in format Wav using Java 1.6 and android 4.0.3?
From what earlier comments here were alluding to:
From this link Android appears only support AMR, PCM and GSM codecs. If you need .flac (often google's services favorite) I suggest using the lossless PCM (supports .wav) formats and using FFMPEG, SOX or some other audio converter service to then make them .flac.
(all codecs implemented in Android 3.1x (level 12) so this should apply to your version)

Categories