title: Android mediarecorder doesn't record data when using libvlc
body: <p>I want to use the android MediaRecorder to record a remote url. I called a fragment from an acticity with an url, and LibVLC plays that.
  I check it's acticvity's onClick event from a Timer task. I want to start recording here. It works, it calls start, but the recording doesn't work.
  It creates the output file, but it has a 0 byte - size.</p>
  
  <p>I would be appreciated for any advice.</p>
  
  <pre><code>Public class VideoFragment extends Fragment implements SurfaceHolder.Callback, IVideoPlayer {
  
      public static final String TAG = "VideoFragment";
      public static final String VIDEO_URL = "VIDEO_URL";
  
  
      private String mFilePath, ip;
      private SurfaceView mSurface;
      private SurfaceHolder holder;
      private LibVLC libvlc;
      private int mVideoWidth;
      private int mVideoHeight;
      private final static int VideoSizeChanged = -1;
      private Context mContext;
      private EventHandler mEventHandler;
      private ViewGroup mContainer;
  
      boolean recording;
  
      MediaRecorder mediaRecorder;
  
      public static VideoFragment newInstance(String videoURL) {
          VideoFragment fragment = new VideoFragment();
          Bundle args = new Bundle();
          args.putString(VIDEO_URL, videoURL);
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          Log.d(TAG, "onCreateView");
  
          View view = View.inflate(
                  getActivity(), R.layout.fragment_video, null);
  
          mContainer = container;
          mContext = getActivity();
  
          mFilePath = this.getArguments().getString(VIDEO_URL);
          ip = mFilePath.substring(mFilePath.lastIndexOf("/") + 1, mFilePath.length());
          ip = ip.substring(0, ip.lastIndexOf(":") + 1);
  
  
          mSurface = (SurfaceView) view.findViewById(R.id.fragment_videoview_surface);
          holder = mSurface.getHolder();
          holder.addCallback(this);
  
  
          mediaRecorder = new MediaRecorder();
          initMediaRecorder();
  
          Timer myTimer = new Timer();
  
          myTimer.schedule(new TimerTask() {
              @Override
              public void run() {
                  // Your logic here...
  
                  // When you need to modify a UI element, do so on the UI thread.
                  // 'getActivity()' is required as this is being ran from a Fragment.
                  getActivity().runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          // This code will always run on the UI thread, therefore is safe to modify UI elements.
                          if (VideoActivity.startRecord) {
                              VideoActivity.startRecord = false;
                              Log.e("error", "start");
                              mediaRecorder.start();
                          }
                      }
                  });
              }
          }, 2000, 5000);
  
          return view;
      }
      public void createPlayer() {
          releasePlayer();
          try {
              String media = mFilePath;
  
  
              // Create a new media player
              libvlc = new LibVLC();
              mEventHandler = libvlc.getEventHandler();
              libvlc.init(mContext);
              libvlc.setHardwareAcceleration(LibVLC.HW_ACCELERATION_FULL);
              libvlc.setSubtitlesEncoding("");
              libvlc.setAout(LibVLC.AOUT_OPENSLES);
              libvlc.setTimeStretching(true);
              libvlc.setVerboseMode(true);
              libvlc.setNetworkCaching(1000);
              NativeCrashHandler.getInstance().setOnNativeCrashListener(
                      nativecrashListener);
              if (LibVlcUtil.isGingerbreadOrLater())
                  libvlc.setVout(LibVLC.VOUT_ANDROID_WINDOW);
              else
                  libvlc.setVout(LibVLC.VOUT_ANDROID_SURFACE);
              LibVLC.restartInstance(mContext);
              mEventHandler.addHandler(mHandler);
              holder.setKeepScreenOn(true);
              MediaList list = libvlc.getMediaList();
              list.clear();
              list.add(new Media(libvlc, LibVLC.PathToURI(media)), false);
              libvlc.playIndex(0);
              mute();
          } catch (Exception e) {
              Log.e(TAG, e.toString());
              Toast.makeText(mContext, "Error creating player!",
                      Toast.LENGTH_LONG).show();
          }
      }
  
  
      /**
       * **********
       * Events
       * ***********
       */
  
      private Handler mHandler = new MyHandler(this);
  
      private static class MyHandler extends Handler {
          private WeakReference&lt;VideoFragment&gt; mOwner;
  
          public MyHandler(VideoFragment owner) {
              mOwner = new WeakReference&lt;VideoFragment&gt;(owner);
          }
  
          @Override
          public void handleMessage(Message msg) {
              VideoFragment player = mOwner.get();
  
              // SamplePlayer events
              if (msg.what == VideoSizeChanged) {
                  player.setSize(msg.arg1, msg.arg2);
                  return;
              }
  
              // Libvlc events
              Bundle b = msg.getData();
              switch (b.getInt("event")) {
                  case EventHandler.MediaPlayerEndReached:
                      player.releasePlayer();
                      break;
                  case EventHandler.MediaPlayerPlaying:
                  case EventHandler.MediaPlayerPaused:
                  case EventHandler.MediaPlayerStopped:
                  default:
                      break;
              }
          }
      }
  
      public OnNativeCrashListener nativecrashListener = new OnNativeCrashListener() {
  
          @Override
          public void onNativeCrash() {
              Log.e("vlcdebug", "nativecrash");
          }
      };
  
      public void releasePlayer() {
          if (libvlc == null)
              return;
          mEventHandler.removeHandler(mHandler);
          libvlc.stop();
          libvlc.detachSurface();
          libvlc.closeAout();
          libvlc.destroy();
          libvlc = null;
  
          mVideoWidth = 0;
          mVideoHeight = 0;
      }
  
      private void setSize(int width, int height) {
          if (libvlc != null) {
              libvlc.closeAout();
              libvlc.setVolume(0);
          }
  
          // Dimensions of the native video
          mVideoWidth = width;
          mVideoHeight = height;
  
          if (mVideoWidth * mVideoHeight &lt;= 1)
              return;
  
          // Dimensions of the surface frame
          int surfaceFrameW = mContainer.getMeasuredWidth();
          int surfaceFrameH = mContainer.getMeasuredHeight();
  
          float videoAR = (float) mVideoWidth / (float) mVideoHeight;
          float surfaceFrameAr = (float) surfaceFrameW / (float) surfaceFrameH;
  
          int vidW = surfaceFrameW;
          int vidH = surfaceFrameH;
  
          if (surfaceFrameAr &lt; videoAR)
              vidH = (int) (surfaceFrameW / videoAR);
          else
              vidW = (int) (surfaceFrameH * videoAR);
  
          // force surface buffer size
          if (holder != null) {
              holder.setFixedSize(mVideoWidth, mVideoHeight);
  
          } else {
              Toast.makeText(mContext, "Holder was null!!!", Toast.LENGTH_SHORT).show();
          }
  
          // set display size
          LayoutParams lp = mSurface.getLayoutParams();
          lp.width = vidW;
          lp.height = vidH;
          mSurface.setLayoutParams(lp);
          mSurface.invalidate();
      }
  
      public void mute() {
          libvlc.closeAout();
          libvlc.setVolume(0);
      }
  
      @Override
  
      public void onResume() {
          Log.d(TAG, "onResume");
          super.onResume();
          createPlayer();
      }
  
      @Override
      public void onPause() {
          Log.d(TAG, "onPause");
          super.onPause();
          releasePlayer();
      }
  
      @Override
      public void onDestroy() {
          Log.d(TAG, "onDestroy");
          super.onDestroy();
          releasePlayer();
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
          prepareMediaRecorder();
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                 int height) {
          if (libvlc != null)
              libvlc.attachSurface(holder.getSurface(), this);
  
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
          // TODO Auto-generated method stub
  
      }
  
      public void eventHardwareAccelerationError() {
          Log.e(TAG, "Error with hardware acceleration");
          releasePlayer();
          Toast.makeText(mContext, "Error with hardware acceleration",
                  Toast.LENGTH_LONG).show();
      }
  
      @Override
      public void setSurfaceLayout(int width, int height, int visible_width,
                                   int visible_height, int sar_num, int sar_den) {
          Message msg = Message.obtain(mHandler, VideoSizeChanged, width,
                  height);
          msg.sendToTarget();
      }
  
      // Used only for old stuff
      @Override
      public int configureSurface(Surface surface, int width, int height,
                                  int hal) {
          Log.d("", "configureSurface: width = " + width + ", height = "
                  + height);
          if (LibVlcUtil.isICSOrLater() || surface == null)
              return -1;
          if (width * height == 0)
              return 0;
          if (hal != 0)
              holder.setFormat(hal);
          holder.setFixedSize(width, height);
          return 1;
      }
  
  
      private void initMediaRecorder() {
  
  
          SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH-mm-ss");
          String date = df.format(Calendar.getInstance().getTime());
  
  
          SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
          String StreamDirectory = settings.getString("StreamDirectory", "");
  
          mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
          mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
          CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
          mediaRecorder.setProfile(camcorderProfile_HQ);
          Log.e("error", "itt vagyok");
          mediaRecorder.setOutputFile(StreamDirectory + "/" + ip + date + "_video.mp4");
          mediaRecorder.setMaxDuration(5000); // Set max duration in milisecundum
          mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
      }
  
      private void prepareMediaRecorder() {
          Log.e("error", "prepare");
          mediaRecorder.setPreviewDisplay(holder.getSurface());
          try {
              mediaRecorder.prepare();
          } catch (IllegalStateException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  
  }
  </code></pre>