2017-06-12 7 views
-3

私は絶対初心者で、最初のアンドロイドアプリで作業しています。私は、オーディオストリームアプリケーションに取り組んでおり、私はサービス内でmediaPlayerを実装しました。バインドされているかどうかを切り替える再生一時停止ボタンがあります。私が持っている問題は、isPlayingと私は画面の向きを変更すると、サービスが続行されますが、一時停止ボタンではなく、再生ボタンを表示するアクティビティが再作成されます。私は一時停止ボタンを保持するために私が知っている、私は2つのメソッドをオーバーライドする必要が - onSaveInstanceStateとonRestoreInstanceStateしかし、私はmainActivityの状態を保持するために使用する正しいロジックを知っていない。 Plsは私の問題を解決しなかった例を示してくれました。メディア内のアクティビティ状態を保存する

これは、これは私のサービス

// 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ファイルにimagebuttonandroid:src="@drawable/play"を追加している私の主な活動

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); 


    } 

} 

です。

+0

ありがとうございました –

+0

私は問題があります。方向を変更した後でメディアプレーヤーを一時停止しようとすると、サービスが再開されます。これを回避する方法は何ですか? –

+0

オリエンテーションが変更されるたびに 'onStart'が呼び出されるからです。コードから 'onStart'メソッドを削除し、' onCreate'からサービスを開始してください。私は自分の答えを編集しました。それを試してみてください。 –

関連する問題