I am using MediaPlayer to play a music in 60 seconds. I want to increase the sound of the music when it is playing follows the rule:
1. First 10 seconds, the volume is 20% of maximum volume
2. From 20 seconds to 30 seconds, the volume is 40% of maximum volume
3. From 30 seconds to 40 seconds, the volume is 80% of maximum volume
3. From 40 seconds to 60 seconds, the volume is 100% of maximum volume
How can I control the volume of the MediaPlayer by programming in Android M?. This is my code to play music. It is missing the task control volume. Thank all
public MediaPlayer mpCalling = null;
private CountDownTimer mCountDownTimer_Playing;
public void playSound(){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
try {
if (mpCalling != null && mpCalling.isPlaying()) {
mpCalling.stop();
mpCalling.release();
mpCalling=null;
}
mpCalling = MediaPlayer.create(getApplicationContext(), notification);
mpCalling.start();
if(mCountDownTimer_Playing!=null){
mCountDownTimer_Playing.cancel();
mCountDownTimer_Playing=null;
}
mCountDownTimer_Playing = new CountDownTimer(60000,1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
mpCalling.stop();
}
};
mCountDownTimer_Playing.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Related
I want to play a sound (custom, not system default) after some seconds when a key is pressed. I got a countdown timer running on the key. So want to play a sound onFinish function.
my code for that key is:
if(isCountDownRunning)
return;
isCountDownRunning = true;
new CountDownTimer(14000, 1000) {
public void onTick(long millisUntilFinished) {
keyboard.getKeys().get(0).label = millisUntilFinished / (1000) + "";
kv.invalidateKey(0);
}
public void onFinish() {
isCountDownRunning = false;
keyboard.getKeys().get(0).label ="ADV";
kv.invalidateKey(0);
}
}.start();
break;
You can add the custom sound in a folder res/raw/ . Then use MediaPlayer to play sound like,
MediaPlayer mMediaPlayer = MediaPlayer.create(this, rid);//rid -->R.raw.customsound
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mMediaPlayer.stop();
}
});
want to play a sound onFinish function
In your onFinish function, add this line
mMediaPlayer.start();
I have mediaPlayer for playing sound in android and we want play files based on start time and finishing time
for example :
start 10370 ,end: 14759 [OR] start 4754 ,end: 7836
i used this code for my problem but it's not working perfect
Pause/Stop MediaPlayer Android at given time programmatically
My code :
mediaPlayer.setDataSource(fd);
mediaPlayer.prepare();
mediaPlayer.seekTo(words.getW_start());
mediaPlayer.start();
handler.postDelayed(stopPlayerTask, words.getW_end());
Do you Know why code for these times doesn't work?
I could resolve problem .complete code:
mediaPlayer.setDataSource(fd);
mediaPlayer.prepare();
mediaPlayer.seekTo(words.getW_start());
mediaPlayer.start();
new CountDownTimer(endTime, 10) {
public void onTick(long millisUntilFinished) {
if(MediaPlayerUtility.getTime(mediaPlayer)>=endTime){
mediaPlayer.stop();
}
}
public void onFinish() {
}
}.start();
and MediaPlayerUtility class:
public class MediaPlayerUtility {
public static long getTime(MediaPlayer mediaPlayer) {
long totalDuration = mediaPlayer.getDuration(); // to get total duration in milliseconds
long currentDuration = mediaPlayer.getCurrentPosition(); // to Gets the current playback position in milliseconds
return currentDuration;
}
}
I created an Android application that is like a metronome.
Actually I want to play a beep sound every n milliseconds.
I use MediaPlayer and timer for this.
My code is like this:
Soloution 1:
start_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Timer timer = new Timer("MetronomeTimer", true);
TimerTask tone = new TimerTask() {
#Override
public void run() {
//Log
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss:SS");
Date date = new Date();
j++;
Log.i("Beep", df.format(date) + "_____" + j);
//Play sound
music = MediaPlayer.create(MainActivity.this, R.raw.beep);
music.start();
}
};
timer.scheduleAtFixedRate(tone, 500, 500); // every 500 ms
}
});
When I run this code, everything is OK. But after 15 times loop, line of Log works fine, but sound is muted. And occasionally every 15, or 20 Log, sound plays and stop.
Solution 2:
I move this line:
music = MediaPlayer.create(MainActivity.this, R.raw.beep);
out of TimerTask (like this):
start_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
music = MediaPlayer.create(MainActivity.this, R.raw.beep);
Timer timer = new Timer("MetronomeTimer", true);
TimerTask tone = new TimerTask() {
#Override
public void run() {
//Log
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss:SS");
Date date = new Date();
j++;
Log.i("Beep", df.format(date) + "_____" + j);
//Play sound
music.start();
}
};
timer.scheduleAtFixedRate(tone, 500, 500); // every 500 ms
}
});
This code is OK too until 500 ms for repeating. when period time decrease to 400 or 300 ms, every 2 Log, one sound beeps.
How to repair this code that work fine.
SoundPool is better for playing short sounds that are loaded from memory. I have also had problems implementing MediaPlayer, SoundPool has just been a much easier and lower latency experience.
If you set .setMaxStreams() to a number above 1 you shouldn't get any muted sounds. Try and experiment.
I have been integrating the YoutubePlayer API for with my Android project. The videos from the playlist are working well. What I am trying to do is have the audio start low and fade in over 30seconds.
What I have done so far, for some reason is doing somthing really weird. It set it to max volume and if you try to turn it down it turns it back up again really straight away.
The way I thought I could do it was to set the audio manager to a 0 volume and then on an ontick timer raise that by 1 until it got to the max volume.
The code below was my attempt, but like I said it is doing weird things.
Thanks for your help
youTubePlayer.loadPlaylist(playListTitle);
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
// Add listeners to YouTubePlayer instance
youTubePlayer.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onLoaded(String arg0) {
youTubePlayer.play();
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
Integer volumeRaisingTime = 30000;
new CountDownTimer(volumeRaisingTime, 1000) {
Integer deviceMaxVolume = 20;
Integer devicevolume = 0;
public void onTick(long millisUntilFinished) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
if (devicevolume > deviceMaxVolume){
devicevolume++;
Log.d("Device Volume:", ""+devicevolume);
}
}
public void onFinish() {
}
}.start();
}
I want to increase the sound by one level up in each and every 5 seconds meanwhile the sound clip plays.Below is my code:-
MediaPlayer player;
player=MediaPlayer.create(this, R.raw.alarm);
player.setLooping(true);
Is there any method in which i can track each and every 5 minutes when over.How can i achieve this?
I think you can achieve this with java TimerTask, this is an example code, not tested but should work with no big modifications :
Basically you start a task every 5 seconds, in the run() function of the TimerTask you level up your sound level, and when you reach the maxSoundLevel you call cancel() to stop the task.
//Put this in global
int REFRESH_INTERVAL = 5 * 1000; //5 seconds
int maxSoundLevel = 10; // Number of loop to get to max level
int curSoundLevel = 0; //Start at 0 volume level
//Put this after you started the sound
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), 0, REFRESH_INTERVAL);
//Put this after your method
private class MyTimerTask extends TimerTask{
public void run() {
if(curSoundLevel < maxSoundLevel)
{
float logLevel = (float)(Math.log(maxSoundLevel-curSoundLevel)/Math.log(maxSoundLevel));
yourMediaPlayer.setVolume(1-logLevel);
curSoundLevel ++;
}
else {
this.cancel();
}
}
}
If you have more questions feel free to ask me.