在服务中使用mediaplayer在后台播放视频

时间:2014-05-18 07:00:11

标签: android video service media-player

我正在开发一个可以播放来自youtube的视频的应用,如果用户按下主页按钮,用户可以听到音频,然后他们可以再次观看视频。 所以,我使用Mediaplayer和setDisplay surfaceview来观看视频,但是当按下主页时它不能保持玩家的游戏,我知道服务可以在后台执行长时间运行的操作而不提供用户界面。所以,我创建一个持有Mediaplayer的服务

public class VideoPlayerService extends Service implements MediaPlayer.OnPreparedListener {

    private static final String TAG = "VideoPlayerService";

    public MediaPlayer mPlayer;

    public static int position;

    private final IBinder mBinder = new LocalBinder();

    @Override
    public IBinder onBind(Intent intent) {
       Log.d(TAG, "service in onBind");
       return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mPlayer = new MediaPlayer();
        Log.d(TAG, "service created");
    }

    public class LocalBinder extends Binder {
        public VideoPlayerService getService() {
            return VideoPlayerService.this;
        }
    }

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {
        return 1;
    }


    public IBinder onUnBind(final Intent arg0) {
        return null;
    }
}

我的活动绑定它以获取服务实例并将媒体播放器实例发送到setDisplay surfaceview。但看起来,我可以setDisplay到媒体播放器,因为服务实例在我的活动离开onCreate()之前没有返回 android-how-do-i-wait-until-a-service-is-actually-connected

但是表面会立即通过活动创建,我无法setDisplay向媒体播放器显示视频,我怎样才能超越这个问题,这是我的活动:

public class VideoPlayerActivity extends Activity implements SurfaceHolder.Callback, MediaController.MediaPlayerControl, GetStreamUrlTest.IPlayerListener, MediaPlayer.OnPreparedListener {

static final String TAG = "VideoPlayerActivity";

public MediaController mMediaController;

public SurfaceView surfaceView;
public SurfaceHolder holder;

public VideoItem mVideoItem;

public int position = 0;

private VideoPlayerService mVideoPlayerService;

private boolean mBounded;

private ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        VideoPlayerService.LocalBinder localBinder = (VideoPlayerService.LocalBinder)
                iBinder;
        mVideoPlayerService = localBinder.getService();
        mBounded = true;
        Log.d(TAG, "Service connected");

    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        Log.e(TAG, "Service has unexpectedly disconnected");
        mBounded = false;
    }
};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videoplayer);

    Log.d(TAG, "in Oncreate");

    mMediaController = new MediaController(this);
    mMediaController.setMediaPlayer(this);
    mMediaController.setAnchorView(surfaceView);

    Intent intent = getIntent();
    mVideoItem = (VideoItem) intent.getParcelableExtra(ConstantFields.VIDEO_ITEM);

    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    holder = surfaceView.getHolder();
    holder.setFixedSize(800, 480);
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
protected void onStart() {
    super.onStart();
    Intent serviceIntent = new Intent(this, VideoPlayerService.class);
    bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onResume() {
    super.onResume();

}

@Override
protected void onStop() {
    super.onStop();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!mMediaController.isShowing()){
        mMediaController.show();
    } else {
        mMediaController.hide();
    }
    return false;
}

@Override
public void onBackPressed() {
    super.onBackPressed();
}

@Override
protected void onPause() {
    super.onPause();
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
public void doPlayVideo(VideoItem videoItem){
    Log.d(TAG,"in DO Play");
    mVideoPlayerService.mPlayer.reset();
    try {
        mVideoPlayerService.mPlayer.setDataSource(this, Uri.parse(videoItem.getStreamUrl()));

        mVideoPlayerService.mPlayer.prepareAsync();
        mVideoPlayerService.mPlayer.setOnPreparedListener(this);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    mVideoPlayerService.mPlayer.setDisplay(surfaceHolder);
    doPlay(mVideoItem)
    Log.d(TAG, "In surface Created");
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
}

1 个答案:

答案 0 :(得分:2)

我只是在surfaceCreated(SurfaceHolder surfaceHolder)中将surfaceHolder存储为类的字段,然后在创建服务时使用它。 很抱歉有一个愚蠢的问题,但我认为此代码对于想要在服务中使用媒体播放器播放音频或视频的任何人都很有用