+ \r
+ @Override\r
+ public boolean onTouch(View v, MotionEvent event) {\r
+ if (v == mPreview && event.getAction() == MotionEvent.ACTION_DOWN && mFile != null && mFile.isDown() && mFile.isAudio()) {\r
+ if (!mMediaServiceBinder.isPlaying(mFile)) {\r
+ Log.d(TAG, "starting playback of " + mFile.getStoragePath());\r
+ mMediaServiceBinder.start(mAccount, mFile);\r
+ // this is a patch; need to synchronize this with the onPrepared() coming from MediaPlayer in the MediaService\r
+ mMediaController.postDelayed(new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ mMediaController.show(0);\r
+ }\r
+ } , 300);\r
+ } else {\r
+ mMediaController.show(0);\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ \r
+ private void bindMediaService() {\r
+ Log.d(TAG, "Binding to MediaService...");\r
+ if (mMediaServiceConnection == null) {\r
+ mMediaServiceConnection = new MediaServiceConnection();\r
+ }\r
+ getActivity().bindService( new Intent(getActivity(), \r
+ MediaService.class),\r
+ mMediaServiceConnection, \r
+ Context.BIND_AUTO_CREATE);\r
+ }\r
+ \r
+ /** Defines callbacks for service binding, passed to bindService() */\r
+ private class MediaServiceConnection implements ServiceConnection {\r
+\r
+ @Override\r
+ public void onServiceConnected(ComponentName component, IBinder service) {\r
+ if (component.equals(new ComponentName(getActivity(), MediaService.class))) {\r
+ Log.d(TAG, "Media service connected");\r
+ mMediaServiceBinder = (MediaServiceBinder) service;\r
+ if (mMediaServiceBinder != null) {\r
+ if (mMediaController == null) {\r
+ mMediaController = new MediaController(getSherlockActivity());\r
+ }\r
+ mMediaController.setMediaPlayer(mMediaServiceBinder);\r
+ mMediaController.setAnchorView(mPreview);\r
+ mMediaController.setEnabled(true);\r
+ \r
+ Log.d(TAG, "Successfully bound to MediaService, MediaController ready");\r
+ \r
+ } else {\r
+ Log.e(TAG, "Unexpected response from MediaService while binding");\r
+ }\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public void onServiceDisconnected(ComponentName component) {\r
+ if (component.equals(new ComponentName(getActivity(), MediaService.class))) {\r
+ Log.d(TAG, "Media service suddenly disconnected");\r
+ if (mMediaController != null) {\r
+ mMediaController.hide();\r
+ mMediaController.setMediaPlayer(null); // TODO check this is not an error\r
+ mMediaController = null;\r
+ }\r
+ mMediaServiceBinder = null;\r
+ mMediaServiceConnection = null;\r
+ }\r
+ }\r
+ } \r
+\r
+\r