制作不同的按钮

时间:2013-12-06 22:23:10

标签: android media-player playback

假设我有3个按钮来播放声音,我希望如果按下按钮只播放声音,我就用这个:

MediaPlayer mpButton1,mpButton2,mpButton3;

Button b1 = (Button)findViewById(R.id.b1);
b1.setOnClickListener(new Button.OnClickListener() { 

       public void onClick(View v){



       if( mpButton1 != null &&  mpButton1.isPlaying()){
           mpButton1.stop();
        }
       if( mpButton2 != null &&  mpButton2.isPlaying()){
           mpButton2.stop();
        }
       if( mpButton3 != null &&  mpButton3.isPlaying()){
           mpButton3.stop();
        }









       mpButton1 = MediaPlayer.create(getBaseContext(),R.raw.b1);
         mpButton1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mpButton1) {
                // TODO Auto-generated method stub

                mpButton1.start();

                mpButton1.setOnCompletionListener(new OnCompletionListener() {
                    public void onCompletion(MediaPlayer mpButton1) {
                        mpButton1.release();

                    };
                });
                    }
                });

    }});

Button b2 = (Button)findViewById(R.id.b2);
b2.setOnClickListener(new Button.OnClickListener() { 

               public void onClick(View v){

                   if( mpButton1 != null &&  mpButton1.isPlaying()){
                       mpButton1.stop();
                    }
                   if( mpButton2 != null &&  mpButton2.isPlaying()){
                       mpButton2.stop();
                    }
                   if( mpButton3 != null &&  mpButton3.isPlaying()){
                       mpButton3.stop();
                    }

                   mpButton2 = MediaPlayer.create(getBaseContext(),R.raw.b2);
                     mpButton2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mpButton2) {
                            // TODO Auto-generated method stub

                            mpButton2.start();

                            mpButton2.setOnCompletionListener(new OnCompletionListener() {
                                public void onCompletion(MediaPlayer mpButton2) {
                                    mpButton2.release();

                                };
                            });
                                }
                            });

               }});

//其他2个按钮与其他声音相同 我已经尝试了各种方法来做到这一点,例如,通过删除声音的释放,但是当你调用上面的按钮时,所有在logcat中给我一个问题,你开始有一些想法这样做?提前谢谢

logcat的:

12-06 18:18:05.005: E/AndroidRuntime(27089): FATAL EXCEPTION: main
12-06 18:18:05.005: E/AndroidRuntime(27089): java.lang.IllegalStateException
12-06 18:18:05.005: E/AndroidRuntime(27089):    at android.media.MediaPlayer.isPlaying(Native Method)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at org.bizzsound1.menu$3.onClick(menu.java:235)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at android.view.View.performClick(View.java:4211)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at android.view.View$PerformClick.run(View.java:17267)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at android.os.Handler.handleCallback(Handler.java:615)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at android.os.Looper.loop(Looper.java:137)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at android.app.ActivityThread.main(ActivityThread.java:4898)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at java.lang.reflect.Method.invokeNative(Native Method)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at java.lang.reflect.Method.invoke(Method.java:511)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-06 18:18:05.005: E/AndroidRuntime(27089):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

在我看来,你只是告诉mpButton1在你检查它是否正在播放之后开始播放。

它正在崩溃,因为你一旦完成播放就会释放媒体播放器。这将它们设置为null。这意味着当你第二次调用它们时,它们会返回null,因此应用程序崩溃了。删除释放方法,执行以下操作:

移动:

    mpButton1 = MediaPlayer.create(getBaseContext(),R.raw.b1);
    mpButton2 = MediaPlayer.create(getBaseContext(),R.raw.b2);
    mpButton3 = MediaPlayer.create(getBaseContext(),R.raw.b3);

进入活动的onCreate或onResume方法。