在媒体播放器

时间:2017-06-12 05:38:25

标签: java android

我是一个绝对的初学者,在我的第一个Android应用程序上工作。我正在开发音频流应用程序,我在服务中实现了mediaPlayer。有一个播放暂停按钮,如果绑定与否则切换。我的问题是,当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;
        }

    }

1 个答案:

答案 0 :(得分: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();
     }
   }
}

并将android:src="@drawable/play"添加到xml文件中的imagebutton

相关问题