How to set Looping conditions for VideoView in Android - android

I want to set looping conditions and for the VideoView. This is what I am trying to achieve.
Video Starts and finishes if Edit Text is not selected.
If Video Start and EditText is selected then Video is set to looping.
If the user types into the EditText and presses button submit then looping set to false and the activity closes after video completes.
If EditText loses selection the video activity finishes on complete.
Here is the code but it's not working for me
mVideoView.setVideoPath(phone);
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
#Override
public void onPrepared(final MediaPlayer mp) {
mVideoView.start();
if (mVideoView.isPlaying()) {
mp.setLooping(false);
}
}
}
);
CommentBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.setLooping(true);
}
});
}
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp1) {
finish();
}
}
);

Problem is you have not called mediaplayer.start() after you set loop in onCompletionListener().
I executed the below code and it works fine here
Solution:
Create public bundle type of variable
b=new Bundle();
b.putBoolean("repeat", false);
v.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.ak));
v.requestFocus();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (txt.getText().toString().length() > 0) {
b.putBoolean("repeat", false);
}
}
});
txt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean has) {
if (has) {
b.putBoolean("repeat", true);
}
}
});
v.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (b.getBoolean("repeat") == true) {
mediaPlayer.setLooping(true);
mediaPlayer.start();
Toast.makeText(getApplicationContext(),String.valueOf(b.getBoolean("repeat")),Toast.LENGTH_LONG).show();
} else {
mediaPlayer.setLooping(false);
mediaPlayer.stop();
Toast.makeText(getApplicationContext(),String.valueOf(b.getBoolean("repeat")),Toast.LENGTH_LONG).show();
}
}
});
v.start();

Related

How to play only one sound at a time?

I have two ImageViews with different sounds which plays sound when clicked on one of them. When click on the first ImageView I want the sound to play and before the first ImageView finish playing the sound, if the second ImageView is click, I want to restrict the second ImageView to play a sound because the first ImageView is still playing the sound. In fact, I want to restrict a sound to play when another sound is playing. But, I didn't get the desired result.
public class Transportation extends AppCompatActivity {
private MediaPlayer sound,sound1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transportation);
sound = MediaPlayer.create(this,R.raw.vatunabus);
final ImageView imageView= (ImageView)findViewById(R.id.vatunabussound);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound1.isPlaying()){
sound.stop();
sound.release();
}
else if(sound.isPlaying()){
sound.pause();
imageView.setBackgroundResource(R.mipmap.playicon);
}
else {
sound.start();
imageView.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
imageView.setBackgroundResource(R.mipmap.playicon);
}
});
sound1 = MediaPlayer.create(this,R.raw.varowlsanabus);
final ImageView imageView1 = (ImageView)findViewById(R.id.firstbussound);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound.isPlaying()){
sound1.stop();
sound1.release();
}
else if(sound1.isPlaying()){
sound1.pause();
imageView1.setBackgroundResource(R.mipmap.playicon);
}
else {
sound1.start();
imageView1.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
imageView1.setBackgroundResource(R.mipmap.playicon);
}
});
}
}
Here's an idea, let's create a boolean value to tell us if a sound is currently playing.
private MediaPlayer sound,sound1;
private boolean soundPlaying = false; //new boolean variable
Next, before playing a sound, we'll check to make sure it's still false:
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
//Add this check in both onClick methods
if(soundPlaying){
return;
}
...
}
Also, after sound.start() and sound1.start(), let's set the boolean to true:
soundPlaying = true;
Finally, in both instances of the onCompletion methods, let's reset the boolean to false:
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
...
}
The final code will look like this:
public class Transportation extends AppCompatActivity {
private MediaPlayer sound,sound1;
private boolean soundPlaying = false; //new boolean variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transportation);
sound = MediaPlayer.create(this,R.raw.vatunabus);
final ImageView imageView= (ImageView)findViewById(R.id.vatunabussound);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
if(soundPlaying){
return;
}
if(sound1.isPlaying()){
sound.stop();
sound.release();
}
else if(sound.isPlaying()){
sound.pause();
imageView.setBackgroundResource(R.mipmap.playicon);
}
else {
sound.start();
imageView.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
imageView.setBackgroundResource(R.mipmap.playicon);
}
});
sound1 = MediaPlayer.create(this,R.raw.varowlsanabus);
final ImageView imageView1 = (ImageView)findViewById(R.id.firstbussound);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
if(soundPlaying){
return;
}
if(sound.isPlaying()){
sound1.stop();
sound1.release();
}
else if(sound1.isPlaying()){
sound1.pause();
imageView1.setBackgroundResource(R.mipmap.playicon);
}
else {
sound1.start();
imageView1.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
imageView1.setBackgroundResource(R.mipmap.playicon);
}
});
}
}
I hope this helps. Couldn't try it in live code though, so let me know how it goes.

how to use media player in dialog alert button

I try to play media player in dialog alert button, somehow it doesn't work but there is no error notification, the dialog works perfectly, and the cancel button the, but for the media player doesn't work
public void TampilanDoa() {
tampilan = new Dialog(DoaHarianActivity.this);
tampilan.setContentView(R.layout.layout_doa);
dengar = tampilan.findViewById(R.id.btdengar);
kembali = tampilan.findViewById(R.id.btkembali);
final MediaPlayer
mp=MediaPlayer.create(this, R.raw.splash);
dengar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
});
kembali.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tampilan.cancel();
}
});
tampilan.show();
}
}

repeat audio in mediaplayer

for repeat audio in mediaplayer write this code but not work correct :
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
G.mediaPlayer.seekTo(i);
}
if (i >= G.mediaPlayer.getDuration()) {
if (isRepeat) {
G.mediaPlayer.seekTo(0);
G.mediaPlayer.start();
}else {
G.mediaPlayer.seekTo(0);
G.mediaPlayer.pause();
}
}
}
and for play audio do this :
public void playSongWithIndex(final int songIndex) {
progressBar.setVisibility(View.VISIBLE);
if (music_exist.exists()) {
playMusicOfflineMode();
} else {
try {
G.mediaPlayer.reset();
G.mediaPlayer.setDataSource(songList.get(songIndex).getFile_128());
G.mediaPlayer.prepareAsync();
G.mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
runOnUiThread(new Runnable() {
#Override
public void run() {
}
});
G.mediaPlayer.start();
seekBar.setMax(G.mediaPlayer.getDuration());
progressBar.setVisibility(View.INVISIBLE);
timer = new Timer();
timer.schedule(new MainTimer(), 0, 1000);
}
});
} catch (IllegalArgumentException | IllegalStateException | IOException e) {
e.printStackTrace();
}
}
}
but repeat audio some times work done and sometimes when seekbar well be over stop and do not work .
please help me how to repeat audio ?
You can set Media player looping to true,use this code:
G.mediaPlayer.setLooping(true);
And if you want to know when the player has finished you should use OnCompletionListener like this:
G.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
}
});
ok for help other developer write my correct answer :
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (isRepeat) {
G.mediaPlayer.start();
} else {
G.mediaPlayer.seekTo(0);
seekBar.setProgress(0);
current_duration_text.setText("" + utils.milliSecondsToTimer(0));
play_music_img.setImageResource(R.drawable.ic_play_arrow_black_24dp);
}
}
I hope to be useful
i am not sure but i think you can use loop ...for example
mMediaPlayer.setLooping(true);

OnCompletionListener not called once media player has been reset and initialised

In my code, I have a start/stop button.I am trying to play an audio clip from the raw folder. On completion of an audio clip, the start/stop button has to change from stop mode to start mode. I don't want to loop the audio. For this,I have called OnCompletionListener. However, the listener is called only once. If the media player is reset and created again, OnCompletionListener is no longer called.
public void playsong(View view) {
if (mySound != null)
{
if (!pause) {mySound.start();
btnStartStop.setBackgroundResource(R.drawable.button);
pause = true;
} else {
if (mySound.isPlaying()) {
mySound.stop();
do_it();
}
}
}
}
mySound.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mySound) {
do_it();
}
});
public void do_it() {
mySound.reset();
mySound = MediaPlayer.create(this, R.raw.a);
pause = false;
btnStartStop.setBackgroundResource(R.drawable.button2);
}
Try this code ?
In this example oncompletionlistner is called everytime
btnDemo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(objPlayer!=null){
if(objPlayer.isPlaying())
stopPlaying(objPlayer);
}
objPlayer = MediaPlayer.create(getApplicationContext(),R.raw.demoaudio);
if(objPlayer.isPlaying())
objPlayer.pause();
else
objPlayer.start();
Log.d("null", "Media Player started!");
if(objPlayer.isLooping() != true){
Log.d("null", "Problem in Playing Audio");
}
objPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
objPlayer.release();
objPlayer = null;
};
});
}
});

How to play and pause in only one button - Android

I have two buttons in my media player that streams a radio station, play and pause. I want to make it only one button that has two function. First click I want to play it and second click I want to pause it. And when I click it again I want to play it. I need help. Here is my code.
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
play.performClick();
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
}
private void play() {
Uri myUri = Uri.parse("Shoutcast URL");
try {
if (mp == null) {
this.mp = new MediaPlayer();
} else {
mp.stop();
mp.reset();
}
mp.setDataSource(this, myUri); // Go to Initialized state
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnErrorListener(this);
mp.prepareAsync();
Log.d(TAG, "LoadClip Done");
} catch (Throwable t) {
Log.d(TAG, t.toString());
}
}
#Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Stream is prepared");
mp.start();
}
private void pause() {
mp.pause();
}
#Override
public void onDestroy() {
super.onDestroy();
stop();
}
public void onCompletion(MediaPlayer mp) {
stop();
}
Create just one Button and add something like a buttonstatus. Then you can check the status in your listener.
For example:
boolean isPlaying = false;
playPause = (Button) findViewById(R.id.play);
playPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (isPlaying) {
pause();
}else{
play();
}
isPlaying = !isPlaying;
}
});
Boolean b = false;
Button play = (Button) findViewById(R.id.play);
play..setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if(b == false)
{
//write play code here
b= true;
}
else
{
// enter pause code here
}
}
}
You can use Toggle button:
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (toggleButton.isChecked()) {
play();
} else {
pause();
}
}
});
You can also remove the green bar from toggle button.
Toggle button Mkyong example
Android dev tutorial
You don't need to declare any boolean variable to check player state. There is a method already available in mediaplayer class named - isPlaying().
Try my code sequence for playpause using single button.
public void playpause(){
if(!mp.isPlaying()){ // here mp is object of MediaPlayer class
mp.start();
//showNotification("Player State Plying");
}
else if(mp.isPlaying()){
mp.pause();
//showNotification("Player State Pause");
}
}

Categories

Resources