My app keeps crashing when I click the "startButton". I guess the problem is somewhere within that onTouch method, but I've tried solving the problem for hours; disabling different things inside the startButton.setOnTouchListener doesn't make a difference. Can you find anything wrong?
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
final Button startButton = (Button) this.findViewById(R.id.button1);
final TextView timeLeft = (TextView) this.findViewById(R.id.timeLeft);
MediaPlayer.create(getBaseContext(), R.raw.songthingy);
startButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mp.start();
timeLeft.setText("Status: Initiated");
startButton.setText("Stop");
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mp.release();
}
});
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
timeLeft.setText("Status: Starting in... "
+ millisUntilFinished / 1000);
}
public void onFinish() {
timeLeft.setText("Status: Finished");
}
}.start();
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
;
return true;
}
});
}
There is no exception stack in question, but one issue I see is:
MediaPlayer mp;
is pointed to null and you are calling
mp.start();
in OnTouchListener, which results in NullPointerException.
I think what you need to do is:
final TextView timeLeft = (TextView) this.findViewById(R.id.timeLeft);
mp= MediaPlayer.create(getBaseContext(), R.raw.songthingy);
Related
I was working in a very simple music player with a seek bar. But I got a problem with playing music,it doesn't work correctly every second - it backwards a while, then remain again like it's a buggy.
I know why but I can't solve it.
When the progress bar moves forward, the music recalibrates and vice versa, when the music moves forward, the progress bar recalibrates.
It's probably because of that that every time it goes back.
But I tried several things and still have the same problem.
Another problem is that when the music is paused, the progress bar goes in the opposite direction until it reaches zero. It doesn't stay where it was before the break.
The full code :
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private boolean bool = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.seekBar = (SeekBar) findViewById(R.id.sound_bar);
this.mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.music);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition() * seekBar.getMax() / mediaPlayer.getDuration());
}
}, 500, 500);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mediaPlayer.seekTo(seekBar.getProgress() * mediaPlayer.getDuration() / seekBar.getMax());
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void playSound(View view) {
Button button = (Button) view;
if(bool) {
mediaPlayer.pause();
bool = false;
button.setText("Jouer le son");
} else {
mediaPlayer.start();
bool = true;
button.setText("Mettre en pause");
}
}
#Override
protected void onPause() {
super.onPause();
mediaPlayer.pause();
}
#Override
protected void onStart() {
super.onStart();
if(bool) {
mediaPlayer.start();
}
}
}
Maybe use another thread to control the seekbar would help.
Take a look at this
I want write value from SeekBar to log in new Thread. I want press button and start write logs. and change seekBar value and write it in log.
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, View.OnTouchListener {
private SeekBar seekBar;
private Button button;
private TextView textView;
private EditText editText;
private Thread thread;
private int value = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
editText = (EditText) findViewById(R.id.editText);
seekBar.setOnSeekBarChangeListener(this);
button.setOnTouchListener(this);
print();
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
value = progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
print();
Toast.makeText(MainActivity.this, "ACTION_DOWN", Toast.LENGTH_SHORT).show();
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
thread.stop();
Toast.makeText(MainActivity.this, "ACTION_UP", Toast.LENGTH_SHORT).show();
return true; // if you want to handle the touch event
}
return false;
}
public void print() {
Runnable runnable = new Runnable() {
#Override
public void run() {
Log.d("MyLog", String.valueOf(value));
}
};
thread = new Thread(runnable);
thread.start();
}
}
but my log write once. How can I write log every second?
You could do something like this:
Handler handler = new Handler();
boolean stop = false;
Runnable runnable = new Runnable() {
#Override
public void run() {
Log.d("MyLog", String.valueOf(value));
if (!stop) {
handler.postDelayed(this, 1000);
}
}
};
// start logging by calling this method
public void print() {
handler.post(runnable);
}
// stop logging by calling this method
public void printStop() {
stop = true;
}
It works for me:
new Thread(new Runnable() {
#Override
public void run() {
while(true) {
//do whatever you want
try {
Thread.sleep(1000); //sleep time in milliseconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
I have a button that does not have a function until an onTouchListener is called. As you guys may know, if you press a button before it has a function, the app crashes. I was wondering if it was possible to make the button not clickable until the onTouchListener is called? My .java file is below.
public class Prompt1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prompt1);
touchLayout.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
redo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=1;
}
});
}
return true;
}
});
}
Thanks in advance
You could disable the button until the onTouchListener is registered:
myButton.setEnabled(false);
And then when you register the listener:
myButton.setEnabled(true);
You can modify your code like this:
public class Prompt1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prompt1);
Button button = (Button) findViewById(R.id......);
button.setEnabled(false);
touchLayout.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=1;
}
});
return true;
}
}
});
button.setEnabled(true);
}
I have song which duration is 15min. But I would like to play this song for 30 minutes. Also when the user will download PRO version of the application, the song should play for 2 hours. How can I set the time?
I am new to Java and Android.
Thanks for your help.
public class MainActivity extends Activity {
Button start, stop;
// Button btn_play, btn_stop, btn_pause;
MediaPlayer mp;
TextView display, comment;
int length;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
length = 0;
start = (Button) findViewById(R.id.bStart);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(MainActivity.this, R.raw.splahsound);
mp.seekTo(length);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.reset();
mp.release(); // free up memory
mp = null;
length = 0;
}
});
}
});
stop = (Button) findViewById(R.id.bDur);
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp != null) {
try {
mp.stop();
mp.reset();
mp.release();
mp = null;
length = 0;
} catch (Exception e) {
Log.d("error", e.toString());
}
}
}
});
}
}
Use TimerTask. A similar question has already been answered here, so pointing to that.
When a sound that the user previously selected is playing, how can I stop that sound and immediately play the new selected sound? The code I have so far only stops the new sound, but the user has to select the sound again in order to play it.
public class MainActivity extends Activity {
public MediaPlayer mediaPlayer = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a = (Button) findViewById(R.id.First);
a.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.stop();
}
else {
mediaPlayer.start();
mediaPlayer = null;
}
}
}
);
Button b = (Button) findViewById(R.id.Second);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.secondsound);
mediaPlayer.stop();
}
else {
mediaPlayer.start();
mediaPlayer = null;
}
}
}
);
I am making a soundboard with over 30 sounds, if that helps at all. What (slight I assume) changes would I need to make to my code? Any help would be greatly appreciated!
public class MainActivity extends Activity {
public MediaPlayer mediaPlayer = null;
public Boolean playing=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a = (Button) findViewById(R.id.First);
a.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
(playing)mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.start();
playing=true;
}
});
Button b = (Button) findViewById(R.id.Second);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
(playing)mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.start();
playing=true;
}
});
}
}