Too much delay playing a sound after pressing button - android

I'm creating a sampler app for school. I've wrote code which plays a sample when I press and hold a button and stops when I release it. My problem is that it has too much latency. It takes too long after I press the button to play the sound.
My audio files are mp3's.
Here's my code:
smpl1.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent theMotion)
{
switch (theMotion.getAction())
{
case MotionEvent.ACTION_DOWN:
sample = MediaPlayer.create(MainActivity.this, R.raw.bassdrum);
smpl1.setText("ON");
smpl1.setTextColor(Color.GREEN);
sample.start();
break;
case MotionEvent.ACTION_UP:
smpl1.setText("OFF");
smpl1.setTextColor(Color.RED);
sample.stop();
break;
}
return true;
}

Create the sample object before the button press, then just use the start / stop functionality in your handler code.

Related

Problems with audio in onTouchListener and mediaplayer.setLooping

im trying to do a Soundboard for a school proyect, but I have problems when I pulse the buttons, If I pulse a button twice, the sound does not reproduce again, and, even if the condition fullfills, the audio doesnt loop. I have different buttons, and all of they call a method (each one with different parameters on the call
btn1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
reproducir(mp1, motionEvent, btn1, colorin);
return false;
}
});
public void reproducir(MediaPlayer mp, MotionEvent motionEvent, Button btn, String colorin ){
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
mp.start();
btn.setBackgroundColor(getResources().getColor(R.color.Blanco));
if(instrumento.getText().equals("piano")) {
mp.setLooping(true);
}
break;
case MotionEvent.ACTION_UP:
mp.stop();
ponerColor(colorin);
}
}
The last line calls to a method to put again the original color of the button, thanks a lot
Your Code is Fine Only One tiny Mistake:
You should replace mp.stop(); with mp.pause();
That's All.
Better to it Like this though:
if (mp.isPlaying()){
mp.pause();
}
....
if (!mp.isPlaying()){
mp.start();
}
...
if (!mp.isLooping()){
mp.setLooping(true);
}
I should also let you know that mp.stop() Stops the media, it's Good, But Stop is only to be used in case You want the Whole MediaPlayer Forget what song is being played and we just wanna change The Audio, reset MEdia player and start another Audio.
Hope you find this useful

Keep a sound sample playing till button is released android

I am trying to implement a musical keyboard application that will play string sounds. String sounds are needed to be played till the user releases the key.
I am using a small sample of 1 second with the idea of looping the sample using SoundPool on
MotionEvent.ACTION_DOWN,
and stopping it as
ACTION_UP
gets called. But looping does not seem to work. I can put a "long enough" sound sample assuming no user would keep the button pressed for that long, but that is not quite the way I want the app work.
What should I do?
Try this code:
inside onCreate()
mp=MediaPlayer.create(MainActivity.this,R.raw.beep);
b.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mp.setLooping(true);
mp.start();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
mp.stop();
mp=MediaPlayer.create(MainActivity.this,R.raw.beep);
}
return false;
}
});

Amazon fire remote affecting background process

I have a media application for the Amazon Fire tv and tv stick. I have successfully captured the buttons and have customized the events accordingly.
Issue arises in the case when some other media app such as pandora is running in the background. When I fastforward,rewind etc in my app , even pandora gets changed in the process.Amazon has declined the app for the same reason.How do I get to set the focus of the remote in the current app only.
The following is my code for remote
#Override
public boolean onKeyDown(int keyCode, KeyEvent event){
boolean handled = false;
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_DPAD_LEFT:
mPlayerView.seek((int)mPlayerView.getPosition()-3000);
handled = true;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.e("right","pressed");
mPlayerView.seek((int)mPlayerView.getPosition()+3000);
handled = true;
break;
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
mPlayerView.seek((int)mPlayerView.getPosition()+60000);
handled=true;
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
mPlayerView.seek((int)mPlayerView.getPosition()-60000);
handled=true;
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
mPlayerView.play();
handled=true;
break;
case KeyEvent.KEYCODE_MENU:
subtitle=!subtitle;
if(subsexists){
if(subtitle) {
mPlayerView.setCurrentCaptions(1);
Toast.makeText(this,"Subtitles ON",Toast.LENGTH_LONG).show();
}
else {
mPlayerView.setCurrentCaptions(0);
Toast.makeText(this,"Subtitles OFF",Toast.LENGTH_LONG).show();
}
}
handled=true;
break;
}
return handled || super.onKeyDown(keyCode, event);
}
See this section of the FireTV Developer FAQ. Specifically you need to implement code to:
When app starts playing, request audio focus with AudioManager.requestAudioFocus()
If audio focus was granted, register a media button receiver with AudioManager.registerMediaButtonEventReceiver()
you also need to make sure that you gracefully give up control as well if another media player app has the users attention
Listen for the loss of audio focus with AudioManager.onAudioFocusChangeListener()
If your app loses audio focus, stop playback and unregister the media buttons with AudioManager.unregisterMediaButtonEventReceiver()

VLC Compile. Add Button and play video

Have the source code VLC.
Compiled under android.
Added activity.
It has three buttons.
at the touch of a button should switch the channel stream , video from sd card or video from internet.
public void onClick(View v)
{
switch (v.getId()) {
case R.id.b1:
break;
case R.id.b2:
break;
case R.id.b3:
Context context = this;
VideoPlayerActivity.start(context, "http://www.youtube.com/watch?v=9Xjw2PtKjJE",
null, false, false);
break;
}
All compiled. By clicking the button, a player, but the video does not start to play.
Blank screen and the player does not see the video.
The same problem with the video playback from the card.
Tell me how to handle the press of a button.
Sorry for my English.
Instead of VideoPlayerActivity.start
VLCCallbackTask task = new VLCCallbackTask(MainActivity.this)
{
#Override
public void run() {
AudioServiceController c = AudioServiceController.getInstance();
c.append("http://www.youtube.com/watch?v=9Xjw2PtKjJE");
}
};
task.execute();

Problem with default Android onKey... sound

I'm having hard time figuring out how to get rid of the default sound played while any key is pressed. I'm using following method but it still plays the same "beep" sound.
#Override public boolean onKeyDown( int keyCode, KeyEvent event )
{
switch ( keyCode )
{
case 25:
ChangeImageUP( );
break;
case 24:
ChangeImageDOWN( );
break;
default:
return super.onKeyDown( keyCode, event );
}
return true;
}
Please help.
// Update
I have figure out that if I overwrite onKeyUp in my main activity the beep is gone. But when i start a second activity the beep returns, even after using that onKeyUp method.
That is controlled by the operating system and the users settings. You might not be able to modify it..

Categories

Resources