score:0
try this (just define media player in public area)
public class main extends activity {
mediaplayer mymediaplayer = null;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
//related to the media player
mymediaplayer = mediaplayer.create(main.this, r.raw.audio);
//button related to play btn
button mybuttonone = (button) findviewbyid(r.id.btn_audio);
mybuttonone.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
mymediaplayer.start();
}
});
//button related to stop btn
button mybuttontwo = (button) findviewbyid(r.id.btn_stop);
mybuttontwo.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
mymediaplayer.pause();
}
});
}
}
score:0
the point is implement mediaplayer in the loop of if or any loop
public void onclick(view v) {
// todo auto-generated method stub
switch (v.getid()){
case r.id.buttonplay:
//mp.reset();
mp=mediaplayer.create(ur context.this, r.raw."ursong.mp3");
mp.start();
break;
case r.id.buttonstop:
mp.stop();
break;
}
}
score:0
you must move this code :
final mediaplayer mymediaplayer = mediaplayer.create(main.this, r.raw.audio);
in :
mybuttonone.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
final mediaplayer mymediaplayer = mediaplayer.create(main.this,r.raw.audio);
mymediaplayer.start();
}
});
score:1
if you want to start /stop sound on button click then use mediaplayer.start()
and mediaplayer.pause()
to pause currently playing sound which start again on start button click . and override activity onpause
to finally stop and free mediaplayer. make changes in your code as :
onstop button click :
mybuttontwo.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
mymediaplayer.pause(); //>>pause current sound
mymediaplayer.seekto(0); //>> seek to start it again
}
});
and override onpause method of activity :
@override
protected void onpause{
super.onpause();
mymediaplayer.stop(); //>>> stop mymediaplayer
mymediaplayer.release(); //>>> free mymediaplayer
}
Source: stackoverflow.com
Related Query
- How to play and stop an mp3 file in an android app
- How to implement and use google cloud sql in my android app (eclipse)
- How to use and package a JAR file with my Android app?
- How to generate a jar file for a java project and use the jar in an android project
- How to publish an Android app from Android-Studio using a Keystore file generated in Eclipse?
- How to build and deploy a phonegap app from within Android Studio without eclipse
- How to program a button to export table layout to image or PDF file on android app
- How to scan all mp3 files and not only from specific path for an android media player I make
- How to get an Android app running on API level 7 and 8?
- Play MP3 at Android App
- How to debug Android App and Google-App-Engine projects simultaneously with Eclipse
- How to stop hard coding text attribute error in Android app development Eclipse?
- How to convert android library project to .jar file with all classes and resources?
- How to include a new .java file in my Android app project?
- How to check Relay Server existence in SAP Mobile Platform, and pairing server with Android App
- How to make Android App (.apk file size) size smaller?
- How to retrieve the file name of an image in Parse.com, for an Android app
- How to correctly build Jsoup.jar and export it to be used by Android APP
- How do i retrieve the data using JSON from the Remote Database and display in my android app xml file?
- How to stop my XML file from being truncated when I try and parse the file
- How do you integrate PyGame and Android SDK together inside Eclipse to build an Android App running Java SDK as the main menu for PyGame?
- How to make an app with android browser and a fixed url to work upon
- how do I get statistic of ram and cpu utilization when android app run
- how to stop writing the android logcat into file in dos prompt
- How to make the button fit the same amount of the screen and be in the same place on android app
- how to stop play sound when close the app
- How to change the icon of an Android app in Eclipse?
- Typical .gitignore file for an Android app
- How to exit an Android app programmatically?
- How to load my app from Eclipse to my Android phone instead of AVD
More Query from same tag
- eclipse project after exporting to jar doesn't work
- Is there a way to use Eclipse to generate a JSF form from a JPA POJO?
- Modifying Java Paths when deploying WAR
- Java Equivalent of .NET's ManualResetEvent and WaitHandle
- Where Should I put beans.xml content (Jdbc Connect with Spring-Eclipse Dynamic webapplication)
- Eclipse exits when closing Jython Eclipse console
- Questions about IntelliJ to Eclipse transition
- Include source folder to classpath in Eclipse
- Unable to update Maven configuration Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5
- Why Eclipse m2e plugin deploy wrong formatted jars to Tomcat server
- Error while running the build.xml usint ANT Build in eclipse/junit
- How can I observe the changed state of model items in an ObservableList?
- Cannot see primefaces components in visual tab of eclipse
- java.lang.runtimeexception unable to start activity componentinfo java.lang.nullpointerexception android
- Android-sdk folder doesn't appear inside Program files
- Hi am trying to add maven plugin to eclipse the following error can one help me out to solve this
- Linking SDL project in Eclipse
- How to access External Micro SD card of the phone?
- Where are hadoop jar files in hadoop 2?
- Why does not Run as work in Eclipse the first time?
- maven Goals list in eclipse using m2eclipse
- Eclipse EAR project structure in eclipse
- Howto ignore specific undefined variables in Pydev Eclipse
- Location listener working in debug mode but not in compiled APK
- Running a java program located at any location from another java program
- Where is the detach option in Eclipse Kepler?
- collect2.exe: error: ld returned 1 exit status in eclipse cdt
- Using EGit and git simultaneously
- Eclipse file template plugin doesn't use my template file
- ID Types in hibernate