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();
}
}
Related
Being a novice coder, trying to code an app that plays a sound on buttonclick in a fragment; successfully did it but I want to add more small pieces of sounds to play one after another. How can I add those sounds(sound2, sound3, sound4, etc) after that 'R.raw.sound1'? Please guide..
buttonPlayAudioVar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp != null) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}
mp = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.sound1);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;
}
});
}
});
//you can add all the songs in a array
ArrayList<String> songsArray = new ArrayList<>();
int position = 0;
buttonPlayAudioVar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer(songsArray, position);
}
});
private void mediaPlayer(ArrayList<String> songsArray) {
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
}
}
if (position > songsArray.size() - 1) {
return;
}
try {
mp.setDataSource(songsArray.get(position));
position++;
mp.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;
mediaPlayer(songsArray, position);
}
});
}
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.
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();
I am having a problem:
What I am trying to do is to finish an activity once the audio has been played. I use the the following code:
public void addButtonListener() {
mPlay = (ImageButton) findViewById(R.id.bPlay);
mPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Punto.this, "ImageButton is working!", Toast.LENGTH_SHORT).show();
/* mPlayer = MediaPlayer.create(Punto.this,
Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/omw/zipSample/1.ogg"));
mPlayer.start();*/
String A = final_route + uriAudio;
mPlayer = MediaPlayer.create(Punto.this, Uri.parse(final_route + uriAudio));
mPlayer.start();
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
finish();
}
});
}
});
}
If I just remove the 'setOnCompletionListener', then it works (without having finished the activity). What am I doing wrong?
Thanks.
use this following line instead of your line " mp.release(); "
public void addButtonListener() {
mPlay = (ImageButton) findViewById(R.id.bPlay);
mPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Punto.this, "ImageButton is working!", Toast.LENGTH_SHORT).show();
/* mPlayer = MediaPlayer.create(Punto.this,
Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/omw/zipSample/1.ogg"));
mPlayer.start();*/
String A = final_route + uriAudio;
mPlayer = MediaPlayer.create(Punto.this, Uri.parse(final_route + uriAudio));
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if(mPlayer!=null){
mPlayer.release();
mPlayer=null;
finish();
}
}
});
mPlayer.start();
} //
});
}
This is a code for play and stop mediaplayer
It made music successfully and played that.But stop button didn't work
Why it's not working?
Please lead me to the correct code
Thanks
mp = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Titraj/"+name+".mp3"));
play.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(mp.isPlaying()){
if(mp!=null){
mp.pause();
play.setImageResource(R.drawable.play);
}
}else{
if(mp!=null){
mp.start();
play.setImageResource(R.drawable.pa);
}
}
}
});
stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(mp.isPlaying()){
if(mp!=null){
mp.stop();
}
}
}
});
mp.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer arg0) {
play.setImageResource(R.drawable.play);
}
});
Use this one....
stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
});
Also must check you haven't set android:focusable = false for your button in xml file.
Try this in your on stop method
mp.stop();
mp.release();
mp= null;
mp = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Titraj/"+name+".mp3"));
play.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(mp.isPlaying()){
if(mp!=null){
mp.pause();
play.setImageResource(R.drawable.play);
}
}else{
if(mp!=null){
mp.start();
play.setImageResource(R.drawable.pa);
}
}
}
});
stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(mp.isPlaying()){
mp.stop();
}
}
});
mp.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer arg0) {
play.setImageResource(R.drawable.play);
}
});
Check this code i hop it working....