search
HomeJavaHow to make audio stop when another audio starts (lots of audio)

Question content

I have 30-31 buttons and I want them to play one at a time, so if I click one button it plays, then when I click another button whatever Whichever button it is, it will stop, here is the code sample basically does that and repeats, but just the numbers change, so 1,2,3,4 etc:

public void Button1Text(View view) {
        Media1 = MediaPlayer.create(QuranJ30MA.this, R.raw.hannaba);
        Media1.start();
    }

    public void Button1Text2(View view) {
        Button1p = MediaPlayer.create(QuranJ30MA.this, R.raw.mannaba);
        Button1p.start();
    }

I did try to see if other people are trying to do the same thing but since I'm not very good with java I don't understand how to apply it to my machine, if it's not too much to ask I just want to explain Let's learn how it works, thanks :)

Solution

You should never create multiple mediaplayers, they are heavyweight objects and take up a lot of resources. Instead, make a media player and use setdatasource to change the sound its settings play (of course, you'll need to stop the original sound first).

The next thing to change is the build data. Create a mapping of ids to sounds:

map<int, int> sounds = new hashmap();
sounds.put(button1id, r.raw.hannaba);
sounds.put(button2id, r.raw.mannaba);
...

Then you can have the click function:

public void buttonClick(View view) {
   mediaPlayer.stop()
   mediaPlayer.setDataSource(sounds.get(view.getId())
   mediaPlayer.start()
}

Set this as the onclicklistener for all buttons you want to execute and only need to write it once.

The above is the detailed content of How to make audio stop when another audio starts (lots of audio). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor