I was wondering if releasing my media player before I play a random sound is bad practice:
So I don't usually deal with media output too much, but I am making a simple app that plays a random sound every time a button is clicked (sounds [] is an array filled with raw media files)
public void onClick(View v){
if(mediaplayer != null){
mediaplayer.release();
}
mediaplayer = MediaPlayer.create(this, sounds[randomNum])
mediaplayer.start();
}
So my question is, would releasing my media player every time before creation be considered good/bad practice? Would there be any better way to do this, as releasing and re-initializing the MediaPlayer object seems like it would consume resources...
Thanks,
Ruchir
You typically use release() when you no longer want to use a MediaPlayer any more. Once you call that, it can never be used again. It effectively destroys the native components that back its functionality.
If you do release, you will have to prepare the media all over again the next time you want to play it. This can be a time consuming process. If you want a sound to play responsively to a button press, you probably don't want to have to prepare it each time.
Related
I'm creating a narrative application in which I need to play videos.
When playing a media with MediaPlayer.playMedia(Media media).
In order to avoid the black background I wait the wait for the playing event before showing the media player.
player.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
#Override
public void playing(MediaPlayer mediaPlayer) {
showVideo();
}
}
The problem is that this event is triggered before the video actually starts playing, so it still displays black for a little while before.
How can I find a way around this? Thanks
I don't know for certain, but you could try some other event listener methods - perhaps the videoOutput, elementaryStreamAdded or elementaryStreamSelected (and check the stream type parameter was video), or maybe wait for the very first positionChanged event (whilst not ideal, it might work well enough for your use case).
I have a program that plays a sound every time the user does an action. But after some time none of the audio works.
Sample code :
public void bPressed(View view) {
MediaPlayer mp = MediaPlayer.create(activity.this, R.raw.audio);
mp.start();
}
You need to use mp.release(); When you are done with MediaPlayer:
As an example, consider the problems that could happen if you forgot to release the MediaPlayer when your activity is stopped, but create a new one when the activity starts again. As you may know, when the user changes the screen orientation (or changes the device configuration in another way), the system handles that by restarting the activity (by default), so you might quickly consume all of the system resources as the user rotates the device back and forth between portrait and landscape, because at each orientation change, you create a new MediaPlayer that you never release. (For more information about runtime restarts, see Handling Runtime Changes.)
http://developer.android.com/intl/es/guide/topics/media/mediaplayer.html
add mp.release(); for example in your onStop()
I want to create a music loop pad, something like this: https://www.youtube.com/watch?v=fwBPYwiYp-Y
Right now, i am using MediaPlayer to play the sounds. I have a global variable that stores the current time (position) of the first audio that is playing, and when i press another button the corresponding audio file starts playing at the position given by the global variable.
My proble is: the sounds are playing out of sync.
can you give me some help? How can i sync the sounds? like this app https://www.youtube.com/watch?v=fwBPYwiYp-Y
Can you point me some info or tutorials about how to create a loop pad?
Thanks
Richardd
Programming something like this is harder than you think. Here are two things you must do to have the sounds play in sync:
Make sure all you sounds are at the same tempo, and are cropped properly - that means that the starting and ending of the sound clip must be exactly on beat - another solution would be to store the starting and ending time for each clip (very easy with OOP)
Use a timer to make sure the sound starts playing on beat. For example, if you wanted to make your music run at 120 bpm, you would need to have your timer fire every 500 ms.
Here is some code (only concept, won't compile):
Timer beatTimer;
boolean isDrumLoopEnabled;
boolean isDrumLoopCurrentlyPlaying;
Sound drumloop;
public void startMusic(){
//prepare timer
beatTimer=new Timer();
beatTimer.interval=500;//for 120 bpm
beatTimer.onTick+=onTickHandler;
isDrumLoopPlaying=false;
isDrumLoopCurrentlyPlaying=false;
//load sounds
Sound drumloop=new Sound("/storage/emulated/drumloop.ogg");
//start music
beatTiemr.start();
}
//this method will run on every beat:
public void onTickHandler(){
if(isDrumLoopEnabled){
//make sure drum loop isn't already playing
if(!isDrumLoopCurrentlyPlaying){
//begin looping drumloop in background
drumloop.startloop();
isDrumLoopCurrentlyPlaying=true;
}
}else{
if(isDrumLoopCurrentlyPlaying){
//stop playing drumloop
drumloop.stopPlaying();
}
}
}
//when the button is pressed...
public void drumloop_buttonpressed(){
//this will toggle whether to play the drum loop:
isDrumLoopEnabled=!isDrumLoopEnabled;
}
I have a MediaPlayer (.wav file) that sometimes needs to be repeated in rapid succession. If it's already playing, I restart it:
if (player.isPlaying()) {
player.pause();
player.seekTo(0);
}
player.start();
The problem is that when the MediaPlayer is interrupted at some random position, there's often a tiny but noticeable scratchy noise at the end.
One solution I've tried is to create an array of MediaPlayers that load the same wav file, cycle through them, never interrupt, and ignore the call if the current
item is already playing (which happens rarely, and the missed call isn't noticed in the general barrage anyway).
MediaPlayer player = players.get(current);
if (!player.isPlaying())
player.start();
if((++current) >= players.size())
current = 0;
This gets rid of the noise, but the solution is kind of ugly. Is there a better way?
The noise is probably the waveform being cut mid-cycle. It can possibly be fixed with calls to setVolume before stopping and starting. Having multiple MediaPlayers isn't a terrible idea, but you may want to have a look at SoundPool.
My app plays a coin sound every time a button is pressed.
coin_sound.start();
You can easily press faster than the coin sound. When this happens I want the coin sound to start from the beginning ever time the button is pressed.
if(coin_sound.isPlaying()){
coin_sound.reset();
coin_sound = MediaPlayer.create(getContext(), R.raw.coin02);
}
coin_sound.start();
The problem with this is that loading a media file tiny as it may be is still a relatively slow process. When you start to click the button really fast the app lags hard.
Are there any solutions to my problem? The only idea I have is to do something with an array of coin_sounds, but this method seems like it will be messy and gross...
The other answer posted here is somewhat correct. You should not call create over and over.
The code in that answer has a problem, though. The reset method sends the MediaPlayer into the idle state, where it is illegal to call most other methods. If you were to go that route, you have to call methods in the following order:
coin_sound.reset();
coin_sound.setDataSource(...);
coin_sound.prepare();
coin_sound.start();
The difference between calling create and the previous sequence of method calls is simply the creation of a new instance. That, however, is not the quickest way to do what should be done.
You should simply call coin_sound.seekTo(0); when you want the current playing sound to restart. So do something like:
if (coin_sound.isPlaying()) coin_sound.seekTo(0);
else coin_sound.start();
That assumes you have left the MediaPlayer in the prepared state so start can be called. You can accomplish that by calling reset, setDataSource, and prepare in the onCompletion listener. Also, make sure to call release when the sound is no longer needed.
It is because you are initiating coin_sound in the button click event, try this
initiate this variable in your oncreate method
coin_sound = MediaPlayer.create(getContext(), R.raw.coin02);
then make this your code for your button
if(coin_sound.isPlaying()){
coin_sound.reset();
}
coin_sound.start();
the problem is you are recreating a new media player each time the button is clicked so the new media player doesnt think there is a sound
and do you need to start it again with coin_sound.start();? doesnt restart stop then start the sound for you?