我正在制作一个收音机应用程序,该应用程序已经完成了。但我想让用户通过点击为每个电台指定的按钮来更改电台。
更新:我输入了更多代码。
所以我在考虑这样的事情?但我不知道该把它放在哪里。
public void onClickListener (View v) {
if (v == rDuhok) {
kRadio.setDataSource("http://87.106.215.231:9992");
} else if (v == rVin) {
kRadio.setDataSource("http://93.91.197.214:1234");
}
}
现在我有了显示 MyURI 的电台的链接,应用程序按预期工作。但我希望能够通过单击按钮来更改URI。
public class KurdRadioActivity extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private Button rDuhok;
private Button rVin;
private MediaPlayer kRadio;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
rDuhok = (Button) findViewById(R.id.rDuhok);
rDuhok.setOnClickListener(this);
rVin = (Button) findViewById(R.id.rVin);
rVin.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setVisibility(View.GONE);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(50);
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setVisibility(View.VISIBLE);
buttonPlay.setVisibility(View.GONE);
playSeekBar.setVisibility(View.VISIBLE);
kRadio.prepareAsync();
kRadio.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
playSeekBar.setVisibility(View.INVISIBLE);
kRadio.start();
}
});
}
private void stopPlaying() {
if (kRadio.isPlaying()) {
kRadio.stop();
kRadio.release();
initializeMediaPlayer();
}
buttonPlay.setVisibility(View.VISIBLE);
buttonStopPlay.setVisibility(View.GONE);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() {
kRadio = new MediaPlayer();
try {
kRadio.setDataSource("http://87.106.215.231:9992");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
kRadio.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
@Override
protected void onPause() {
super.onPause();
if (this.isFinishing()) {
kRadio.stop();
}
}
我想说的是我在编程时的新功能,我只是在学习,谢谢你的帮助。