当我在暂停后播放流时,我为收音机制作了一个应用程序,从我暂停的最后一个点继续播放流,但是我需要停止播放,然后继续播放最新的播放。
包radiofm.arabelradio;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import java.io.IOException;
import static android.media.AudioManager.STREAM_MUSIC;
public class MainActivity extends AppCompatActivity {
ImageButton id_play;
MediaPlayer mediaPlayer;
boolean prepared = false;
boolean started =false;
String stream ="http://arabelfm.ice.infomaniak.ch/arabelprodcastfm.mp3";
//main_page contain arabel_photo player for radio
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/////////////////////////////////////////////////////////////////////////////
id_play = (ImageButton) findViewById(R.id.id_play);
id_play.setEnabled(false);
//id_play.setText("LOADING");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
new PlayerTask().execute(stream);
id_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (started){
started=false;
mediaPlayer.pause();
id_play.setImageResource(R.drawable.play);
// id_play.setText("PLAY");
}
else {
started=true;
mediaPlayer.start();
id_play.setImageResource(R.drawable.pause);
//id_play.setText("PAUSE");
}
}
});
//main_page contain arabel_photo player for radio//////////////////////////:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle=new ActionBarDrawerToggle(this,mDrawerLayout, R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar() .setDisplayHomeAsUpEnabled(true);
getSupportActionBar() .setHomeButtonEnabled(true);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
class PlayerTask extends AsyncTask<String,Void,Boolean>{
@Override
protected Boolean doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings[0] );
mediaPlayer.prepare();
prepared=true;
} catch (IOException e) {
e.printStackTrace();
}
return prepared;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
id_play.setEnabled(true);
id_play.setImageResource(R.drawable.play);
// id_play.setText("PLAY");
}
}
@Override
protected void onPause() {
super.onPause();
if(started){
mediaPlayer.pause();
}
}
@Override
protected void onResume() {
super.onResume();
if(started){
mediaPlayer.start();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(prepared){
mediaPlayer.release();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//main_page contain arabel_photo player for radio//////////////////////////:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
}