Demo entry 6352235

java

   

Submitted by anonymous on Mar 24, 2017 at 07:55
Language: Java. Code size: 3.5 kB.

class MusicService extends Service {
    private AudioManager mAm;
    private boolean isPlaymusic;
    private String url;
    private MediaPlayer mediaPlayer;
    @Override
    public void onCreate() {
        super.onCreate();
        mAm = (AudioManager) getSystemService(AUDIO_SERVICE);//获取AudioManager
    }
    @Override
    public void onStart(Intent intent, int startId) {
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                isPlaymusic = bundle.getBoolean("isPlay", true);
                url = bundle.getString("url");
                if (isPlaymusic)
                    play();
                else
                    stop();
            }
        }
    }
    AudioManager.OnAudioFocusChangeListener afChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                // Pause playback 短暂失去焦点
                pause();
            } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                // Resume playback 短暂失去焦点之后重获焦点
                resume();
            } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                //永久失去焦点
                // mAm.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
                mAm.abandonAudioFocus(afChangeListener);
                // Stop playback
                stop();
            }
        }
    };
    private boolean requestFocus() {
        // Request audio focus for playback 请求音频焦点
        int result = mAm.requestAudioFocus(afChangeListener,
                // Use the music stream.
                AudioManager.STREAM_MUSIC,
                // Request permanent focus.
                AudioManager.AUDIOFOCUS_GAIN);
        return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
    }
    private void resume() {
        if (mediaPlayer != null) {
            mediaPlayer.start();
        }
    }
    private void pause() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }
    MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer player) {
            if(!player.isLooping()){
                mAm.abandonAudioFocus(afChangeListener);
            }
        }
    };
    private void play() {
        if (requestFocus()) {
            if (mediaPlayer == null) {
                try {
                    mediaPlayer = new MediaPlayer();
                    mediaPlayer.setDataSource(url);
                    mediaPlayer.prepare();
                    mediaPlayer.setOnCompletionListener(completionListener);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer.start();
            }
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null)
            mediaPlayer.release();
    }
    private void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
        }
    }
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).