I have created an array of sounds that is randomly accessed to play via MediaPlayer when a button is pressed - this works fine my only issue is that when testing it and pressing the button rapidly the sound would stop and not play anymore - is there some way I can implement a start stop thing? I tried beginning my onClick method with a butPress.stop() but my android emulator didn't like it. the code follows :
public void onClick(View arg0) {
try{
display.setText(pp.getText());
int x = r.nextInt(sfx.length);
butPress = MediaPlayer.create(PitchMaker.this, sfx[x]);
butPress.start();
}catch(Exception e){
display.setText("Whoops we had a problem. Please try again");
}
}
Please use this line of code in the catch block so that we can get the exact error report.
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
Related
i am searched many solution but could not find proper result when i press the back button of mobile i am using one counter variable and put the condition like when counter is 0 it called the music file.
but when i am comeout from that activity means press back button of mobile still that activity is runnig and called the music file .i am using this below code for music play.please anyone have solution then help me.i am using one timer function.like this when i==0 that time call one music file but when i press back button. this timer i wants to stop.
public void btntimer(){
new Handler().postDelayed((new Runnable() {
#Override
public void run() {
if(i!=32) {
matchNo();
}
else {
try{
showdialog();
media=1;
}catch (Exception e){
media=0;
}
updatepoints();
}
}
}), 4000);
}
song=MediaPlayer.create(this,R.raw.cartoon1);
song.start();
song.setLooping(false);
If I understand you correct, you want to stop playing music on back button pressed.
Call stop() in onBackPressed method.
#Override
public void onBackPressed() {
song.stop();
song.release();
song = null;
}
I am trying to play a song that is in remote server and is in this link. You also can check the song. but as per what i have coded the song is not getting played from the remote server.
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try{
mySong = new MediaPlayer();
mySong.setDataSource("http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3");
mySong.setAudioStreamType(AudioManager.STREAM_MUSIC);
mySong.prepareAsync();
mySong.start();
}
catch(Exception ee){
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(ee.getMessage());
}
finally{
mySong.reset();
mySong.release();
}
}
});
are you serious?
You are starting it just to reset and release it instantly?find the wrong logic!
Or do you think the finally statement will be executed after the song is played through?
You call prepareAsync() in your code. Because you are preparing asynchronously, you're going to receive a callback called onPrepared(MediaPlayer) after you've declared that your Activity implements MediaPlayer.OnPreparedListener. It is here that you should be calling mySong.start(). Calling it right after prepareAsync would most likely cause an IllegalStateException to occur because the MediaPlayer isn't prepared yet. Finally, you'll want to set a MediaPlayer.OnCompletionListener so you can release the MediaPlayer there instead of the finally block. Also, resetting the MediaPlayer and releasing is redundant. If you're going to release it right away, there's no reason to reset it.
I am not really sure if the error is because of I have an alarm or while loop:
MediaPlayer mpChange;
MediaPlayer saved;
mpChange = MediaPlayer.create(this, R.raw.change);
saved = MediaPlayer.create(this, R.raw.saved);
When I add a user I call a function and inside I informed the user message has saved by:
saved.start();
After that I check if user has special meaning or not and again inform the user if it does not:
else
{
/*Implement Audio Change*/
while(saved.isPlaying() == true)
{
//Nothing except wait
System.out.println("looop");
}
//saved.stop();
System.out.println("finished");
mpChange.start();
}
Code is working fine and I don't get any error or conflict between the voices. However, I get an error with in my application which says:
Application is not responding. Would you like to close?
I was not sure if it is the while loop or MediaPlayer.
Last output was:
looop
looop
looop
looop
finished
Thanks in advance for any help.
saved.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mpChange.start();
}
});
try this code in else part remove while loop
I have created a simple Ukulele tuner (The market I think lacks a visually pleasing and extremely simple tuner).
Any how, firstly, through the developer console I can see that there is a Null Pointer Exception at the Button Onclick Events. I have not been able to recreate this, however it has been reported four times.
Secondly, looking at the log while using the app I can see this warning;
E/MP3Extractor(68): Unable to resync. Signalling end of stream.
This entry here MediaPlayer array causing null pointer in Android seems to be along the same lines.
How it works.
Through the use of radio buttons the user selects either to play a single note or a continuous note. I have created a subroutine called StopMediaPlayer that stops, resets and instantiates the MediaPlayers again. This was used because I could never seem to stop the continuous play back but only pause it.
Is the warning and the NullPointerException related? Is there a more efficient/better means of stopping MediaPlayer that will mean that I wont have to re instantiate the notes every time.
Thank You
One of the offending Onclicks
Button gButton = (Button) findViewById(R.id.gButton);
gButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (singleRadio.isChecked() == true)
{
StopMediaPLayer();
gNote.setLooping(false);
gNote.start();
}
else if (contRadio.isChecked() == true)
{
StopMediaPLayer();
gNote.setLooping(true);
gNote.start();
}
}
});
Stop Media Player Subroutine
public void StopMediaPLayer()
{
Log.i("UkuleleTuner", "Stop Media Player");
gNote.setLooping(false);
cNote.setLooping(false);
eNote.setLooping(false);
aNote.setLooping(false);
gNote.stop();
cNote.stop();
eNote.stop();
aNote.stop();
gNote.reset();
cNote.reset();
eNote.reset();
aNote.reset();
gNote = MediaPlayer.create(this, R.raw.g_note);
cNote = MediaPlayer.create(this, R.raw.c_note);
eNote = MediaPlayer.create(this, R.raw.e_note);
aNote = MediaPlayer.create(this, R.raw.a_note);
}
I'm developing a new app and am using VideoView to display mpeg clips.
I need to switch between videos very quickly, however, loading a new clip to a VideoView seems to take about half a second of black screen.
The transition must be seamless, how would you go about solving this kind of problem?
I had a similar issue and resolved with a still-image (ImageView) transition:
build a FrameLayout with an ImageView over the VideoView
the ImageView shows the first frame of the video
initially the ImageView is visible
start the video, wait for an arbitrary time (e.g. 2-300ms)
hide the ImageView
to switch two videos:
- show the image
- switch the video
- hide the image
a bit hackish but worked for me
I faced the same problem, but I used mediaplayer, my code is:
Here I use a button to trigger the switch action, before switching, I have already loaded the video and prepare for switching. When you need to do so, you just need to stop and release the old mediaplayer and starting the new one.The key point for seamless switching is the sleep(), when the process is sleeping, actually the video is still going on, but give the system some time to prepare for the next one.
switch_button = (Button) findViewById(R.id.button_switch);
switch_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button switch_button = (Button) v;
VideoPlayer2 = new MediaPlayer();
try {
VideoPlayer2.setDataSource("rtsp://" + hostIP + ":1935/vod/Timer.mp4");
VideoPlayer2.prepare();
VideoPlayer2.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
VideoPlayer.release();
VideoPlayer2.setDisplay(vidHolder);
VideoPlayer2.start();
}
});