制作不同的按钮

时间:2013-12-07 00:12:13

标签: android

假设我有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();

                                };
                            });
                                }
                            });

               }});

//与其他声音相同的其他按钮我尝试了各种方法来做到这一点,例如,通过删除声音的释放,但是当你调用上面的按钮时,所有在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)

您可以使用此代码播放声音

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

       public void onClick(View v){
          startPlaying();
   }
});
b2.setOnClickListener(new Button.OnClickListener() { 

       public void onClick(View v){
          startPlaying();
   }
});
b3.setOnClickListener(new Button.OnClickListener() { 

       public void onClick(View v){
          startPlaying();
   }
});

private void startPlaying(int position) {

            MediaPlayer mPlayer = new MediaPlayer();
             File file=new File(Environment.getExternalStorageDirectory(),"test.mp3");


                 if (test.exists()) {
                            //for playing record we should use 

                                    try {
                                        mPlayer.reset();
                                        mPlayer.setDataSource(file+"");
                                        mPlayer.prepare();
                                        mPlayer.start();
                                    } catch (IllegalArgumentException e) {
                                        e.printStackTrace();
                                    } catch (SecurityException e) {
                                        e.printStackTrace();
                                    } catch (IllegalStateException e) {
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                    } 
    }