2017-06-12 146 views
-3

我是一名絕對的初學者,工作於我的第一個Android應用程序。我正在處理音頻流應用程序,並在服務中實現了mediaPlayer。有一個play-pause按鈕可以在綁定或不綁定時切換。我有的問題是,當isPlaying,我改變屏幕的方向,雖然服務繼續,活動重新創建,使播放按鈕,而不是暫停按鈕。我知道我保留暫停按鈕,我需要重寫兩個方法 - onSaveInstanceState和onRestoreInstanceState,但我不知道正確的邏輯來使用,以保留mainActivity的狀態。 請幫助我,因爲我看到的例子沒有解決我的問題。在媒體播放器中保存活動狀態播放器

這是我的主要活動

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //....... 
     if(savedInstanceState != null) { 
      boolean isPlaying = savedInstanceState.getBoolean("isPlaying"); 
      if(!isPlaying){ 
       imageButton2.setImageResource(R.drawable.stop); 
      } else { 
       imageButton2.setImageResource(R.drawable.play); 
      } 
     } 

    @Override 
    protected void onStart() { 
     // bind to the radio service 
     Intent intent = new Intent(this, RadioService.class); 
     startService(intent); 
     bindService(intent, mConnection, BIND_AUTO_CREATE); 
     super.onStart(); 
    } 

    @Override 
    protected void onResume() { 


     super.onResume(); 
    } 


    @Override 
    protected void onStop() { 
     // unbind the radio 
     // service 

     if (boundToRadioService) { 
      unbindService(mConnection); 
      boundToRadioService = false; 
     } 
     super.onStop(); 
    } 

    @Override 
    protected void onDestroy() { 
    radioService.hideNotification(); 

     if (boundToRadioService) { 
      unbindService(mConnection); 
      boundToRadioService = false; 
     } 
    super.onDestroy(); 
    } 

    public void setupMediaPlayer(View view) { 

     if (boundToRadioService) { 
      boundToRadioService = false; 
      imageButton2.setImageResource(R.drawable.stop); 
      radioService.play(); 
     } else { 
      boundToRadioService = true; 
      imageButton2.setImageResource(R.drawable.play); 
      radioService.pause(); 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
outState.putBoolean("isPlaying", boundToRadioService); 
     super.onSaveInstanceState(outState); 


    } 

} 

這是我的服務

// Binder given to clients 
    private final IBinder mBinder = new RadioBinder(); 
    } 

    public void pause() { 
     audioManager.abandonAudioFocus(audioFocusChangeListener); 
     unregisterReceiver(audioBecomingNoisy); 
     mediaPlayer.pause(); 
    } 

    @Override 
    public boolean onError(MediaPlayer mp, int what, int extra) { 
     Toast.makeText(this, "Oops! Error Occured, Check your network connection", Toast.LENGTH_SHORT).show(); 
     mediaPlayer.reset(); 
     return false; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return START_NOT_STICKY; 
    } 


    @Override 
    public void onPrepared(MediaPlayer mp) { 
     { 
      mp.start(); 
     } 

    } 



    public class RadioBinder extends Binder { 
     RadioService getService() { 
      // Return this instance of RadioBinder so clients can call public methods 
      return RadioService.this; 
     } 
    } 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 

     Log.d(TAG, "onBind: "); 

     return mBinder; 
    } 

    public void play() { 
     registerReceiver(audioBecomingNoisy, noisyIntentFilter); 
     mediaPlayer = new MediaPlayer(); 
     mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     new PlayerTask().execute(stream); 
     mediaPlayer.setOnPreparedListener(this); 
     mediaPlayer.setOnErrorListener(this); 
     mediaPlayer.reset(); 
     showNotification(); 
     Toast.makeText(this, "loading... please wait", Toast.LENGTH_LONG).show(); 
     Log.i(TAG, "onCreate, Thread name " + Thread.currentThread().getName()); 

    } 


    class PlayerTask extends AsyncTask<String, Void, Boolean> { 
     @Override 
     protected Boolean doInBackground(String... strings) { 
      try { 
       mediaPlayer.setDataSource(stream); 
       mediaPlayer.prepareAsync(); 
       prepared = true; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return prepared; 
     } 

    } 

回答

0

試試這個,

public class MainActivity extends AppCompatActivity { 

private boolean boundToRadioService = false; 
private ImageButton imageButton2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    imageButton2 = (ImageButton) findViewById(R.id.my_button); 
    if(savedInstanceState != null){ 
     boundToRadioService = savedInstanceState.getBoolean("isPlaying", false); 
     if(boundToRadioService){ 
      imageButton2.setImageResource(R.drawable.stop); 
     } else { 
      imageButton2.setImageResource(R.drawable.play); 
     } 
    } else { 
     // bind to the radio service 
     Intent intent = new Intent(this, RadioService.class); 
     startService(intent); 
     bindService(intent, mConnection, BIND_AUTO_CREATE); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    outState.putBoolean("isPlaying", boundToRadioService); 
    super.onSaveInstanceState(outState); 
} 
public void setupMediaPlayer(View view){ 
    if (boundToRadioService) { 
     boundToRadioService = false; 
     imageButton2.setImageResource(R.drawable.play); 
     radioService.pause(); 
    } else { 
     boundToRadioService = true; 
     imageButton2.setImageResource(R.drawable.stop); 
     radioService.play(); 
    } 
    } 
} 
在XML文件中

,並添加android:src="@drawable/play"imagebutton

+0

謝謝您對此的支持 –

+0

我遇到問題。在嘗試在改變方向後暫停媒體播放器時,服務會重新啓動。這是怎麼回事? –

+0

這是因爲每當方向改變時都會調用onStart。從你的代碼中刪除'onStart'方法,並從'onCreate'開始你的服務。我編輯了我的答案。嘗試一下。 –