title: Play encrypted video by libmedia in android
body: <p>I am playing a encrypted mp4 video file from sdcard. for this what i do .....</p>
  
  <p><strong>STEP 1.</strong> i copy file from sdcard in internal memory and i write that file in world readable and world writeable format so that that i could Play</p>
  
  <p><strong>STEP 2.</strong> I decrypt that file in a temprary folder</p>
  
  <p><strong>STEP 3.</strong> now i play this , file plays no problem</p>
  
  <p>Problem is this process takes time , because file is large about 25 mb , and this process ( copying and decryption ) takes about 2 minutes time</p>
  
  <p>after some reading i found a solution of this problem i could reduce time by using 
  libmedia library , but i am facing some problem 
   when i play this by using libmedia library 
  error comes in log cat</p>
  
  <pre><code>10-10 06:33:42.908: E/MediaPlayer(9249): error (1, -2147483648)
  </code></pre>
  
  <p>on Activity screen i got a message in dialog 
  "can't play video"</p>
  
  <p>libmedia library link is <a href="http://libeasy.alwaysdata.net/" rel="nofollow">http://libeasy.alwaysdata.net/</a></p>
  
  <p>thanks for your valuable suggestion and help</p>
  
  <p>my activity code is </p>
  
  <pre><code>package com.example.playvideo2;
  
  import java.io.IOException;
  import java.security.InvalidKeyException;
  import java.security.NoSuchAlgorithmException;
  
  import javax.crypto.Cipher;
  import javax.crypto.KeyGenerator;
  import javax.crypto.NoSuchPaddingException;
  import javax.crypto.SecretKey;
  
  import fr.maxcom.http.LocalSingleHttpServer;
  import fr.maxcom.libmedia.Licensing;
  import android.media.MediaPlayer;
  import android.media.MediaPlayer.OnCompletionListener;
  import android.os.Bundle;
  import android.os.Environment;
  import android.app.Activity;
  import android.view.Menu;
  import android.widget.MediaController;
  import android.widget.VideoView;
  
  
  public class MainActivity extends Activity implements OnCompletionListener 
  {
      LocalSingleHttpServer mServer;
      VideoView mVideoView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.video_view);
  
          Licensing.allow(this);
          playENCVideo(Environment.getExternalStorageDirectory()
                  + "/encVideo.mp4");
      }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  public void playENCVideo(String path) {
  
      try {
          Cipher decipher = null;
  
          KeyGenerator kgen = KeyGenerator.getInstance("AES");
          SecretKey skey = kgen.generateKey();
  
          decipher = Cipher.getInstance("AES");
  
          decipher.init(Cipher.DECRYPT_MODE, skey);
  
          mServer = new LocalSingleHttpServer();
  
          mServer.setCipher(decipher);
          mServer.start();
  
          path = mServer.getURL(path);
  
          mVideoView = (VideoView) findViewById(R.id.videoView1);
          mVideoView.setVideoPath(path);
  
          // mVideoView.setMediaController(new MediaController(this));
          // mVideoView.requestFocus();
          mVideoView.start();
      } catch (InvalidKeyException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (NoSuchAlgorithmException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (NoSuchPaddingException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  @Override
  public void onCompletion(MediaPlayer mp) {
      // MediaPlayer.OnCompletionListener interface
      mServer.stop();
  
  }
  
  }
  </code></pre>
  
  <p>my manifest file is </p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.playvideo2"
      android:versionCode="1"
      android:versionName="1.0" 
      &gt;
  
  &lt;uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="18" /&gt;
  
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
      &lt;activity
          android:name="com.example.playvideo2.MainActivity"
          android:label="@string/app_name" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;meta-data
          android:name="fr.maxcom.libmedia.apiKey"
          android:value="qV1T3Zf+IyCpb1jomsomS3NUXBk7LdDaV31DcwmVIoMpvWWK8xAQIl1RUVWQegSt2jJbwH+cleik7YCH2/chP1JWdhpSfgMaNRl3fDMOez8mGn98Jhp7KyAffjc5RA==" /&gt;
  &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>This is that code which i used for encryption and decryption</p>
  
  <pre><code>        FileInputStream fis = new FileInputStream(new File("D:/QLurnVideo/inputVideo.mp4"));
          File outfile = new File("D:/QLurnVideo/encVideo.mp4");
          int read;
          if(!outfile.exists())
              outfile.createNewFile();
          File decfile = new File("D:/QLurnVideo/decVideo.mp4");
          if(!decfile.exists())
              decfile.createNewFile();
          FileOutputStream fos = new FileOutputStream(outfile);
          FileInputStream encfis = new FileInputStream(outfile);
          FileOutputStream decfos = new FileOutputStream(decfile);
          Cipher encipher = Cipher.getInstance("AES");
          Cipher decipher = Cipher.getInstance("AES");
          KeyGenerator kgen = KeyGenerator.getInstance("AES");
  
          SecretKey skey = kgen.generateKey();
          //Lgo
          encipher.init(Cipher.ENCRYPT_MODE, skey);
          CipherInputStream cis = new CipherInputStream(fis, encipher);
          decipher.init(Cipher.DECRYPT_MODE, skey);
          CipherOutputStream cos = new CipherOutputStream(decfos,decipher);
          while((read = cis.read())!=-1)
                  {
                      fos.write((char)read);
                      fos.flush();
                  }   
          fos.close();
          while((read=encfis.read())!=-1)
          {
              cos.write(read);
              cos.flush();
          }
  cos.close();
  </code></pre>