简体   繁体   中英

Active button on android to call method

I have button than call method play stream like this:

// method for play stream after stop it.
    public void startradio(View v) {
        try{
            if(mp.isPlaying()){
                return;
            }
               mp.start();
        } catch(IllegalStateException ex){
            ex.printStackTrace();
        } 
    }

and I define button like this:

Button PlayBtn = (Button)findViewById(R.id.btnPlay);

Now I want when click on the PlayBtn I want to start that function by using this way:

setOnClickListener

Edit:

my all code like this:

package com.example.kam;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;


public class MainActivity extends Activity {
    public MediaPlayer mp;
    boolean isPrepared = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void onCompletion(MediaPlayer mediaPlayer) {
        synchronized(this){
            isPrepared = false;
        }
    }

    protected void onResume (){
        super.onResume();

        mp = new MediaPlayer();
        try {
            mp.setDataSource("http://radio.arabhosters.com:8015/");
        } 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();
        }
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } //also consider mp.prepareAsync().
        // defult start stream when start App.
        mp.start();
    }

    // method for play stream after stop it.
        public void startradio(View v) {
            try{
                if(mp.isPlaying()){
                    return;
                }
                   mp.start();
            } catch(IllegalStateException ex){
                ex.printStackTrace();
            } 
        }

    // method for pause stream. 
    public void pauseradio(View v) {
        mp.pause();
    }

    public boolean isPlaying() {
        return mp.isPlaying();
    }

    public boolean isLooping() {
        return mp.isLooping();
    }

    public void setLooping(boolean isLooping) {
        mp.setLooping(isLooping);
    }

    public void setVolume(float volumeLeft, float volumeRight) {
        mp.setVolume(volumeLeft, volumeRight);
    }

    // method for stop stream.
    public void stopradio(View v) {
        if(mp.isPlaying()){
            mp.stop();
        }
        mp.release();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

}
Button PlayBtn = (Button) findViewById(R.id.btnPlay);
PlayBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startradio(v);
    }
});
PlayBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//to do ur function
}
});

You could use this code:

Button PlayBtn = (Button)findViewById(R.id.btnPlay);
PlayBtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        startradio(v);
    }
});

I'm not sure why you need View v in your startradio method; you can remove it from the method declaration and the call.

1- implement your activity with onClickListener (which should be view not dialog)

2-

Button PlayBtn = (Button)findViewById(R.id.btnPlay);
   playBtn.setOnClickListener(this);

3-

@override 
   public void onClick(View v)
    {
       try{
        if(mp.isPlaying()){
            return;
        }
           mp.start();
    } catch(IllegalStateException ex){
        ex.printStackTrace();
    } 
}

Update:-

put this

mp = new MediaPlayer();

inside onCreate(). remove from onResume()

//

public class MainActivity extends Activity implements OnClickListener{
    public MediaPlayer mp;
    boolean isPrepared = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mp = new MediaPlayer();
      Button PlayBtn = (Button)findViewById(R.id.btnPlay);
        PlayBtn .setonClickListener(this);


    }

@override
public void onClick(View v)
{
startradio(v);
}

    public void onCompletion(MediaPlayer mediaPlayer) {
        synchronized(this){
            isPrepared = false;
        }
    }

    protected void onResume (){
        super.onResume();

        try {
            mp.setDataSource("http://radio.arabhosters.com:8015/");
        } 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();
        }
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } //also consider mp.prepareAsync().
        // defult start stream when start App.
        mp.start();
    }

    // method for play stream after stop it.
        public void startradio(View v) {
            try{
                if(mp.isPlaying()){
                    return;
                }
                   mp.start();
            } catch(IllegalStateException ex){
                ex.printStackTrace();
            } 
        }

    // method for pause stream. 
    public void pauseradio(View v) {
        mp.pause();
    }

    public boolean isPlaying() {
        return mp.isPlaying();
    }

    public boolean isLooping() {
        return mp.isLooping();
    }

    public void setLooping(boolean isLooping) {
        mp.setLooping(isLooping);
    }

    public void setVolume(float volumeLeft, float volumeRight) {
        mp.setVolume(volumeLeft, volumeRight);
    }

    // method for stop stream.
    public void stopradio(View v) {
        if(mp.isPlaying()){
            mp.stop();
        }
        mp.release();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

}
public class MainActivity extends Activity implements OnClickListener{
    public MediaPlayer mp;
    boolean isPrepared = false;
    Button PlayBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PlayBtn = (Button)findViewById(R.id.btnPlay);
        PlayBtn.setOnClickListener(this);
        mp = new MediaPlayer();
      try {
            mp.setDataSource("http://radio.arabhosters.com:8015/");
            mp.prepare();
        } 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();
        }
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }
    @Override
    public void onClick(View v){
     if(v == PlayBtn){
         start();
      }
     else if(v == PauseBtn){
          pause();
      }
     else if(v == StopBtn){
          stop();
      }
    }
    public void onCompletion(MediaPlayer mediaPlayer) {
        synchronized(this){
            isPrepared = false;
        }
    }



    // method for play stream after stop it.
        public void start() {
            try{
                if(mp.isPlaying()){
                    return;
                }
                   mp.start();
            } catch(IllegalStateException ex){
                ex.printStackTrace();
            } 
        }

    // method for pause stream. 
    public void pause() {
        mp.pause();
    }

    public boolean isPlaying() {
        return mp.isPlaying();
    }

    public boolean isLooping() {
        return mp.isLooping();
    }

    public void setLooping(boolean isLooping) {
        mp.setLooping(isLooping);
    }

    public void setVolume(float volumeLeft, float volumeRight) {
        mp.setVolume(volumeLeft, volumeRight);
    }

    // method for stop stream.
    public void stop() {
        if(mp.isPlaying()){
            mp.stop();
        }
        mp.release();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

}

Create a Button for Pause and Stop your mediaplayer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM