Reputation: 541
I'm develop a app that can play video from youtube, if user pressed home button, user can hear the audio and then they can back to see video again. So, i use Mediaplayer and setDisplay surfaceview to it to see video, but it can't keep player play when home pressed, i know Service can perform long-running operations in the background and does not provide a user interface. so, i create a service that hold 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;
}
}
And my activity bind on it to get service instance and get mediaplayer instance to setDisplay
surfaceview to it. But it seem like, i can setDisplay
to mediaplayer because service instance do not return before my activity leave onCreate()
acording to this
android-how-do-i-wait-until-a-service-is-actually-connected
but surface created imediately with activity, and i can't setDisplay
to mediaplayer to show video, how can i surpass this problem, here is my activity:
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) {
}
}
Upvotes: 3
Views: 4200
Reputation: 541
I just store surfaceHolder in surfaceCreated(SurfaceHolder surfaceHolder)
as a field of class and then use it when service created.
Sorry for a stupid question, but i think this code can be useful for anyone who want to use mediaplayer in service to play a audio or video
Upvotes: 1