I'm using an Android MediaPlayer to play a bytestream that I receive externally.
Now I know it isn't possible to use a bytestream as input for my MediaPlayer (that would be perfect).
Therefor I want to create a file out of this stream and let the MediaPlayer just read that file.
But now I want to know exactly how far the MediaPlayer is in reading this file/stream, not in milliseconds but in bytes.
But is it possible to have some-sort of callback to know howmany bytes this MediaPlayer has read?
Related
I want create an app which records a sound into bytestream and it can be listened again. It will not save the recorded audio into a file, rather it will save it into a hardware memory. And a play option will be there by which the recorded file can be played. Recorded system will be like talking tom: record the audio and play it instantly
Can anyone provide me a sample code for this functions ?
Without saving the audio into a file, you can use the AudioRecord class which lets you read the recorded audio into a byte array.
An easier option to code and use would be the MediaRecorder, although it will come with a loss in performance as it does save the audio into a file every time.
I am streaming from an mp3 stream using the MediaPlayer.
I set the datasource, call preparyAsync(), call start() on the onPrepared() callback.
Now I want to know the number of bytes downloaded by the mediaplayer when streaming a song.
Is there a way to track the number of bytes?
Its not going to be one song but instead an mp3 stream.
I understand you want to know the number of bytes of downloaded compressed data, not the number the bytes of uncompressed data inside MediaPlayer which is proportional to the lenght of audio played. (The compressed data may not be proportional in case of using VBR for example).
To get the number of bytes of downloaded compressed data you need to count the bytes of stream downloaded by MediaPlayer, but I believe the only way to have access to the stream downloaded is to implement a local streamproxy that feeds the MediaPlayer, or a second solution is to save the stream to a local file and open this file with MediaPlayer, but the later solution has some limitations as MediaPlayer locks the local file and data cannot be appended.
For the first solution see this solution on how to implement a proxy to feed MediaPlayer: MediaPlayer stutters at start of mp3 playback
I'm developing an audio processing application where I need to record audio, and then process it to obtain features of that recording. However, I want the audio in a playable format to play it after with MediaPlayer.
I've seen that to record audio to process it it's better to use AudioRecord, because I can get the raw audio from there. But then I can't write the data to a file in a playable format (is there any library to do this in android?).
I used this method to record raw data and then write it into a file:
http://andrewbrobinson.com/2011/11/27/capturing-raw-audio-data-in-android/
But when i try to play this file on the device, it is not playable.
Then if I use MediaRecorder I don't know how to decode the data to extract the features. I've been looking at MediaExtractor, but it seams that MediaExtractor doesn't decode the frames.
So.. what's the best way to do this? I imagine that's common in any audio processing application, but I wasn't able to find the way to manage this.
Thanks to your replies.
Using AudioRecord is the right way to go if you need to do any kind of processing. To play it back, you have a couple options. If you're only going to be playing it back in your app, you can use AudioTrack instead of MediaPlayer to play raw PCM streams.
If you want it to be playable with other applications, you'll need to convert it to something else first. WAV is normally the simplest, since you just need to add the header. You can also find libraries for converting to other formats, like JOrbis for OGG, or JLayer for MP3, etc.
For best quality result you have to use AudioRecord class instead of MediaRecorder.
Please have a look to below link:
Need a simple example for audio recording
Also have a look to this link: http://i-liger.com/article/android-wav-audio-recording
If you use AudioRecord object to get the raw audio signal, the next step to save it save as a playable file is not so difficult, you just need to add a WAV Head before the audio data you capture, then you get a .WAV file which you can play it on most mobile phones.
A .WAV file is a file under the RIFF format. the RIFF header for WAV file is 44 byte long and contains the sample rate, sample width and channel counts information. you can get the detail information from here
I did the sample function on Android phones and it worked.
I can receive a byte array from a Socket. I need to play audio from this byte array - the audio is encoded with AMR 8000Hz.
I found that I can play AMR audio with MediaPlayer. However, MediaPlayer can't play music from byte array, and I don't want to write them to file.
Is there a way to play AMR sound from byte array on android?
The Android framework allows you to play back audio data directly from memory using the AudioTrack class; the drawback is that the audio must already be decoded into PCM data. If you are lucky enough to target Android 4.1, there are new APIs that allow you to decode the data separately so it can be passed to AudioTrack (see MediaExtractor and MediaCodec). However, prior to that there were no exposed APIs for encoding/decoding beyond MediaRecorder and MediaPlayer.
If targeting a version of Android prior to 4.1 (which I imagine you probably are) you have two options:
Find a 3rd party decoder for the AMR data so you can pass it on to AudioTrack
Save your data to a file (even temporarily) so it can be handed to MediaPlayer
HTH
I writing simple application that need to make some record of the incoming sound.
That mean that if the user will turn the record on => the application need to 'listen' what the user is saying and convert the sound of the user to byte array and save the byte array to some file ( mp3 format ) .
I don't finding any way to get the sound that coming from the user ..
Someone can help me with this issue ?
Thanks for any help.
Performing Audio Capture
Create a new instance of android.media.MediaRecorder.
Set the audio source using MediaRecorder.setAudioSource(). You will probably want to use MediaRecorder.AudioSource.MIC.
Set output file format using MediaRecorder.setOutputFormat().
Set output file name using MediaRecorder.setOutputFile().
Set the audio encoder using MediaRecorder.setAudioEncoder().
Call MediaRecorder.prepare() on the MediaRecorder instance.
To start audio capture, call MediaRecorder.start().
To stop audio capture, call MediaRecorder.stop().
When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. Calling MediaRecorder.release() is always recommended to free the resource immediately.
Example and source can be found here:
http://developer.android.com/guide/topics/media/audio-capture.html
You could also use AudioRecorder if you need other things and codecs (for example raw pcm 16 bit).
With AudioRecorder you get directly the bytes which you can process like you want (ie converting to mp3 codec yourself).
Anyhow I think you should explain what exactly you want to do.. ie if you like to process the audio for speech recognition mp3 is not what you want.