简体   繁体   English

在android音板中,如何在单击另一个声音后停止播放声音?

[英]In an android soundboard how to make one sound stop playing after another one is clicked on?

How do I modify this so that if one sound is playing already and another is clicked on, the previous stops playing and the newly selected starts? 如何修改此设置,以便如果一个声音已经在播放而另一声音被单击,则先前的声音会停止播放,而新选择的声音会开始播放? Thanks everyone for their help in advance. 预先感谢大家的帮助。 (this is not all the code just the most important) (这不仅是所有代码中最重要的)

public class newBoard extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    Toast.makeText(this, "Thank you for using this App.", Toast.LENGTH_LONG).show();

    // ads - load request to display
    AdView layout = (AdView)this.findViewById(R.id.adView);

    // ads - load display with an ad
    AdRequest adRequest = new AdRequest();
    adRequest.setTesting(true);

    layout.loadAd(adRequest);

    // import sound files
    final MediaPlayer sound01 = MediaPlayer.create(this, R.raw.sound01);
    final MediaPlayer sound02 = MediaPlayer.create(this, R.raw.sound02);
    final MediaPlayer sound03 = MediaPlayer.create(this, R.raw.sound03);
    final MediaPlayer sound04 = MediaPlayer.create(this, R.raw.sound04);
    final MediaPlayer sound05 = MediaPlayer.create(this, R.raw.sound05);
    final MediaPlayer sound06 = MediaPlayer.create(this, R.raw.sound06);
    final MediaPlayer sound07 = MediaPlayer.create(this, R.raw.sound07);
    final MediaPlayer sound08 = MediaPlayer.create(this, R.raw.sound08);
    final MediaPlayer sound09 = MediaPlayer.create(this, R.raw.sound09);
    final MediaPlayer sound10 = MediaPlayer.create(this, R.raw.sound10);
    final MediaPlayer sound11 = MediaPlayer.create(this, R.raw.sound11);
    final MediaPlayer sound12 = MediaPlayer.create(this, R.raw.sound12);
    final MediaPlayer sound13 = MediaPlayer.create(this, R.raw.sound13);
    final MediaPlayer sound14 = MediaPlayer.create(this, R.raw.sound14);
    final MediaPlayer sound15 = MediaPlayer.create(this, R.raw.sound15);
    final MediaPlayer sound16 = MediaPlayer.create(this, R.raw.sound16);
    final MediaPlayer sound17 = MediaPlayer.create(this, R.raw.sound17);
    final MediaPlayer sound18 = MediaPlayer.create(this, R.raw.sound18);
    final MediaPlayer sound19 = MediaPlayer.create(this, R.raw.sound19);
    final MediaPlayer sound20 = MediaPlayer.create(this, R.raw.sound20);
    final MediaPlayer sound21 = MediaPlayer.create(this, R.raw.sound21);
    final MediaPlayer sound22 = MediaPlayer.create(this, R.raw.sound22);
    final MediaPlayer sound23 = MediaPlayer.create(this, R.raw.sound23);
    final MediaPlayer sound24 = MediaPlayer.create(this, R.raw.sound24);
    final MediaPlayer sound25 = MediaPlayer.create(this, R.raw.sound25);

    // play sound files on clicks
    Button s01 = (Button) findViewById(R.id.button01); 
    s01.setText(this.getString(R.string.quote01));
    s01.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound01.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound01.start();                
            }

    });
    registerForContextMenu(s01);

    Button s02 = (Button) findViewById(R.id.button02); 
    s02.setText(this.getString(R.string.quote02));
    s02.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound02.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound02.start();
        }
    });
    registerForContextMenu(s02);

    Button s03 = (Button) findViewById(R.id.button03); 
    s03.setText(this.getString(R.string.quote03));
    s03.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound03.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound03.start();
        }
    });
    registerForContextMenu(s03);

    Button s04 = (Button) findViewById(R.id.button04); 
    s04.setText(this.getString(R.string.quote04));
    s04.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound04.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound04.start();
        }
    });
    registerForContextMenu(s04);

    Button s05 = (Button) findViewById(R.id.button05); 
    s05.setText(this.getString(R.string.quote05));
    s05.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound05.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound05.start();
        }
    });
    registerForContextMenu(s05);

    Button s06 = (Button) findViewById(R.id.button06); 
    s06.setText(this.getString(R.string.quote06));
    s06.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound06.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound06.start();
        }
    });
    registerForContextMenu(s06);

Use a object member variable to hold on to your currently playing sound so that you can call stop() and don't forget to null check it. 使用对象成员变量来保留您当前正在播放的声音,以便您可以调用stop()并且不要忘记对它进行空检查。 Don't forget to release() your MediaPlayer object once you leave your activity. 离开活动后,不要忘记release() MediaPlayer对象。

The following is probably a better design: move all your audio files to your assets directory and put the file name in the button's tag, use the same click event handler to get the state of the one MediaPlayer object, stop it if its currently playing, set the new file to play from the button's tag and play the file. 以下可能是一个更好的设计:将所有音频文件移动到资产目录,并将文件名放在按钮的标记中,使用相同的click事件处理程序获取一个MediaPlayer对象的状态,如果当前正在播放,则将其停止,设置新文件以从按钮的标签播放并播放文件。 This will reduce your duplicate code significantly. 这将大大减少您的重复代码。

Use a single MediaPlayer instance. 使用单个MediaPlayer实例。 Yours is going to fail on most devices as it is anyway for allocating to many instances of MediaPlayer . 您的设备将在大多数设备上失败,因为无论如何它都会分配给许多MediaPlayer实例。 Also, repetitious code is bad. 同样,重复的代码也是不好的。 Bad bad: 坏坏:

public class NewBoard extends Activity {
    private MediaPlayer player;
    private Resources res;

    private int buttonIds = { R.id.button01, R.id.button02, R.id.button03,
                              R.id.button04, R.id.button05, R.id.button06,
                              R.id.button07, R.id.button08, R.id.button09,
                              R.id.button10, R.id.button11, R.id.button12,
                              R.id.button13, R.id.button14, R.id.button15,
                              R.id.button16, R.id.button16, R.id.button17,
                              R.id.button18, R.id.button19, R.id.button20,
                              R.id.button21, R.id.button22, R.id.button23,
                              R.id.button24, R.id.button25 };

    private int soundIds =  { R.raw.sound01, R.raw.sound02, R.raw.sound03,
                              R.raw.sound04, R.raw.sound05, R.raw.sound06,
                              R.raw.sound07, R.raw.sound08, R.raw.sound09,
                              R.raw.sound10, R.raw.sound11, R.raw.sound12,
                              R.raw.sound13, R.raw.sound14, R.raw.sound15,
                              R.raw.sound16, R.raw.sound16, R.raw.sound17,
                              R.raw.sound18, R.raw.sound19, R.raw.sound20,
                              R.raw.sound21, R.raw.sound22, R.raw.sound23,
                              R.raw.sound24, R.raw.sound25 };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        //Kill this with fire -- just an unnecessary user annoyance.
        Toast.makeText(this, 
            "Thank you for using this App.", Toast.LENGTH_LONG).show();

        AdView layout = (AdView)findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest();
        adRequest.setTesting(true);
        layout.loadAd(adRequest);

        player = new MediaPlayer();
        res = getResources();

        for(int i = 0, n = buttonIds.length(); i < n; i++) {
            Button button = (Button)findViewById(buttonIds[i]);
            button.setOnClickListener(new SoundClickListener(soundIds[i]));
        }
    }

    private class SoundClickListener implements OnClickListener {
        private int id;

        public SoundClickListener(int soundId) {
            id = soundId;
        }

        public void onClick(View v) {
            player.reset();
            player.setDataSource(
                res.openRawResourceFd(id).getFileDescriptor());
            player.prepare();
            player.start();
        }
    }
}

Something like this. 这样的事情。 May or may not compile as is. 可能会编译,也可能不会编译。 Also, as commented, kill the Toast with fire -- that's just annoying. 另外,正如所评论的那样,用火杀死Toast -真令人讨厌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Soundboard一次只能发出一种声音 - Android Soundboard only one sound at a time 在Android Studio中播放另一种声音时停止播放声音 - stop playing sound when another sound is playing in android studio 在Android(Java)中一首一首的播放歌曲 - Playing songs one after another in Android(Java) 如何让媒体播放器继续播放相同的音轨,而不在Android中启动另一个音轨 - How to keep the media player keep playing the same sound track and not starting another one in Android 切换活动后,Android Studio Soundboard按钮将无法播放声音,我只能按它们7次才能停止 - Android Studio Soundboard buttons won't play sound after switching activity i can press them only like 7 times before they stop 之后如何停止Android中的声音 - How to stop sound in Android after 许多Mediaplayer缩减为一个Mediaplayer? 怎么样? 音板 - A lot of Mediaplayers shrink to one Mediaplayer? How? Soundboard 如何停止已经播放的声音并立即开始播放 - How to stop already playing sound and immediately start another 如何通过另一种方法停止声音(在播放时) - How to stop a sound (while its playing) from another method 如何在单击按钮时停止播放声音以启动另一个声音文件? - How to stop playing sounds on button click to start another sound file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM