1
public class AudioView extends Activity implements OnClickListener 

{

private MediaPlayer mp;

private ImageView play, pause, back, far;

private ProgressBar myProgressBar;

//private SeekBar seekbar;

// private int Screen_Id;

private boolean b = false;

private boolean status = false;

private boolean status1 = false;

private String url;

Intent frmHome;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.audio_main);
    frmHome = getIntent();
    getIntentValues();
    initUI();
    uiListener();
}

private void getIntentValues() {
    // TODO Auto-generated method stub
    //String id = frmHome.getStringExtra("Activity_Id");
    url = frmHome.getStringExtra("url");
    // Screen_Id = Integer.parseInt(id);
}

private void uiListener() {
    // TODO Auto-generated method stub
    play.setOnClickListener(this);
    pause.setOnClickListener(this);
    pause.setVisibility(android.view.View.INVISIBLE);
    back.setOnClickListener(this);
    far.setOnClickListener(this);
}

private void initUI() {
    // TODO Auto-generated method stub
    back = (ImageView) findViewById(R.id.back);
    play = (ImageView) findViewById(R.id.play);
    pause = (ImageView) findViewById(R.id.pause);
    far = (ImageView) findViewById(R.id.fwd);
    myProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
    myProgressBar.setProgress(0);
}
private Thread myThread = new Thread() {
    public void run() {
        // TODO Auto-generated method stub
        while (mp.getCurrentPosition() < mp.getDuration()) {
            try {
                // myHandle.sendMessage(myHandle.obtainMessage());
                myProgressBar.setProgress(mp.getCurrentPosition());
                // Thread.sleep(1000);
            } catch (Throwable t) {
            }
        }
    }
};

@Override
public void onAttachedToWindow() {
    //disabling the home button
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    this.finish();
}
@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    myThread.stop();
}
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
}
@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    mp = MediaPlayer.create(this,Uri.parse(FinalConstants.AUDIO_PRE_URL + url));
    myProgressBar.setMax(mp.getDuration());
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    myThread.stop();
    mp.stop();
}
public PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state,
            String incomingNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            // INCOMING call do all necessary action to pause
            // the audio
            if (mp.isPlaying()) {
                mp.pause();
                status1 = false;
            }
        } else if (state == TelephonyManager.CALL_STATE_IDLE) {
            // Not IN CALL
            // do anything if the phone-state is idle
            if (mp.isPlaying() == status1) {
                mp.start();
            } else {
                mp.start();
                status1 = true;
            }
        }
        if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
            // A call is dialing, active or on hold
            // do all necessary action to pause the audio
            // do something here
            if (mp.isPlaying()) {
                mp.pause();
            }
        }
        super.onCallStateChanged(state, incomingNumber);
    }
};// end PhoneStateListener
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.play:
        if (!b) {
            try {
                mp.prepareAsync();
            } catch (Exception e) {
                e.printStackTrace();
            }
            mp.start();
            myThread.start();
        } else {
            mp.getCurrentPosition();
            mp.start();
        }
        pause.setVisibility(android.view.View.VISIBLE);
        play.setVisibility(android.view.View.INVISIBLE);
        b = true;
        myProgressBar.setProgress(mp.getCurrentPosition());
        break;
    case R.id.pause:
        mp.pause();
        play.setVisibility(android.view.View.VISIBLE);
        pause.setVisibility(android.view.View.INVISIBLE);
        break;
    case R.id.back:
        int dur = mp.getCurrentPosition();
        int pos = (dur > 10000 ? dur - 5000 : 0);
        mp.seekTo(pos);
        break;
    case R.id.fwd:
        int curpos = mp.getCurrentPosition();
        int dur2 = mp.getDuration();
        int pos2 = (curpos + 5000 > dur2 ? dur2 : curpos + 5000);
        mp.seekTo(pos2);
        break;
    }
    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if (mgr != null) {
        mgr.listen(phoneStateListener,
                PhoneStateListener.LISTEN_CALL_STATE);
    }
}
}
4

0 回答 0