How can I put a song to repeat in an android application? - android

I put in background of my android application a song. I don't know how much time the application is open. And I want to put this song to repeat. My code is:
MediaPlayer mySong;
mySong = MediaPlayer.create(X_0Activity.this, R.raw.tj);
mySong.start();

Uri mediaUri = createUri(context, R.raw.media); // Audiofile in raw folder
Mediaplayer mPlayer = new MediaPlayer();
mPlayer.setDataSource(context, mediaUri);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.prepare();
mPlayer.setLooping(true); // for repeat song
mPlayer.start();

mySong.setLooping(true) // repeat Song
mySong.start(),
And now you are ready with repeat mode on.

Use SoundPool, you can easily loop it any time you want! Here is a very good example: Play sound with SoundPool
In the
spool.play(soundID, volume, volume, 1, 0, 1f);
the number 0 represents the number you want to repeat the song. For infinite loop the suitable value is -1.

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

playing android audio from a specified time

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

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?

How to play music files in raw folder continuously

Creating music player related application,In raw folder having 5 mp3 files.want to play continuously and individually ,
else if (theText == song3) {
mediaPlayer = MediaPlayer.create(this, R.raw.hiii);
} else if (theText == song4) {
mediaPlayer = MediaPlayer.create(this, R.raw.hiiii);
}
This is how i am playing individual songs but how to play 5 songs continuously.Help me.
1.make an array of id if songs in raw folder.
int arr[]={R.id.hiii,R.id.Hiiii,...........}
2.use this array in your code
mediaPlayer=MediaPlayer.create(this, arr[i]);
3.implement setOnCompletionListener(Context); to start another song after one is over and loop it

Determining the duration and format of an audio file

Given a path (on the SD card) to an audio file, what is the best way of determining the length of the audio in milliseconds and the file format (or Internet media type)?
(For the duration one could use MediaPlayer's getDuration-method, but this seems too slow/clumsy.)
For the length of the audio file:
File yourFile;
MediaPlayer mp = new MediaPlayer();
FileInputStream fs;
FileDescriptor fd;
fs = new FileInputStream(yourFile);
fd = fs.getFD();
mp.setDataSource(fd);
mp.prepare();
int length = mp.getDuration();
mp.release();
Check this for MimeType:
https://stackoverflow.com/a/8591230/3937699
I think the easiest way is:
MediaPlayer mp = MediaPlayer.create(this, Uri.parse(uriOfFile);
int duration = mp.getDuration();
mp.release();
/*convert millis to appropriate time*/
return String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(duration),
TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration))
);
Just taking a stab at an answer for you, but you could probably determine the media type by the file extension - which I think MediaFile may be able to help you with. As for duration, I believe the getDuration() method is actually a native call, so I don't know if you will be able to do it much faster.

Categories

Resources