android在打开活动时自动启动onClick

android在打开活动时自动启动onClick,android,mp3,Android,Mp3,现在,当我单击“buttonPlayPause”图像按钮时,此代码将播放mp3。但我需要在打开此活动时自动启动此播放按钮。。。有可能吗 public class StreamingMp3Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{ private ImageButton buttonPla

现在,当我单击“buttonPlayPause”图像按钮时,此代码将播放mp3。但我需要在打开此活动时自动启动此播放按钮。。。有可能吗

public class StreamingMp3Player extends Activity implements OnClickListener,          OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;

private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; 

private final Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
}

/** This method initialise all the views in project*/
private void initView() {
    buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
    buttonPlayPause.setOnClickListener(this);

    seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);  
    seekBarProgress.setMax(99); // It means 100% .0-99
    seekBarProgress.setOnTouchListener(this);
    editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
    editTextSongURL.setText(R.string.testsong_20_sec);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
}

private void primarySeekBarProgressUpdater() {
    seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/   mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
         /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
        try {
            mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
            mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL

        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            buttonPlayPause.setImageResource(R.drawable.button_pause);
        }else {
            mediaPlayer.pause();
            buttonPlayPause.setImageResource(R.drawable.button_play);
        }

        primarySeekBarProgressUpdater();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.SeekBarTestPlay){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
            mediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
     /** MediaPlayer onCompletion event handler. Method which calls then song   playing is complete*/
    buttonPlayPause.setImageResource(R.drawable.button_play);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    /** Method which updates the SeekBar secondary progress by current song   loading from URL position*/
    seekBarProgress.setSecondaryProgress(percent);
}
}
我的布局

public class StreamingMp3Player extends Activity implements OnClickListener,          OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;

private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; 

private final Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
}

/** This method initialise all the views in project*/
private void initView() {
    buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
    buttonPlayPause.setOnClickListener(this);

    seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);  
    seekBarProgress.setMax(99); // It means 100% .0-99
    seekBarProgress.setOnTouchListener(this);
    editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
    editTextSongURL.setText(R.string.testsong_20_sec);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
}

private void primarySeekBarProgressUpdater() {
    seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/   mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
         /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
        try {
            mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
            mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL

        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            buttonPlayPause.setImageResource(R.drawable.button_pause);
        }else {
            mediaPlayer.pause();
            buttonPlayPause.setImageResource(R.drawable.button_play);
        }

        primarySeekBarProgressUpdater();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.SeekBarTestPlay){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
            mediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
     /** MediaPlayer onCompletion event handler. Method which calls then song   playing is complete*/
    buttonPlayPause.setImageResource(R.drawable.button_play);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    /** Method which updates the SeekBar secondary progress by current song   loading from URL position*/
    seekBarProgress.setSecondaryProgress(percent);
}
}

我正在传递另一个活动的url。。。我需要打开此活动并自动播放mp3。。不希望单击播放按钮

onClick()
的逻辑移动到方法中,并从onClick和onCreate调用此方法

public class StreamingMp3Player extends Activity implements OnClickListener,          OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;

private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; 

private final Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
}

/** This method initialise all the views in project*/
private void initView() {
    buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
    buttonPlayPause.setOnClickListener(this);

    seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);  
    seekBarProgress.setMax(99); // It means 100% .0-99
    seekBarProgress.setOnTouchListener(this);
    editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
    editTextSongURL.setText(R.string.testsong_20_sec);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
}

private void primarySeekBarProgressUpdater() {
    seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/   mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
         /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
        try {
            mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
            mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL

        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            buttonPlayPause.setImageResource(R.drawable.button_pause);
        }else {
            mediaPlayer.pause();
            buttonPlayPause.setImageResource(R.drawable.button_play);
        }

        primarySeekBarProgressUpdater();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.SeekBarTestPlay){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
            mediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
     /** MediaPlayer onCompletion event handler. Method which calls then song   playing is complete*/
    buttonPlayPause.setImageResource(R.drawable.button_play);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    /** Method which updates the SeekBar secondary progress by current song   loading from URL position*/
    seekBarProgress.setSecondaryProgress(percent);
}
}
<TextView 
    android:text="Song Titile" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/SongTitle"/>   

<TextView 
    android:layout_below="@+id/SongTitle"
    android:text="The song URL" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/SongUrl"/> 
<ImageButton 
    android:layout_below="@+id/SongUrl"
    android:id="@+id/ButtonTestPlayPause" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:src="@drawable/button_play"/>


<SeekBar 
    android:layout_below="@+id/ButtonTestPlayPause"
    android:id="@+id/SeekBarTestPlay" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:thumb="@drawable/thumb_transparent"/>
</RelativeLayout>
onClick()
的逻辑移动到方法中,并从onClick和onCreate调用此方法

public class StreamingMp3Player extends Activity implements OnClickListener,          OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;

private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; 

private final Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
}

/** This method initialise all the views in project*/
private void initView() {
    buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
    buttonPlayPause.setOnClickListener(this);

    seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);  
    seekBarProgress.setMax(99); // It means 100% .0-99
    seekBarProgress.setOnTouchListener(this);
    editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
    editTextSongURL.setText(R.string.testsong_20_sec);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
}

private void primarySeekBarProgressUpdater() {
    seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/   mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
         /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
        try {
            mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
            mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL

        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            buttonPlayPause.setImageResource(R.drawable.button_pause);
        }else {
            mediaPlayer.pause();
            buttonPlayPause.setImageResource(R.drawable.button_play);
        }

        primarySeekBarProgressUpdater();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.SeekBarTestPlay){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
            mediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
     /** MediaPlayer onCompletion event handler. Method which calls then song   playing is complete*/
    buttonPlayPause.setImageResource(R.drawable.button_play);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    /** Method which updates the SeekBar secondary progress by current song   loading from URL position*/
    seekBarProgress.setSecondaryProgress(percent);
}
}
<TextView 
    android:text="Song Titile" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/SongTitle"/>   

<TextView 
    android:layout_below="@+id/SongTitle"
    android:text="The song URL" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/SongUrl"/> 
<ImageButton 
    android:layout_below="@+id/SongUrl"
    android:id="@+id/ButtonTestPlayPause" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:src="@drawable/button_play"/>


<SeekBar 
    android:layout_below="@+id/ButtonTestPlayPause"
    android:id="@+id/SeekBarTestPlay" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:thumb="@drawable/thumb_transparent"/>
</RelativeLayout>

您还可以在onCreate语句中执行:

public class StreamingMp3Player extends Activity implements OnClickListener,          OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;

private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; 

private final Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
}

/** This method initialise all the views in project*/
private void initView() {
    buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
    buttonPlayPause.setOnClickListener(this);

    seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);  
    seekBarProgress.setMax(99); // It means 100% .0-99
    seekBarProgress.setOnTouchListener(this);
    editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
    editTextSongURL.setText(R.string.testsong_20_sec);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
}

private void primarySeekBarProgressUpdater() {
    seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/   mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
         /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
        try {
            mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
            mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL

        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            buttonPlayPause.setImageResource(R.drawable.button_pause);
        }else {
            mediaPlayer.pause();
            buttonPlayPause.setImageResource(R.drawable.button_play);
        }

        primarySeekBarProgressUpdater();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.SeekBarTestPlay){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
            mediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
     /** MediaPlayer onCompletion event handler. Method which calls then song   playing is complete*/
    buttonPlayPause.setImageResource(R.drawable.button_play);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    /** Method which updates the SeekBar secondary progress by current song   loading from URL position*/
    seekBarProgress.setSecondaryProgress(percent);
}
}
private void myMethod() {
  // logic here
}


@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
          myMethod();
    }

 }



   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
    myMethod();
}

您还可以在onCreate语句中执行:

public class StreamingMp3Player extends Activity implements OnClickListener,          OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;

private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; 

private final Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
}

/** This method initialise all the views in project*/
private void initView() {
    buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
    buttonPlayPause.setOnClickListener(this);

    seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);  
    seekBarProgress.setMax(99); // It means 100% .0-99
    seekBarProgress.setOnTouchListener(this);
    editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
    editTextSongURL.setText(R.string.testsong_20_sec);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
}

private void primarySeekBarProgressUpdater() {
    seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/   mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
         /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
        try {
            mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
            mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL

        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            buttonPlayPause.setImageResource(R.drawable.button_pause);
        }else {
            mediaPlayer.pause();
            buttonPlayPause.setImageResource(R.drawable.button_play);
        }

        primarySeekBarProgressUpdater();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.SeekBarTestPlay){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
            mediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
     /** MediaPlayer onCompletion event handler. Method which calls then song   playing is complete*/
    buttonPlayPause.setImageResource(R.drawable.button_play);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    /** Method which updates the SeekBar secondary progress by current song   loading from URL position*/
    seekBarProgress.setSecondaryProgress(percent);
}
}
private void myMethod() {
  // logic here
}


@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
          myMethod();
    }

 }



   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
    myMethod();
}
在你的onclick()中:

public class StreamingMp3Player extends Activity implements OnClickListener,          OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;

private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; 

private final Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
}

/** This method initialise all the views in project*/
private void initView() {
    buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
    buttonPlayPause.setOnClickListener(this);

    seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);  
    seekBarProgress.setMax(99); // It means 100% .0-99
    seekBarProgress.setOnTouchListener(this);
    editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
    editTextSongURL.setText(R.string.testsong_20_sec);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
}

private void primarySeekBarProgressUpdater() {
    seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/   mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
         /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
        try {
            mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
            mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL

        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            buttonPlayPause.setImageResource(R.drawable.button_pause);
        }else {
            mediaPlayer.pause();
            buttonPlayPause.setImageResource(R.drawable.button_play);
        }

        primarySeekBarProgressUpdater();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.SeekBarTestPlay){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
            mediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
     /** MediaPlayer onCompletion event handler. Method which calls then song   playing is complete*/
    buttonPlayPause.setImageResource(R.drawable.button_play);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    /** Method which updates the SeekBar secondary progress by current song   loading from URL position*/
    seekBarProgress.setSecondaryProgress(percent);
}
}
以及游戏中使用的方法:

public class StreamingMp3Player extends Activity implements OnClickListener,          OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;

private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; 

private final Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
}

/** This method initialise all the views in project*/
private void initView() {
    buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
    buttonPlayPause.setOnClickListener(this);

    seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);  
    seekBarProgress.setMax(99); // It means 100% .0-99
    seekBarProgress.setOnTouchListener(this);
    editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
    editTextSongURL.setText(R.string.testsong_20_sec);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
}

private void primarySeekBarProgressUpdater() {
    seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/   mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
         /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
        try {
            mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
            mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL

        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            buttonPlayPause.setImageResource(R.drawable.button_pause);
        }else {
            mediaPlayer.pause();
            buttonPlayPause.setImageResource(R.drawable.button_play);
        }

        primarySeekBarProgressUpdater();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.SeekBarTestPlay){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
            mediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
     /** MediaPlayer onCompletion event handler. Method which calls then song   playing is complete*/
    buttonPlayPause.setImageResource(R.drawable.button_play);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    /** Method which updates the SeekBar secondary progress by current song   loading from URL position*/
    seekBarProgress.setSecondaryProgress(percent);
}
}


if(v.getId() == R.id.ButtonTestPlayPause){
play();

}

在你的onclick()中:

public class StreamingMp3Player extends Activity implements OnClickListener,          OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;

private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; 

private final Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
}

/** This method initialise all the views in project*/
private void initView() {
    buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
    buttonPlayPause.setOnClickListener(this);

    seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);  
    seekBarProgress.setMax(99); // It means 100% .0-99
    seekBarProgress.setOnTouchListener(this);
    editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
    editTextSongURL.setText(R.string.testsong_20_sec);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
}

private void primarySeekBarProgressUpdater() {
    seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/   mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
         /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
        try {
            mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
            mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL

        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            buttonPlayPause.setImageResource(R.drawable.button_pause);
        }else {
            mediaPlayer.pause();
            buttonPlayPause.setImageResource(R.drawable.button_play);
        }

        primarySeekBarProgressUpdater();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.SeekBarTestPlay){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
            mediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
     /** MediaPlayer onCompletion event handler. Method which calls then song   playing is complete*/
    buttonPlayPause.setImageResource(R.drawable.button_play);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    /** Method which updates the SeekBar secondary progress by current song   loading from URL position*/
    seekBarProgress.setSecondaryProgress(percent);
}
}
以及游戏中使用的方法:

public class StreamingMp3Player extends Activity implements OnClickListener,          OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;

private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; 

private final Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
}

/** This method initialise all the views in project*/
private void initView() {
    buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
    buttonPlayPause.setOnClickListener(this);

    seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);  
    seekBarProgress.setMax(99); // It means 100% .0-99
    seekBarProgress.setOnTouchListener(this);
    editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
    editTextSongURL.setText(R.string.testsong_20_sec);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
}

private void primarySeekBarProgressUpdater() {
    seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/   mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.ButtonTestPlayPause){
         /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
        try {
            mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
            mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL

        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            buttonPlayPause.setImageResource(R.drawable.button_pause);
        }else {
            mediaPlayer.pause();
            buttonPlayPause.setImageResource(R.drawable.button_play);
        }

        primarySeekBarProgressUpdater();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.SeekBarTestPlay){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
            mediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
     /** MediaPlayer onCompletion event handler. Method which calls then song   playing is complete*/
    buttonPlayPause.setImageResource(R.drawable.button_play);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    /** Method which updates the SeekBar secondary progress by current song   loading from URL position*/
    seekBarProgress.setSecondaryProgress(percent);
}
}


if(v.getId() == R.id.ButtonTestPlayPause){
play();

}


您可以在图像按钮上使用
performClick()
。在onCreate()中调用
performClick()。这将基本上实现
blackbelt
的建议。您可以在图像按钮上使用
performClick()
。在onCreate()中调用
performClick()。这将基本上实现
blackbelt
的建议。是的。。它的工作有一些错误。。。我正在设法修理它。。谢谢你。。它的工作有一些错误。。。我正在设法修理它。。谢谢你