从应用程序的起始点开始将phone_state作为空闲的应用程序。这不是必需的

时间:2013-01-11 11:57:42

标签: android streaming android-mediaplayer

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部分。所以任何人都可以建议我做什么,以便第一次app按设计运行,并且只有在应用程序已经运行时接收或进行任何调用时才会实现此PHONE_STATE_IDLE部分代码。 提前很多..

希望你们所有人都有我的意思..

0 个答案:

没有答案