1
我對Java相當陌生,並且一直在開發一個應用程序來從互聯網傳輸實時音頻。除了暫停按鈕外,我已經完成了所有工作。現在它所做的只是在短時間內對音頻進行加擾。我猜這跟mediaPlayer.pause()
被調用的地方有關,但我可能是錯的。感謝您的時間和迴應。暫停按鈕只能在短時間內加擾音頻
package com.example.jacob.wutk;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import java.io.IOException;
public class radio extends AppCompatActivity {
/** Called when the user touches the button */
public void playPauseMusic (View playPause) throws IOException {
String url = "http://streamer.cci.utk.edu:8000/wutk-vorbis"; // your URL here
final MediaPlayer mediaPlayer = new MediaPlayer();
ImageButton imb = (ImageButton) findViewById(R.id.playPause);
if (mediaPlayer.isPlaying()) {
imb.setImageResource(R.drawable.play1);
mediaPlayer.pause();
return;
} else {
imb.setImageResource(R.drawable.pause1);
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer){
mediaPlayer.start();
}
});
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
}
}
它的偉大工程。我會接受它,但我確實有兩個要求。 1.)您是否可以在MediaPlayer無法啓動的地方播放,直到按下playPause? 2.)我是Java新手,你能解釋一下你在這裏做了什麼嗎? – McLemore
我編輯我的問題。現在來解釋一下: 我創建了一個布爾參數來檢查MediaPlayer是否準備好了(準備開始),並且在我們創建這個ImageButton(imb)之後,我們設置了一個onClickListener(https://developer.android.com/ reference/android/view/View.OnClickListener.html),並告訴偵聽器,如果我們沒有預付MediaPlayer,開始準備它並將布爾參數設置爲true(以知道MediaPlayer已準備好)。然後每次點擊ImageButton(imb)時,我們都會進入其他功能(檢查播放器是暫停啓動還是停止播放) –