我正在創建一個流媒體播放器,至今其工作正常。我可以設法開始和停止流式傳輸。當我按下主頁按鈕時,活動已關閉,但聲音仍在播放。我很好,問題是當我再次啓動應用程序並點擊停止按鈕時,對象爲空。這就是我的代碼:按下主屏幕按鈕後播放器變爲空Android
public class Main extends Activity {
private MediaPlayer player;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onPlayClick(View v) {
try {
player = new MediaPlayer();
player.setDataSource("*****");
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.i("In", "Prepared Listener");
player.start();
}
});
player.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onStopClick(View v) {
if (player != null && player.isPlaying()) {
player.stop();
player.release();
player = null;
}
}
}
有誰知道什麼可能是錯的這個簡單的代碼? 非常感謝 牛逼
我會看看它!謝謝@Femi – Thiago 2011-04-29 15:01:01