Android MediaPlayer在服务屏幕旋转中播放视频流

时间:2016-07-19 11:09:07

标签: android android-service android-mediaplayer android-service-binding

我想创建Activity with video player来播放在线流,使用MediaPlayer类和SurfaceView进行显示。我在单独的服务中创建MediaPlayer,因此在屏幕旋转之后,不必再次创建播放器并且不必连接到流。我的问题是我不知道怎么写Activity,所以我的服务在每次屏幕旋转后都不会启动。

我的代码如下,在onStart()中,我开始服务,但我不知道如何更改它,所以每次都没有开始。

public class VideoPlayerActivity extends Activity implements SurfaceHolder.Callback {
    private String path;
    private SurfaceHolder vidHolder;
    private SurfaceView vidSurface;
    private VideoService videoService;
    private Intent playIntent;
    private boolean videoBound = false;

    private ServiceConnection musicConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            VideoService.VideoBinder binder = (VideoService.VideoBinder) service;
            videoService = binder.getService();
            videoService.setUrl(path);
            videoBound = true;
            if (vidHolder != null && videoService.getMediaPlayer() != null) {
                videoService.getMediaPlayer().setDisplay(vidHolder);
                videoService.playVideo();
            }
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            videoBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        path = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4"; //TODO tmp
        playIntent = new Intent(this, VideoService.class);
        bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
        startService(playIntent);
        vidSurface = (SurfaceView) findViewById(R.id.surfView);
        vidHolder = vidSurface.getHolder();
        vidHolder.addCallback(VideoPlayerActivity.this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        if (videoService != null && videoService.getMediaPlayer() != null) {
            videoService.getMediaPlayer().setDisplay(vidHolder);
        } 
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        Log.d("ServiceConnection", "surfaceChanged  " + i + "   " + i1 + "   " + i2);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        Log.d("ServiceConnection", "surfaceDestroyed");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(musicConnection);
        stopService(playIntent);
        videoService = null;
    }
}

服务类:

public class VideoService extends Service implements OnPreparedListener {
    private MediaPlayer player;
    private String path;
    private final IBinder musicBind = new VideoBinder();

    @Override
    public void onCreate() {
        Log.d("VideoService", "onCreate");
        super.onCreate();
        player = new MediaPlayer();
        initMusicPlayer();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return musicBind;
    }

    @Override
    public boolean onUnbind(Intent intent){
        player.stop();
        player.release();
        return false;
    }

    public void initMusicPlayer() {
        player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setOnPreparedListener(this);
    }

    public void playVideo() {
        try {
            player.setDataSource(path);
            player.prepareAsync();
        } catch (IOException e) {}
    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mediaPlayer.start();
    }

    public void setUrl(String url) {
        path = url;
    }

    public MediaPlayer getMediaPlayer() {
        return player;
    }

    public class VideoBinder extends Binder {
        public VideoService getService() {
            return VideoService.this;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果新创建了活动,您可以检查savedInstanceState是否为仅启动服务

if (savedInstanceState == null) {
        startService...
}

或通过添加

自行处理屏幕旋转
android:configChanges="orientation|keyboardHidden|screenSize"

清单中的活动。像这样onCreate将不再被轮换调用。