0
private ImageButton malayalam, hindi, tamil;

private MediaPlayer mp = null;
ImageButton pause;
ImageView facebook, twitter, linkedin;
private String TAG = "debug";
boolean playVideo = false, pauseVideo = true, isVideoPlaying = false, streaming = true;
TextView stream, events;
AudioManager audioManager;
ProgressBar progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);


    progress = (ProgressBar)findViewById(R.id.progress);
    facebook = (ImageView)findViewById(R.id.facebook);
    twitter = (ImageView)findViewById(R.id.twitter);
    linkedin = (ImageView)findViewById(R.id.linkedin);
    pause = (ImageButton)findViewById(R.id.pause);
    pause.setVisibility(View.GONE);
    stream = (TextView)findViewById(R.id.textview2);
    events = (TextView)findViewById(R.id.events);
    playFm();
    ;


    audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    SeekBar volControl = (SeekBar)findViewById(R.id.seekbar);
    volControl.setMax(maxVolume);
    volControl.setProgress(curVolume);
    volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            // TODO Auto-generated method stub
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub

        }

    });

    pause.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (playVideo) {

                playVideo = false;
                pauseVideo = true;
                isVideoPlaying = true;
                pause.setVisibility(View.VISIBLE);
                onPrepared(mp);
            } else {

                pauseFm();
                playVideo = true;
                pauseVideo = false;
                isVideoPlaying = false;
                pause.setVisibility(View.VISIBLE);
                Toast.makeText(MyTuneIndia.this, "paused", Toast.LENGTH_SHORT).show();
                pause.setBackgroundResource(R.drawable.play);
            }


        }
    });


    PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                // do all necessary action to pause the audio
                if (mp != null) {// check mp
                    if (mp.isPlaying()) {
                        Toast.makeText(MyTuneIndia.this, "Incoming call..!!.",
                                Toast.LENGTH_SHORT).show();
                        pauseFm();
                        playVideo = true;
                        pauseVideo = false;
                        isVideoPlaying = false;
                        pause.setVisibility(View.VISIBLE);
                        Toast.makeText(MyTuneIndia.this, "paused", Toast.LENGTH_SHORT).show();
                        pause.setBackgroundResource(R.drawable.play);

                    }
                }
                // Incoming call: Pause music
            } else if (state == TelephonyManager.CALL_STATE_IDLE) {
                Toast.makeText(MyTuneIndia.this, "Playing Again..!!", Toast.LENGTH_SHORT)
                        .show();
                playVideo = false;
                pauseVideo = true;
                isVideoPlaying = true;
                pause.setVisibility(View.VISIBLE);
                onPrepared(mp);
            } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                Toast.makeText(MyTuneIndia.this, "Outgoing Call..!!", Toast.LENGTH_SHORT)
                        .show();
                pauseFm();
                playVideo = true;
                pauseVideo = false;
                isVideoPlaying = false;
                pause.setVisibility(View.VISIBLE);
                Toast.makeText(MyTuneIndia.this, "paused", Toast.LENGTH_SHORT).show();
                pause.setBackgroundResource(R.drawable.play);

            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };// end PhoneStateListener

    TelephonyManager mgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
    if (mgr != null) {
        mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}


public void playFm() {
    Uri myUri = Uri.parse("http://xxx.mp3");
    try {
        if (mp == null) {
            this.mp = new MediaPlayer();
        } else {
            mp.stop();
            mp.reset();
        }
        mp.setDataSource(this, myUri); // Go to Initialized state
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setOnPreparedListener(this);
        mp.setOnBufferingUpdateListener(this);

        mp.setOnErrorListener(this);
        mp.prepareAsync();

        Log.d(TAG, "LoadClip Done");
    } catch (Throwable t) {
        Log.d(TAG, t.toString());
    }

}

public void pauseFm() {

    mp.pause();
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    // TODO Auto-generated method stub

}

@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onPrepared(MediaPlayer arg0) {
    // TODO Auto-generated method stub
    pause.setBackgroundResource(R.drawable.pause);
    pause.setVisibility(View.VISIBLE);
    stream.setVisibility(View.GONE);
    progress.setVisibility(View.GONE);
    streaming = false;
    isVideoPlaying = true;
    if (isVideoPlaying)
        mp.start();
}

@Override
public void onCompletion(MediaPlayer mp) {
    // TODO Auto-generated method stub

}


public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onBackPressed() {
    if (isVideoPlaying) {
        mp.stop();
        finish();
    } else if (streaming) {
        Toast.makeText(MyTuneIndia.this, "Streaming stopped..", Toast.LENGTH_SHORT).show();
        mp.reset();
        mp.release();
        finish();
    } else {
        finish();
    }
}

这是我用于流式传输音频和播放的在线音频播放器的代码。现在的问题是它应该在应用程序刚刚启动时运行代码。但它正在这样做。当我从启动器启动应用程序时,它会说“再次播放”,这不在我的主代码中,而是这PHONE_STATE_IDLE部分代码。因此,任何人都可以建议我该怎么做,以便应用程序第一次按设计运行,并且这PHONE_STATE_IDLE部分代码仅在应用程序已经运行时收到或发出任何呼叫时实现。提前感谢很多..

希望你们都明白我的意思..

4

0 回答 0