Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在每次单击时更改按钮的背景_Java_Android_Button_Media Player - Fatal编程技术网

Java 如何在每次单击时更改按钮的背景

Java 如何在每次单击时更改按钮的背景,java,android,button,media-player,Java,Android,Button,Media Player,我正在开发音乐应用程序 所以我试着做一个按钮,当我点击它的歌曲 重复和背景改变 我有两个背景其中一个我想让它成为按钮的背景 什么时候播放一次这首歌 当这首歌重复的时候,另一首 package khaledbanafa.shaylat; import android.media.MediaPlayer; import android.os.Build; import android.support.annotation.RequiresApi; import

我正在开发音乐应用程序

所以我试着做一个按钮,当我点击它的歌曲

重复和背景改变

我有两个背景其中一个我想让它成为按钮的背景

什么时候播放一次这首歌

当这首歌重复的时候,另一首

    package khaledbanafa.shaylat;
    import android.media.MediaPlayer;
    import android.os.Build;
    import android.support.annotation.RequiresApi;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    import android.widget.SeekBar;
    import android.widget.TextView;
    import android.widget.ToggleButton;
    import java.io.IOException;
    import static android.app.PendingIntent.getActivity;
    import static khaledbanafa.shaylat.R.drawable.play;
    public class ha extends AppCompatActivity {
    MediaPlayer x;
    RelativeLayout mylayout;
    TextView textView;
    TextView textView2;
    TextView textView3;
    SeekBar seekBar;
    boolean pressed = false;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_on);
        final Button a = (Button) findViewById(R.id.button1);
        a.setText("ارفع الراس يا حربي");
        final Button b = (Button) findViewById(R.id.button2);
        b.setText("سما سما");
        final Button c = (Button) findViewById(R.id.button3);
        c.setText("الحربي للحربي فدا");
        final Button d = (Button) findViewById(R.id.button4);
        d.setText("العبو ياحرب دام العمر فاني");
        final Button e = (Button) findViewById(R.id.button5);
        e.setText("حرب الحرايب");
        final Button f = (Button) findViewById(R.id.button6);
        f.setText("حرب الحرايب لحن دحة");
        final Button g = (Button) findViewById(R.id.button7);
        g.setText("حرب الدول");
        final Button h = (Button) findViewById(R.id.button8);
        h.setText("حرب ماتنقص تزود");
        final Button i = (Button) findViewById(R.id.button9);
        i.setText("حنا اهل الفخر");
        final Button j = (Button) findViewById(R.id.button10);
        j.setText("ياحرب شوشو والعبو");
        Button play = (Button) findViewById(R.id.buttonplay);
        Button pause = (Button) findViewById(R.id.buttonpause);
        Button stop = (Button) findViewById(R.id.buttonstop);
        final Button repeat = (Button)findViewById(R.id.repeat);
        Button nextmedia = (Button)findViewById(R.id.nextmedia);
        textView = (TextView)findViewById(R.id.textView);
        textView2 = (TextView)findViewById(R.id.textView2);
        textView3 = (TextView)findViewById(R.id.textView3);
        seekBar = (SeekBar)findViewById(R.id.seekBar);



        final SeekBar SEK = new SeekBar(ha.this);
        SEK.setPadding(150,0,140,0);
        SEK.setX(0);
        SEK.setY(1659);
        SEK.setProgressDrawable(getDrawable(R.drawable.red_scrubber_progress));
        SEK.setThumb(getDrawable(R.drawable.red_scrubber_control));
        mylayout = (RelativeLayout)findViewById(R.id.onrel);
        SEK.setLayoutParams(new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(x != null)
                    x.start();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(x != null)
                x.stop();
                textView.setText("");
                mylayout.removeView(SEK);
                mylayout.addView(SEK);
                SEK.setProgress(0);
                SEK.setMax(0);

            }
        });
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(x != null)
                x.pause();
            }
        });

        a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylayout.removeView(seekBar);
                mylayout.removeView(SEK);
                stopPlaying();
                x = MediaPlayer.create(ha.this, R.raw.haone);
                class mythread extends Thread {
                    public void run() {
                        while (x != null) {
                            try {
                                Thread.sleep(1000);
                            } catch (Exception e) {
                            }
                            SEK.post(new Runnable() {
                                @Override
                                public void run() {
                                    SEK.setProgress(x.getCurrentPosition());
                                    int tim = (SEK.getMax() / 1000);
                                    int m = tim / 60;
                                    int s = tim % 60;
                                    /////////
                                    int tim0 = (SEK.getProgress() / 1000);
                                    int m0 = tim0 / 60;
                                    int s0 = tim0 % 60;
                                    textView2.setText(m0 + ":" + s0);
                                    textView3.setText(m + ":" + s);
                                }
                            });
                        }
                    }
                }
                SEK.setMax(x.getDuration());
                mythread my = new mythread();
                textView.setText(a.getText());
                mylayout.addView(SEK);
                my.start();
                x.start();

                        SEK.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    int progresval;
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        progresval = progress;}
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {}
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        x.seekTo(progresval);
                    }
                });}
        });

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylayout.removeView(seekBar);mylayout.removeView(SEK);stopPlaying();
                x = MediaPlayer.create(ha.this, R.raw.hatwo);class mythread extends Thread {
                    public void run() {while (x != null) {try {Thread.sleep(1000);} catch (Exception e) {}
                        SEK.post(new Runnable() {
                            @Override
                            public void run() {
                                SEK.setProgress(x.getCurrentPosition());
                                int tim = (SEK.getMax()/1000);
                                int m = tim/60;
                                int s= tim %60;
                                /////////
                                int tim0 = (SEK.getProgress()/1000);
                                int m0 = tim0/60;
                                int s0 = tim0%60;
                                textView2.setText(m0+":"+s0);
                                textView3.setText(m+":"+s);}});}}}
                SEK.setMax(x.getDuration());mythread my = new mythread();
                textView.setText(b.getText());
                mylayout.addView(SEK);
                my.start();x.start();
                SEK.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    int progresval;
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        progresval = progress;}
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {}
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        x.seekTo(progresval);
                    }
                });
            }
        });

        c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylayout.removeView(seekBar);mylayout.removeView(SEK);stopPlaying();
                x = MediaPlayer.create(ha.this, R.raw.hathree);class mythread extends Thread {
                    public void run() {while (x != null) {try {Thread.sleep(1000);} catch (Exception e) {}
                        SEK.post(new Runnable() {
                            @Override
                            public void run() {
                                SEK.setProgress(x.getCurrentPosition());
                                int tim = (SEK.getMax()/1000);
                                int m = tim/60;
                                int s= tim %60;
                                /////////
                                int tim0 = (SEK.getProgress()/1000);
                                int m0 = tim0/60;
                                int s0 = tim0%60;
                                textView2.setText(m0+":"+s0);
                                textView3.setText(m+":"+s);}});}}}
                SEK.setMax(x.getDuration());mythread my = new mythread();
                textView.setText(c.getText());
                mylayout.addView(SEK);
                my.start();x.start();
                SEK.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    int progresval;
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        progresval = progress;}
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {}
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        x.seekTo(progresval);
                    }
                });


            }
        });
        d.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylayout.removeView(seekBar);mylayout.removeView(SEK);stopPlaying();
                x = MediaPlayer.create(ha.this, R.raw.hafour);class mythread extends Thread {
                    public void run() {while (x != null) {try {Thread.sleep(1000);} catch (Exception e) {}
                        SEK.post(new Runnable() {
                            @Override
                            public void run() {
                                SEK.setProgress(x.getCurrentPosition());
                                int tim = (SEK.getMax()/1000);
                                int m = tim/60;
                                int s= tim %60;
                                /////////
                                int tim0 = (SEK.getProgress()/1000);
                                int m0 = tim0/60;
                                int s0 = tim0%60;
                                textView2.setText(m0+":"+s0);
                                textView3.setText(m+":"+s);}});}}}
                SEK.setMax(x.getDuration());mythread my = new mythread();
                textView.setText(d.getText());
                mylayout.addView(SEK);
                my.start();x.start();
                SEK.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    int progresval;
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        progresval = progress;}
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {}
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        x.seekTo(progresval);
                    }
                });

            }
        });
        e.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylayout.removeView(seekBar);mylayout.removeView(SEK);stopPlaying();
                x = MediaPlayer.create(ha.this, R.raw.hafive);class mythread extends Thread {
                    public void run() {while (x != null) {try {Thread.sleep(1000);} catch (Exception e) {}
                        SEK.post(new Runnable() {
                            @Override
                            public void run() {
                                SEK.setProgress(x.getCurrentPosition());
                                int tim = (SEK.getMax()/1000);
                                int m = tim/60;
                                int s= tim %60;
                                /////////
                                int tim0 = (SEK.getProgress()/1000);
                                int m0 = tim0/60;
                                int s0 = tim0%60;
                                textView2.setText(m0+":"+s0);
                                textView3.setText(m+":"+s);}});}}}
                SEK.setMax(x.getDuration());mythread my = new mythread();
                textView.setText(e.getText());
                mylayout.addView(SEK);
                my.start();x.start();
                SEK.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    int progresval;
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        progresval = progress;}
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {}
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        x.seekTo(progresval);
                    }
                });

            }
        });

        f.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylayout.removeView(seekBar);mylayout.removeView(SEK);stopPlaying();
                x = MediaPlayer.create(ha.this, R.raw.hasix);class mythread extends Thread {
                    public void run() {while (x != null) {try {Thread.sleep(1000);} catch (Exception e) {}
                        SEK.post(new Runnable() {
                            @Override
                            public void run() {
                                SEK.setProgress(x.getCurrentPosition());
                                int tim = (SEK.getMax()/1000);
                                int m = tim/60;
                                int s= tim %60;
                                /////////
                                int tim0 = (SEK.getProgress()/1000);
                                int m0 = tim0/60;
                                int s0 = tim0%60;
                                textView2.setText(m0+":"+s0);
                                textView3.setText(m+":"+s);}});}}}
                SEK.setMax(x.getDuration());mythread my = new mythread();
                textView.setText(f.getText());
                mylayout.addView(SEK);
                my.start();x.start();
                SEK.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    int progresval;
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        progresval = progress;}
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {}
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        x.seekTo(progresval);
                    }
                });

            }
        });
        g.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylayout.removeView(seekBar);mylayout.removeView(SEK);stopPlaying();
                x = MediaPlayer.create(ha.this, R.raw.haseven);class mythread extends Thread {
                    public void run() {while (x != null) {try {Thread.sleep(1000);} catch (Exception e) {}
                        SEK.post(new Runnable() {
                            @Override
                            public void run() {
                                SEK.setProgress(x.getCurrentPosition());
                                int tim = (SEK.getMax()/1000);
                                int m = tim/60;
                                int s= tim %60;
                                /////////
                                int tim0 = (SEK.getProgress()/1000);
                                int m0 = tim0/60;
                                int s0 = tim0%60;
                                textView2.setText(m0+":"+s0);
                                textView3.setText(m+":"+s);}});}}}
                SEK.setMax(x.getDuration());mythread my = new mythread();
                textView.setText(g.getText());
                mylayout.addView(SEK);
                my.start();x.start();
                SEK.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    int progresval;
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        progresval = progress;}
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {}
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        x.seekTo(progresval);
                    }
                });

            }
        });
        h.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylayout.removeView(seekBar);mylayout.removeView(SEK);stopPlaying();
                x = MediaPlayer.create(ha.this, R.raw.haeight);class mythread extends Thread {
                    public void run() {while (x != null) {try {Thread.sleep(1000);} catch (Exception e) {}
                        SEK.post(new Runnable() {
                            @Override
                            public void run() {
                                SEK.setProgress(x.getCurrentPosition());
                                int tim = (SEK.getMax()/1000);
                                int m = tim/60;
                                int s= tim %60;
                                /////////
                                int tim0 = (SEK.getProgress()/1000);
                                int m0 = tim0/60;
                                int s0 = tim0%60;
                                textView2.setText(m0+":"+s0);
                                textView3.setText(m+":"+s);}});}}}
                SEK.setMax(x.getDuration());mythread my = new mythread();
                textView.setText(h.getText());
                mylayout.addView(SEK);
                my.start();x.start();
                SEK.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    int progresval;
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        progresval = progress;}
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {}
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        x.seekTo(progresval);
                    }
                });

            }
        });
        i.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylayout.removeView(seekBar);mylayout.removeView(SEK);stopPlaying();
                x = MediaPlayer.create(ha.this, R.raw.hanine);class mythread extends Thread {
                    public void run() {while (x != null) {try {Thread.sleep(1000);} catch (Exception e) {}
                        SEK.post(new Runnable() {
                            @Override
                            public void run() {
                                SEK.setProgress(x.getCurrentPosition());
                                int tim = (SEK.getMax()/1000);
                                int m = tim/60;
                                int s= tim %60;
                                /////////
                                int tim0 = (SEK.getProgress()/1000);
                                int m0 = tim0/60;
                                int s0 = tim0%60;
                                textView2.setText(m0+":"+s0);
                                textView3.setText(m+":"+s);}});}}}
                SEK.setMax(x.getDuration());mythread my = new mythread();
                textView.setText(i.getText());
                mylayout.addView(SEK);
                my.start();x.start();
                SEK.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    int progresval;
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        progresval = progress;}
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {}
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        x.seekTo(progresval);
                    }
                });

            }
        });
        j.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylayout.removeView(seekBar);mylayout.removeView(SEK);stopPlaying();
                x = MediaPlayer.create(ha.this, R.raw.haten);class mythread extends Thread {
                    public void run() {while (x != null) {try {Thread.sleep(1000);} catch (Exception e) {}
                        SEK.post(new Runnable() {
                            @Override
                            public void run() {
                                SEK.setProgress(x.getCurrentPosition());
                                int tim = (SEK.getMax()/1000);
                                int m = tim/60;
                                int s= tim %60;
                                /////////
                                int tim0 = (SEK.getProgress()/1000);
                                int m0 = tim0/60;
                                int s0 = tim0%60;
                                textView2.setText(m0+":"+s0);
                                textView3.setText(m+":"+s);}});}}}
                SEK.setMax(x.getDuration());mythread my = new mythread();
                textView.setText(j.getText());
                mylayout.addView(SEK);
                my.start();x.start();
                SEK.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    int progresval;
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        progresval = progress;}
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {}
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        x.seekTo(progresval);
                    }
                });

            }
        });
    }

    public void onDestroy(){
        super.onDestroy();
        if(x != null)
        x.stop();

    }

    private void stopPlaying() {
        if (x != null) {
            x.stop();
            x.release();
            x = null;

        }
    }


}

您可以从mediaPlayer获取重复状态

repeat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(x != null ){
                if ( x.isLooping()) {
                    //player is in repeat mode , so turn it off and change repeat buttun background to normal
                    repeat.setBackgroundResource(R.drawable.repeat_off);
                    x.setLooping(false);
                }else {
                    //player isn't in repeat mode , so turn it on and change repeat buttun background to repeating image
                    repeat.setBackgroundResource(R.drawable.repeat_on);
                    x.setLooping(true);
                }
            }else {
                //player is null
            }
        }
    });

请注意,
setLooping
只需为您的媒体播放器设置重复模式,这样,如果您的x(媒体播放器)未播放,您也可以根据您的播放器状态设置
x.start()
x.resume()

你可以在这篇文章中找到答案:

在处理button时,请查看以下有趣的链接:


以便检测背景何时变化。我建议您实现一个扩展MediaPlayer的类并使用它。为了观察背景的变化,你应该在你的活动中实现一个公共界面

但是,当背景发生变化时,如何使歌曲重复?