playing android audio from a specified time - android

How can i play audio from a specified time ?
I have a prerecorded audio and i want to play it from a specific time to a specific time.
I have tried seekTo function in MediaPlayer but it doesnt seem to work.
mPlayer = new MediaPlayer();
try{
mPlayer.setDataSource(PATH_FILE + selectedAudio);
mPlayer.prepare();
mPlayer.seekTo(2);
mPlayer.start();
}catch(IOException ioe){
System.out.println("Error in startPlaying method");
}
Thanks in advance

Related

Android - record sound and check sound volume

I am trying to find out if my device is recording audio correctly (Volume of recorded audio is not too low and actually the recorded file has sound). The way I tried doing it is:
start recording --> play sound --> stop recording --> get file recorded max volume
The code I used to record sound:
public void playSound() {
File myDataPath = new File(getActivity().getFilesDir().getAbsolutePath()
+ File.separator + ".CheckAudio");
if (!myDataPath.exists())
myDataPath.mkdirs();
recordFile = myDataPath + File.separator + "Recording_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()) + ".mp3";
am.setStreamVolume(AudioManager.STREAM_RING, am.getStreamMaxVolume(AudioManager.STREAM_RING), 0);
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
try {
md = new MediaRecorder();
md.setAudioSource(MediaRecorder.AudioSource.MIC);
md.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
md.setOutputFile(recordFile);
md.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
md.prepare();
md.start();
} catch (IllegalStateException | IOException e) {
recording = false;
removeItem("Unable to record audio, please try again."); // (Show toast)
return;
}
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getActivity(), defaultRingtoneUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mediaPlayer.prepare();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
md.stop();
md.release();
mediaPlayer.release();
mediaPlayer = null;
// get recordfile volume
}
});
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
removeItem("Unable to play audio");
sound = false;
}
}
However, I can't find out how to analyze the mp3 file created and check if it is not empty (from sound), is there a library or another way?
I hope you guys understood what I am trying to achieve as my English is pretty bad, Thanks.
EDIT:(Some more explaination)
If you play sound (ringtone or something) while recording sound from microphone, the decibels recorded should be around 90 decibels. meaning the sound playing working and also the microphone, but if the decibels recorded around 30 means only microphone is working, and playing sound not, if the decibels are around zero then the microphone is not working.
You can use a visualiser to visualise real time if recording sound is getting too low or too loud.
I have build a project which visualise recording sound strength via bar graph . Higher the bar louder the recorded sound lower the bar low decibels .
This project also have inapp player which allow user to play all his recordings. The inbuilt player also visualise playback sound data.
I am suggesting this because I thought this is what you are trying to achieve in
start recording --> play sound --> stop recording --> get file recorded max volume.
Instead of getting max volume each time you can rely on visualiser to keep an eye on recorder if recording file is getting recorded above acceptable decibals.
You can find source code on github
https://github.com/hiteshsahu/Android-Audio-Recorder-Visualization-Master

background sound during call in android

I want to play a sound automatically when I call to somebody
but I don't know the way for this.
It is possible because we have some application that play some noise (traffic and ...) to background of call but I could not find a code or something for this
example of sound play in background
this is a code to play music
MediaPlayer player;
AssetFileDescriptor afd;
try {
// Read the music file from the asset folder
afd = getAssets().openFd(“home.mp3″);
// Creation of new media player;
player = new MediaPlayer();
// Set the player music source.
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),afd.getLength());
// Set the looping and play the music.
player.setLooping(true);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
but can we use this during call?

Streaming music and save cache file on local on Android

I use below code to play streaming music:
try {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
mediaPlayer.start();
}
catch(Exception e) {
e.printStackTrace();
}
I want to buffering file to local cache file at the same time.
And next time want to play this url music can play the local file directly.
How can I do it?
Or anyone can provide references?

MediaRecorder records bad audio file on Droid X2

I'm trying to record audio using MediaRecorder on the Droid X2, and I'm running into issues. The MediaRecorder seems to prepare and start recording just fine, but when I stop recording and try to listen to the file that is produced the playback immediately stops. Even if I try to open the audio file in the standard media player app, it immediately stops.
Here's the code I'm using to record:
mCurrentRecordingFilePath = mContext.getExternalFilesDir(null).getAbsolutePath() + File.separator + fileName;
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setOutputFile(mCurrentRecordingFilePath);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
// mRecorder.setAudioEncodingBitRate(16);
// mRecorder.setAudioSamplingRate(44100);
try {
mRecorder.prepare();
mIsPrepared = true;
} catch (IOException e) {
Log.e("Test", "MediaRecorder prepare() failed");
e.printStackTrace();
}
if (mRecorder != null && mIsPrepared) {
mRecorder.start();
mIsRecording = true;
Log.i("Test", "Started audio capture: " + mCurrentRecordingFilePath);
}
I commented out the setAudioEncodingBitRate() call because it was causing the prepare() to fail, and just to be on the safe side I also commented out setAudioSamplingRate(). I have tried every combination of output format and audio encoder that is available and the result is always the same. I get no exceptions, and a file is created that is not empty, but it will not play back properly.
Not sure if it will help diagnose, but here's the code I use to stop recording:
if (mRecorder != null && mIsRecording) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
mIsPrepared = false;
mIsRecording = false;
Log.i("Test", "Stopped audio capture");
}
The recording code works fine on a Galaxy Nexus, Nexus S, EVO 4G, and Galaxy SII. Any idea why my audio file would be bad on the Droid X2?
Found the answer here: MediaPlayer cant play audio files from program data folder?
Problem wasn't the recording, it was the way I was setting the MediaPlayer's datasource. Though I have no idea why the stock media player app wouldn't play the file... maybe it's a permission thing. Here's the code I was using to play the file:
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(fileName);
mPlayer.prepare();
mPlayer.start();
Here's the code that will work if the file is stored in the external files directory:
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource((new FileInputStream(fileName)).getFD());
mPlayer.prepare();
mPlayer.start();

How to play audio file of format wav in android webview?

This is my code: mp3 format file plays without any error, but wav format brings MediaPlayer error(1,-1):
try {
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("h*.wav");
player.prepare();
player.start();} catch (Exception e) {
// TODO: handle exception
}
Kindly make a reference to Android supported media-formats

Categories

Resources