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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)