Sunday, November 4, 2012

Java based Winamp Controller for Android

Here is a small Android based application which controls Winamp

It is certainly Not of production quality - this is meant for demo purposes only :-)

Code is available from : https://github.com/giridharmb

File:PlayActivity.java

package com.giri.macplayercontrol;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.view.Menu;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.giri.player.NewClient;

import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class PlayActivity extends Activity {

 private List mylist = new ArrayList();
 private ListView list;
 private int selectedItem = 0;
 private NewClient newClient = null;

 private void show(int value) {
  Toast.makeText(PlayActivity.this, String.valueOf(value), Toast.LENGTH_SHORT).show();
 }


 private String fetchIPAddrFromTextBox() {
  String myStr = null;
  EditText editText = (EditText) findViewById(R.id.editText1);
  myStr = editText.getText().toString();
  return myStr;
 }

 private void createListView() {
  newClient = new NewClient();

  // setupButtonHandler();

  final EditText myEditText = (EditText) findViewById(R.id.editText1);
  Button btn = (Button) findViewById(R.id.button1);
  btn.setOnClickListener(new View.OnClickListener() {

   public void onClick(View v) {
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
    
    String ip = fetchIPAddrFromTextBox();

    if (ip == null)
     return;

    newClient.setSERVER_IP(ip);
    
    LinkedList ll = new LinkedList();
    ll = newClient.fetchPlayListLinkedList();
    
    mylist = new ArrayList();
      
    list = (ListView) findViewById(R.id.listView1);
      
    for (int i = 0; i < ll.size(); i++) {
     mylist.add(ll.get(i));
    }
    
    list.setAdapter(new ArrayAdapter(PlayActivity.this,android.R.layout.simple_list_item_1, mylist));
   }
  });
 
  list.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3) {
    show(arg2);
    newClient.playIndex(arg2);
   }
  });
 }

 @Override
 public void onBackPressed() {
     moveTaskToBack(true);
 }
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_play);
  
  list = (ListView) findViewById(R.id.listView1);

  createListView();

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_play, menu);
  return true;
 }
}

File:AllSongsObject.java

package com.giri.player;

import java.io.Serializable;
import java.util.LinkedList;

public class AllSongsObject implements Serializable {
 
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 private LinkedList _ll = new LinkedList();
 
 public AllSongsObject(LinkedList lls) {
  _ll = lls;
 }

 public LinkedList get_ll() {
  return _ll;
 }

 public void set_ll(LinkedList _ll) {
  this._ll = _ll;
 }
 
}

File: Constants.java

package com.giri.player;

public class Constants {
 public static String PLAY = "COMMAND_PLAY";
 public static String STOP = "COMMAND_STOP";
 public static String PLAYRANDOM = "COMMAND_PRANDOM";
 public static String VOLUMEUP = "COMMAND_VOLUMEUP";
 public static String VOLUMEDOWN = "COMMAND_VOLUMEDOWN";
 public static String VOLUMESET = "COMMAND_VOLUMESET@";
 public static String GETPLAYLIST = "COMMAND_GETPLAYLIST";
 public static String PLAYINDEX = "COMMAND_PLAYINDEX@";
 public static String LOOP = "COMMAND_LOOP";
 public static String DONTLOOP = "COMMAND_DONTLOOP";
}

File: Message.java

package com.giri.player;


import java.io.Serializable;

public class Message implements Serializable{

    /**
  * 
  */
 private static final long serialVersionUID = 1L;
 
 private String message;

    public Message(String message) {
        this.message = message;
    }

    public String getMessage(){
        return message;
    }
}

File: NewClient.java

package com.giri.player;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.LinkedList;

import com.giri.player.AllSongsObject;
import com.giri.player.Constants;
import com.giri.player.Message;

public class NewClient {

    private  String SERVER_IP = "192.168.0.89";
    private  int SERVER_PORT = 8081;
    private  String message = null;
    private static Socket socket = null;

    private LinkedList<String> playList = new LinkedList<String>();
 
    public NewClient() {
     
    }
    
   public NewClient(String msg) {
     message = msg;
    }

    public String getSERVER_IP() {
  return SERVER_IP;
 }
 public void setSERVER_IP(String sERVER_IP) {
  SERVER_IP = sERVER_IP;
 }
 public int getSERVER_PORT() {
  return SERVER_PORT;
 }
 public void setSERVER_PORT(int sERVER_PORT) {
  SERVER_PORT = sERVER_PORT;
 }
 public LinkedList<String> getPlayList() {
  return playList;
 }
 public void setPlayList(LinkedList<String> playList) {
  this.playList = playList;
 }
   
    public String getMessage() {
  return message;
 }
 public void setMessage(String message) {
  this.message = message;
 }
 
 public void playIndex(int value) {
  
  Object obj = null;
        
        try
        {
         //if(socket!=null) {
         // if(!socket.isClosed()) {
         //  socket.close();
         // }
         //}
            socket = new Socket(SERVER_IP, SERVER_PORT);
            socket.setReuseAddress(true);
            System.out.println("# Client # : Connected to server!");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
         System.out.println("# Client # : Error connecting to server: " + ex.getMessage());
        }

        ObjectInputStream in = null;
        ObjectOutputStream out = null;

 
        try
        {
    
            out = new ObjectOutputStream(socket.getOutputStream());

            //read a string
            
            System.out.println("# Client # : Message entered is: " + Constants.PLAYINDEX+Integer.toString(value));

            //send it to server
            out.writeObject(new Message(Constants.PLAYINDEX+Integer.toString(value)));
            out.flush();
            
            System.out.println("# Client # : Reading from input stream...");
      
            in = new ObjectInputStream(socket.getInputStream());
            
            obj = (Object) in.readObject();
      
        }
        catch (Exception ex)
        {
         ex.printStackTrace();
            System.out.println("# Client # : Error: " + ex);
        }
        finally
        {
         try 
         {
             out.close();
             //socket.close();
         } catch(Exception e)
         {
          e.printStackTrace();
         }
        }
 }
 
 public LinkedList<String> fetchPlayListLinkedList() {
  
  LinkedList<String> ll = new LinkedList<String>();

        AllSongsObject aso = null;
        
        try
        {
         //if(socket != null) {
      // if(!socket.isClosed()) {
      //  socket.close();
      // }
         //}
            socket = new Socket(SERVER_IP, SERVER_PORT);
            System.out.println("# Client # : Connected to server!");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
         System.out.println("# Client # : Error connecting to server: " + ex.getMessage());
        }

        ObjectInputStream in = null;
        ObjectOutputStream out = null;

 
        try
        {
    
            out = new ObjectOutputStream(socket.getOutputStream());

            //read a string
            
            System.out.println("# Client # : Message entered is: " + Constants.GETPLAYLIST);

            //send it to server
            out.writeObject(new Message(Constants.GETPLAYLIST));
            out.flush();
            
            System.out.println("# Client # : Reading from input stream...");
      
            in = new ObjectInputStream(socket.getInputStream());
            
            aso = (AllSongsObject) in.readObject();
            
            System.out.println("# Client # : Server returned object of size: " + aso.get_ll().size());
            
            System.out.println("# Client # : Contents of the list: ");
            
            for(int i=0; i<aso.get_ll().size(); i++) {
             System.out.println(aso.get_ll().get(i));
            }
            
            ll = aso.get_ll();

        }
        catch (Exception ex)
        {
         ex.printStackTrace();
            System.out.println("# Client # : Error: " + ex);
        }
        finally
        {
         try 
         {
             out.close();
             //socket.close();
         } catch(Exception e)
         {
          e.printStackTrace();
         }
        }
        return ll;
 }
 
 public void MainMethod()
    {
        AllSongsObject aso = null;
        
        try
        {
         //if(socket != null) {
         // if(!socket.isClosed()) {
         //  socket.close();
         // }
         //}
            socket = new Socket(SERVER_IP, SERVER_PORT);
            System.out.println("# Client # : Connected to server!");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
         System.out.println("# Client # : Error connecting to server: " + ex.getMessage());
        }

        ObjectInputStream in = null;
        ObjectOutputStream out = null;

 
        try
        {
    
            out = new ObjectOutputStream(socket.getOutputStream());

            //read a string
            
            System.out.println("# Client # : Message entered is: " + message);

            //send it to server
            out.writeObject(new Message(message));
            out.flush();
            
            System.out.println("# Client # : Reading from input stream...");
      
            in = new ObjectInputStream(socket.getInputStream());
            
            aso = (AllSongsObject) in.readObject();
            
            System.out.println("# Client # : Server returned object of size: " + aso.get_ll().size());
            
            System.out.println("# Client # : Contents of the list: ");
            
            for(int i=0; i<aso.get_ll().size(); i++) {
             System.out.println(aso.get_ll().get(i));
            }

        }
        catch (Exception ex)
        {
         ex.printStackTrace();
            System.out.println("# Client # : Error: " + ex);
        }
        finally
        {
         try 
         {
             out.close();
             //socket.close();
         } catch(Exception e)
         {
          e.printStackTrace();
         }
        }
    }
    
    public void execute() {
     new NewClient().MainMethod();
    }
    
}

No comments :

Post a Comment