Thursday, May 15, 2014

Control VLC Player using Android (Wifi Connected)

    My first application of Android comes here.In my first tutorial on Controlling VLC using JAVA you can see that the VLC provides a Web interface from which it can be controlled and it also accepts certain commands.So that can be used to control the media player in Local Area Network using any programming language.This time its Android

Code:
MainActivity.Java


package com.example.first_app;
import java.net.Authenticator;
import java.net.URL;


import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
 @SuppressLint("NewApi")
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setDetectEnabled(true);
        final TextView tv1 = (TextView) findViewById(R.id.textView1);
        tv1.setText("Bikash");
    tv1.setTextColor(Color.BLACK);
    
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy); 
          
          final Button Prev = (Button) findViewById(R.id.button1);
          final Button Play = (Button) findViewById(R.id.Button01);
          final Button Next = (Button) findViewById(R.id.Button02);
          final Button TenS = (Button) findViewById(R.id.button2);
          final Button OMin = (Button) findViewById(R.id.button4);
          final Button MTS = (Button) findViewById(R.id.button3);
          final SeekBar volume = (SeekBar) findViewById(R.id.seekBar1);
          //volume.get
          
          TenS.setOnClickListener(new View.OnClickListener(){
        @Override
          public void onClick(View v) {
           try {
             String urlString = "http://192.168.0.10:8080/requests/status.xml?command=seek&val=+20";
                   Authenticator.setDefault(new authoRize());
                URL url = new URL(urlString);
                url.getContent();
     } catch (Exception e) {
      tv1.setText(e.toString());
      tv1.setTextColor(Color.BLACK);
     }
            
          }
         
      });          
          OMin.setOnClickListener(new View.OnClickListener(){
        @Override
          public void onClick(View v) {
           try {
             String urlString = "http://192.168.0.10:8080/requests/status.xml?command=seek&val=+60";
                   Authenticator.setDefault(new authoRize());
                URL url = new URL(urlString);
                url.getContent();
     } catch (Exception e) {
      tv1.setText(e.toString());
      tv1.setTextColor(Color.BLACK);
     }
            
          }
         
      });          
          MTS.setOnClickListener(new View.OnClickListener(){
        @Override
          public void onClick(View v) {
           try {
             String urlString = "http://192.168.0.10:8080/requests/status.xml?command=seek&val=-20";
                   Authenticator.setDefault(new authoRize());
                URL url = new URL(urlString);
                url.getContent();
     } catch (Exception e) {
      tv1.setText(e.toString());
      tv1.setTextColor(Color.BLACK);
     }
            
          }
         
      });       
          Prev.setOnClickListener(new View.OnClickListener(){
        @Override
          public void onClick(View v) {
           try {
             String urlString = "http://192.168.0.10:8080/requests/status.xml?command=pl_previous";
                   Authenticator.setDefault(new authoRize());
                URL url = new URL(urlString);
                url.getContent();
     } catch (Exception e) {
      tv1.setText(e.toString());
      tv1.setTextColor(Color.BLACK);
     }
            
          }
         
      });
          Play.setOnClickListener(new View.OnClickListener(){
        @Override
          public void onClick(View v) {
           try {
             String urlString = "http://192.168.0.10:8080/requests/status.xml?command=pl_pause";
                   Authenticator.setDefault(new authoRize());
                URL url = new URL(urlString);
                url.getContent();
     } catch (Exception e) {
      tv1.setText(e.toString());
      tv1.setTextColor(Color.BLACK);
     }
            
          }
         
      });
          Next.setOnClickListener(new View.OnClickListener(){
        @Override
          public void onClick(View v) {
           try {
             String urlString = "http://192.168.0.10:8080/requests/status.xml?command=pl_next";
                   Authenticator.setDefault(new authoRize());
                URL url = new URL(urlString);
                url.getContent();
     } catch (Exception e) {
      tv1.setText(e.toString());
      tv1.setTextColor(Color.BLACK);
     }
            
          }
         
      });
      volume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            int progress = 0;
             @Override
   public void onProgressChanged(SeekBar seekBar,int progresValue, boolean fromUser) 
             {
     progress = progresValue;
     try {
            String urlString = "http://192.168.0.10:8080/requests/status.xml?command=volume&val="+5*progress;
                  Authenticator.setDefault(new authoRize());
               URL url = new URL(urlString);
               url.getContent();
    } catch (Exception e) {
     tv1.setText(e.toString());
     tv1.setTextColor(Color.BLACK);
    }
     
    }
   
   @Override
   public void onStartTrackingTouch(SeekBar seekBar) {}
   @Override
   public void onStopTrackingTouch(SeekBar seekBar) {
     tv1.setText(progress + "/" + seekBar.getMax());
       }
  });
          
 }


    @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;
    }
    private void setDetectEnabled(boolean enable) {
        Intent intent = new Intent(this, CallDetectService.class);
            startService(intent);
            
    }
 
}




CallHelper.Java
package com.example.first_app;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class CallHelper {

 /**
  * Listener to detect incoming calls. 
  */
 private class CallStateListener extends PhoneStateListener {
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
   switch (state) {
   case TelephonyManager.CALL_STATE_RINGING:
    try {               
               HttpClient client = new DefaultHttpClient();
               HttpGet httpget = new HttpGet("http://192.168.0.10/API/mobileControl.php?NO="+incomingNumber+"&STATE=YES");
               ResponseHandler<String> responseHandler = new BasicResponseHandler();
              String SetServerString=client.execute(httpget,responseHandler);
    } catch (Exception e) { } 
    break;
   }
  }
 }
 


 private Context ctx;
 private TelephonyManager tm;
 private CallStateListener callStateListener;
 public CallHelper(Context ctx) {
  this.ctx = ctx;
  
  callStateListener = new CallStateListener();
 }
 
 /**
  * Start calls detection.
  */
 public void start() {
  tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
  tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
  
 }
 
 /**
  * Stop calls detection.
  */
 public void stop() {
  tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
 }

}


CallDetectService.Java
package com.example.first_app;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class CallDetectService extends Service {
 private CallHelper callHelper;
 
    public CallDetectService() {
    }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  callHelper = new CallHelper(this);
  
  int res = super.onStartCommand(intent, flags, startId);
  callHelper.start();
  return res;
 }
 
    @Override
 public void onDestroy() {
  super.onDestroy();
  
  callHelper.stop();
 }

 @Override
    public IBinder onBind(Intent intent) {
  // not supporting binding
     return null;
    }
}


authoRize.Java

package com.example.first_app;

import java.net.Authenticator;

import java.net.PasswordAuthentication;



class authoRize extends Authenticator {

    private String username, password;



    public authoRize() {

      username = "";

      password = "*****";

    }

    protected PasswordAuthentication getPasswordAuthentication() {

      return new PasswordAuthentication(username, password.toCharArray());

    }

  }



NOTE: Download complete code Here