I need some advice about the code below. I'm trying to create a simple apps that when people click play button, then the music will start playing, and when people click the stop button, the music will stop playing. I'm using a single button for this two task start and stop.
I already test on android simulator, this start and stop button are working properly as I wanted to. But the problem occurred when I build an APK file and play it on a device. The stop button cannot work as I wanted to. If I press the stop button, the music will restart the playing from the beginning.
Any ideas about this problem? Thank you.
btn_playstop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (btn_playstop.isPressed()) {
if (player.isPlaying()) {
player.stop();
try {
player.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.seekTo(0);
btn_playstop.setBackgroundResource(R.drawable.play);
}
else {
player.start();
btn_playstop.setBackgroundResource(R.drawable.stop);
}
}
}
});
To achieve the play/stop button, do it this way.
btn_playstop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (player != null && player.isPlaying()) {
player.stop();
player.release();
player = null;
try {
player.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.seekTo(0); //<---- This is to resume the player from a specified position. It is helpful for a pause button. You can remove it for a stop button.
btn_playstop.setBackgroundResource(R.drawable.play);
}
else {
player.start();
btn_playstop.setBackgroundResource(R.drawable.stop);
}
}
});
You don't have to call the seekTo(Length) method for a stop button as it is meant for resuming the player from a specified position. Also if (btn_playstop.isPressed()) is unnecessary as you have already called the button using setOnClickListener.
imabutton=findViewById(R.id.imgbutton);
imabutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (imabutton.getTag().toString().contains("not_playing")){
if (mediaPlayer!=null)mediaPlayer.release();
mediaPlayer=MediaPlayer.create(MainActivity.this,R.raw.music1);
mediaPlayer.start();
imabutton.setImageResource(R.drawable.stop);
imabutton.setTag("playing");
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
imabutton.setTag("not_playing");
imabutton.setImageResource(R.drawable.play);
}
});
}else {
mediaPlayer.release();
imabutton.setImageResource(R.drawable.play);
imabutton.setTag("not_playing");
}
}
});
Related
I have an app with one button for play/stop music . when I run the app , music stops in start up of that . But I want when I run the app that music plays automatically and if I click the button , music stops . I searched the site but I cant find the answer .Please help me my friends.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
mainkan=(ImageButton)findViewById(R.id.imageButton1);
mainkan.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
go();
}
});
public void go(){
mp=MediaPlayer.create(ProjectisengActivity.this, R.raw.test);
if(mp.isPlaying()){
mp.stop();
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.seekTo(0);
You can follow this:
if (mp == null) {
mp = MediaPlayer.create(ProjectisengActivity.this, R.raw.test);
}
if (mp.isPlaying()) {
mp.pause();
} else {
mp.start();
}
you can use toggle button like below
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});
Call go() method in your activity oncreate method.
Its play automatically on activity start when you button clicked its again call the same method so the condition is palying so its stopped.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
mainkan=(ImageButton)findViewById(R.id.imageButton1);
go();
mainkan.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
go();
}
});
public void go(){
mp=MediaPlayer.create(ProjectisengActivity.this, R.raw.test);
if(mp.isPlaying()){
mp.stop();
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.seekTo(0);
I'm trying to create an app that play a certain audio file, that was previously recorded, when I press a button or shake my phone. Here is my code.
public class Reproduzir extends Activity implements SensorEventListener{
MediaPlayer player = new MediaPlayer();
SensorManager sensor;
#Override
protected void onCreate(Bundle savedInstanceState ){
super.onCreate(savedInstanceState);
sensor= (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.reproduzir);
Button reproduzir = (Button) findViewById(R.id.reproduzir);
reproduzir.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
play();
}
});
Button fechar= (Button) findViewById(R.id.fechar);
fechar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (player.isPlaying()) {
player.stop();
player.release();
}
finish();
}
});
}
public void play(){
try {
player.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/2cp.3gp");
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onStart(){
super.onStart();
sensor.registerListener(this,sensor.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.values[0]>10.2
||event.values[1]>10.2
||event.values[2]>10.2){
play();
}
}
}
My problem is that when I run it, I can only play it once. If I press the button a second time or shake it again, it does nothing. Can anyone help?
The easy fix is to simply add player.reset() in your play() method BEFORE calling player.setDataSource(...).
You can only call setDataSource(...) once without resetting the player. It is legal to call reset() in any state however (even if the player isn't yet initialized).
In other words, even if it's the first time you've called play() it is OK to use the following code in your play() method...
try {
player.reset();
player.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/2cp.3gp");
player.prepare();
player.start();
}
// Your catch blocks here
Try to use:
mediaPlayer.reset()
after:
mediaPlayer.stop()
instead of release.
You can only set data source once.
in you code everytime the user presses the button you do this:
player.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/2cp.3gp");
whice is wrong.
you need to setdata and prepare once and play how many times you want.
like so:
put this in you onCreate:
try {
player.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/2cp.3gp");
player.prepare();
player.start();
in the onClick(View v), just have :
player.play();
Long story short - you have to use:
setLooping(true)
For more information about the Media Player check the post below. Take in mind the state diagram
http://developer.android.com/reference/android/media/MediaPlayer.html
MediaPlayer State diagram
To start off, playing and stopping music works fine. I've already searched SX, and have tried some of the solutions listed for this issue, but they haven't resolved my issue.
I have a "Replay Music" button, that is supposed to restart an .mp3 file from the beginning. I've got the necessary code in place (like reset(), prepare(), etc, in order for the media player to replay the music, but when the button is pushed, nothing happens.
I have all the OnClickListeners and other necessary things setup, it is just this one issue.
Below is the complete code for my "Replay Music" method. Any help will be gratefully received.
Just a thought: Perhaps it is something to do with the placement of the mediaPlayer.start() method call?
public void replaySoundButtonListener() {
Button testButton = (Button) findViewById(R.id.replayMusicButton);
testButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource("sdcard/InsomniMusic/insomnimix.mp3");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
}
});
}
You only need to prepare and set the data source of a mediaplayer once. When you call reset the mediaplayer has to be reinitialised completely (set datasource again and call prepare). If you would like to reset the mediaplayer to the start of the song I guess you could use
mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
If the song has already ended (or stopped otherwise), only mediaPlayer.start() will suffice (but I guess in your case you can never be sure of that).
More information on the start function here
I made a functions to start() stop() reset() and pause() in another Class
import android.content.Context;
import android.media.MediaPlayer;
public class AudioPlayer extends MediaPlayer {
private MediaPlayer mPlayer;
public void stop()
{
if(mPlayer!=null)
{
mPlayer.release();
mPlayer=null;
}
}
public void pause()
{
if(mPlayer.isPlaying()) {
mPlayer.pause();
}
else
{
mPlayer.start();
}
}
public void reset(Context c)
{
stop();
mPlayer=MediaPlayer.create(c, R.raw.one_small_step); //uzimamo .create(context,..) jer uzimamo iz res/raw lokalne datoteke
mPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
stop();
}
});
mPlayer.start();
}
public void play(Context c)
{
stop();
mPlayer=MediaPlayer.create(c, R.raw.one_small_step);
mPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
stop();
}
});
mPlayer.start();
}
}
//Here is my fragment
private Button mPlayButton;
private Button mStopButton;
private Button mPauseButton;
private Button mResetButton;
private AudioPlayer mPlayer=new AudioPlayer();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=getActivity().getLayoutInflater().inflate(R.layout.fragment_hello_moon, container);
mPlayButton=(Button)v.findViewById(R.id.hellomoon_playButton);
mPlayButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.play(getActivity());
}
});
mStopButton=(Button)v.findViewById(R.id.hellomoon_stopButton);
mStopButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.stop();
}
});
mPauseButton=(Button)v.findViewById(R.id.hellomoon_pauseButton);
mPauseButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.pause();
}
});
mResetButton=(Button)v.findViewById(R.id.hellomoon_resetButton);
mResetButton.setOnClickListener(new OnClickListener(
) {
#Override
public void onClick(View v) {
mPlayer.reset(getActivity());
}
});
return v;
}
#Override
public void onDestroy() {
super.onDestroy();
mPlayer.stop(); //Nakon sto je fragment unisten MediaPlayer nastavlja radit jer je na drugacijem Threadu pa treba pozvati Stop();
}
GOAL: Start, Stop, then Start play of a sound by clicking button 3 times
Hello All
I am new to Android.
I have had trouble restarting the play of a sound after stopping it by click of a button.
I have researched this problem
( Link ) and found different methods to apply.
So, I have tried four ways (unsuccessfully) to do this.
There are four buttons which use different methods.
Button 1 will be used as a control.
Look to comments for results of different methods.
I will post a couple entries from LogCat below.
Thank You
Here is a link to a relevant Android State Diagram
src/com.example.playsound/MainActivity.java
package com.example.playsound;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
//Create Media Player
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button Sound
final MediaPlayer mPlayer1 = MediaPlayer.create(MainActivity.this, R.raw.playsound0);
final MediaPlayer mPlayer2 = MediaPlayer.create(MainActivity.this, R.raw.playsound1);
final MediaPlayer mPlayer3 = MediaPlayer.create(MainActivity.this, R.raw.playsound2);
final MediaPlayer mPlayer4 = MediaPlayer.create(MainActivity.this, R.raw.playsound3);
//Button References
Button button1 = (Button)this.findViewById(R.id.button1);
Button button2 = (Button)this.findViewById(R.id.button2);
Button button3 = (Button)this.findViewById(R.id.button3);
Button button4 = (Button)this.findViewById(R.id.button4);
//Button 1 OnClickListener
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mPlayer1.isPlaying() ) {
try {
mPlayer1.stop();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
// BUTTON 1
else { // plays sound upon first click, stops on second click
try { // but does not play on third click
mPlayer1.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
});
//Button 2 Uses .prepareAsync()
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mPlayer2.isPlaying() ) { // BUTTON 2
try { // Plays sound upon first click, stops on second click
mPlayer2.stop(); // But will not play upon third click
mPlayer2.reset();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
else {
try {
mPlayer2.prepareAsync();
} catch (IllegalStateException e) {
e.printStackTrace();
}
mPlayer2.start();
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mPlayer3.isPlaying() ) {
try { // BUTTON 3
mPlayer3.stop(); // Does not play sound upon first button click
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
else {
mPlayer3.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mPlayer3) {
mPlayer3.start();
}
});
}
}
});
//Button 4 OnClickListener
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mPlayer4.isPlaying() ) {
try {
mPlayer4.stop();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
// BUTTON 4
else { // does not play sound on first click
try {
mPlayer4.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer4.start();
}
}
});
}
}
Most errors in LogCat are similar to:
prepareAsync called in state 8
start called in state 0
start called in state 1
The errors are mostly "call in state #" errors.
I have referenced the diagram posted in the link above.
I still have these errors when I attempt to call methods in correct place.
You are calling methods in unappropriate states.
PrepareAsync can be called in only {Initialized, Stopped} state.
Start can be called only in {Prepared, Started, Paused, PlaybackCompleted} states.
So if you try to call those in other states than those then it will throw exception.
To have a clear knowledge about the states read this doc
i'm newbie, i'm tried to make play audio play and stop for 1 button only, but i'm in trouble now.
if i touch a button when audio is playing, it doesn't stop, even playing audio again and make a double sound.
here's my code
public class ProjectisengActivity extends Activity{
ImageButton mainkan;
MediaPlayer mp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
mainkan=(ImageButton)findViewById(R.id.imageButton1);
mainkan.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
go();
}
});
public void go(){
mp=MediaPlayer.create(ProjectisengActivity.this, R.raw.test);
if(mp.isPlaying()){
mp.stop();
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.seekTo(0);
}
else {
mp.start();
}
i'm create for android 3.0 (HoneyComb)
try below code in go function....
public void go() {
if(mp == null) {
mp=MediaPlayer.create(ProjectisengActivity.this, R.raw.test);
}
if(mp.isPlaying()){
mp.stop();
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.seekTo(0);
}
else {
mp.start();
}
}
It is simple, you should follow these steps to achieve this.
In your test2.xml create two buttons name start and stop.
Set the android:visibility attribute gone of stop button in xml file.
Now in your activity get the id of these two buttons and write the code for
starting and stopping of media player.
Set the visibility attribute gone on start click and visible of stop button,
follow its opposite on stop click.
I think you have wrong this line :
mp=MediaPlayer.create(ProjectisengActivity.this, R.raw.test);
You create new instance every time user clicks the button, so it's never playing and starts again. Put this line into onCreate rather than go()
Try also this,
public class MainActivity extends Activity {
MediaPlayer mPlayer;
int flag = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (flag == 0) {
Log.v("Inside if", "Success" + "");
mPlayer = MediaPlayer.create(getApplicationContext(),
R.raw.sample);
mPlayer.start();
flag++;
} else {
Log.v("Inside else", "Success" + "");
mPlayer.stop();
mPlayer.release();
flag = 0;
}
}
});
}
}