mediaplayer.prepare, How to Avoid Toast delay. "Loading" - java

how to avoid delay toast while mediaplayer.prepare, i want to show the toast when btn onclick.
FloatingActionButton button = findViewById(R.id.playbtnnine);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()){
mediaPlayer.reset();
Toast.makeText(getBaseContext(), "Stop" , Toast.LENGTH_SHORT ).show();
}else {
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setLooping(true);
mediaPlayer.start();
Toast.makeText(getBaseContext(), "Loading" , Toast.LENGTH_SHORT ).show();
}
}
});

You should try this
FloatingActionButton button = findViewById(R.id.playbtnnine);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Loading" , Toast.LENGTH_SHORT ).show();
if (mediaPlayer.isPlaying()){
mediaPlayer.reset();
Toast.makeText(getBaseContext(), "Stop" , Toast.LENGTH_SHORT ).show();
}else {
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
}
});

Related

I Dont know how can I get bookmark button in fire base

iam already did add bookmark in the firebase but i dont know how can i get it
reference = FirebaseDatabase.getInstance().getReference("pagenumber");
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pagenumber = pdfView.getCurrentPage();
reference.setValue(pagenumber);
if (true) {
Toast.makeText(getApplicationContext(), "تم حفظ العلامة", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "لم يتم حفظ العلامة", Toast.LENGTH_SHORT).show();
}
}
});
load.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}

Android MediaPlayer: How to stop if another audio file is being loaded?

I have a problem with the Android MediaPlayer. As soon as a new sound file is clicked by user input, the old one does not stop, so all audio files overlap.
I already tried common methods like MediaPlayer.stop(); and
MediaPlayer.release();
These methods always result in an app crash, although they are called in the correct order
public void Abspielen(String Datei) {
try {
AndreasPlayer = MediaPlayer.create(MainActivity.this, getResources().getIdentifier(Datei, "raw", getPackageName()));
AndreasPlayer.start();
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Abspielen("filename");
}
I worked out the following solution:
public void Abspielen(String Datei) {
try {
stopKnopf = findViewById(R.id.fab2);
stopKnopf.show();
try {
if (AndreasPlayer.isPlaying()) {
AndreasPlayer.reset();
}
} catch (Exception ex) {
}
AndreasPlayer = MediaPlayer.create(MainActivity.this, getResources().getIdentifier(Datei, "raw", getPackageName()));
AndreasPlayer.start();
stopKnopf.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AndreasPlayer.reset();
stopKnopf.hide();
}
});
AndreasPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
AndreasPlayer.reset();
stopKnopf.hide();
}
});
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}

Creating an listview which displays records

outputfile= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording.3gp";;
audiorecorder = new MediaRecorder();
audiorecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
audiorecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
audiorecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
audiorecorder.setOutputFile(outputfile);
record.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try{
audiorecorder.prepare();
audiorecorder.start();
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
record.setEnabled(true);
stop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_SHORT);
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
audiorecorder.stop();
audiorecorder.release();
audiorecorder =null;
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(getApplicationContext(), "Record successfully recorded", Toast.LENGTH_SHORT);
}
});
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer m = new MediaPlayer();
try{
m.setDataSource(outputfile);
}
catch ( IOException e)
{
e.printStackTrace();
}
try{
m.prepare();
}
catch(IOException e)
{
e.printStackTrace();
}
m.start();
Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_SHORT);
}
});
This is the record ive created .I created another intent which contains a listview which i want to display in it the records SAVED.
any idea how to save the records? and then how to display them in the listview?

Stop played sound in android

I used this code for play a sound by click on a button.
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying())
{
mp.stop();
}
try {
mp.reset();
AssetFileDescriptor afd;
afd = getAssets().openFd("sound/07.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
it's working well but i can't stop sound!
I use mp.stop(); .but click on button plays sound again!
when your onClickListener runs, the if statement checks after that other codes run. try to put an else statement after if you code should be like this :
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying())
{
mp.stop();
}else{
try {
mp.reset();
AssetFileDescriptor afd;
afd = getAssets().openFd("sound/07.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});

How to make a button play a sound as it launches another activity when pressed?

Current code:
MediaPlayer mp;
button1=(ImageButton)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), Activity1.class);
startActivity(i);
}
});
So how would a button start a new activity and play a sound while doing so?
What I've tried: various methods like playSound( ); in the method.
It only plays the default android sound. I want a specific sound stored in the raw directory. So when the button get's pressed it launches both the intent to launch the activity as well as the specific sound.
Error:
When I try to put MediaPlayer mp; above the button, it states variable mp is already defined. I just need someone to append the activity launch code so that it will play the sound as well.
First you need to put a sound.mp3 in raw folder
MediaPlayer mp;
button1=(ImageButton)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mp = MediaPlayer.create(Music.this, R.raw.sound);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mp.start();
Intent i = new Intent(getApplicationContext(), Activity1.class);
startActivity(i);
}
});
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), Uri.parse(""));
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer player) {
// TODO Auto-generated method stub
player.start();
Intent i = new Intent(getApplicationContext(), Activity1.class);
startActivity(i);
}
});
Don't forget to release it : http://developer.android.com/reference/android/media/MediaPlayer.html#release%28%29
Mediaplayer mediaPlayer = MediaPlayer.create(this, R.raw.YOURMP3NAMEFILE);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if(mp != null){
mp.release();
mediaPlayer = null;
Log.d(TAG, "release mediaplayer");
}
}
});
mediaPlayer.start();
launchSecondActivity();

Categories

Resources