android - how to make a button click play a sound file every time it been pressed?

前端 未结 5 1667
旧巷少年郎
旧巷少年郎 2020-12-01 12:42

I have open a new project -

Now what I would like to do is this - By pressing on the button I want an mp3 file being played - and also that each time the button is p

相关标签:
5条回答
  • 2020-12-01 13:21

    Hope this steps will help you to go ahead..

    1.put your mp3 file under row folder (if not exist create one --> Right click on project ->new -> createfolder)

    2.R&D ongoogle to play mp3 first (media player)

    3.on click of button load that perticular file (by setting onclick listner to button)

    best luck

    0 讨论(0)
  • 2020-12-01 13:28
    1. You should put mp3 file in /assets folder.

    2. put this code inside onCreate() method after setContentView()

      final MediaPlayer mp = new MediaPlayer();
      Button b = (Button) findViewById(R.id.button1); 
      
      b.setOnClickListener(new OnClickListener() {
      
          @Override
          public void onClick(View v) {
      
              if(mp.isPlaying())
              {  
                  mp.stop();
              } 
      
              try {
                  mp.reset();
                  AssetFileDescriptor afd;
                  afd = getAssets().openFd("AudioFile.mp3");
                  mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                  mp.prepare();
                  mp.start();
              } catch (IllegalStateException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
      
      
      
          }
      });
      

      3.sound will be played again each time you press button. You don't have to write any extra code for that.

    Note that AudioFile.mp3 is the name of the mp3 file in /assets folder

    Hope this answer is helpful:)

    0 讨论(0)
  • 2020-12-01 13:28

    I solved this like:

    public void onClick(View v) {
        sound.start();
    
        if (sound.isPlaying()) {
            sound.seekTo(0);
            sound.start();
        }
    }
    
    0 讨论(0)
  • 2020-12-01 13:38

    I think this is the pretty much what you wanted:

    public class MainActivity extends Activity {
        String tag;
        static MediaPlayer mp;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            tag=getPackageName();
        }
    
        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            mp = new MediaPlayer();
            Button b = (Button) findViewById(R.id.button1); 
    
             b.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                       // MediaPlayer mp = new MediaPlayer();
    
                        if(!mp.isPlaying())
                        {
                            mp= new MediaPlayer();
                        }
                        try {
                            AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
                            mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                            mp.prepare();
                            mp.start();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
    
                    }
                });
    
            /*b.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    if(mp.isPlaying())
                    {  
                        Log.e(tag,"mp is playing");
    
                        mp.stop();
                        mp.reset();
                        //mp.start();
                    } 
                    try {
    
                        AssetFileDescriptor afd;
                        afd = getAssets().openFd("AudioFile.mp3");
                        mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                        mp.prepare();
                        mp.start();
                      //  mp.release();
                    } catch (IllegalStateException e) {
                        Log.e(tag, e.toString());
                        //e.printStackTrace();
                    } catch (IOException e) {
                        Log.e(tag, e.toString());
    
                        //e.printStackTrace();
                    }
    
    
    
                }
            });
    
            mp.setOnPreparedListener(new OnPreparedListener() {
    
                @Override
                public void onPrepared(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                mp.start(); 
                }
            });*/
    
        }
    
    
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 13:41

    If you really have to invoke the click programmatically because the view has no own sound, i would solve it like that, its the simplest solution and a oneliner

    view.playSoundEffect(SoundEffectConstants.CLICK);
    

    very simple and works, if you want to make a layout play a sound you need to put

    android:soundEffectsEnabled="true"
    

    to the Layout

    0 讨论(0)
提交回复
热议问题