Advertisement
Guest User

BackEnd.java

a guest
Nov 12th, 2010
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. package com.kirodema.icyBeatsBE;
  2.  
  3. import java.io.File;
  4. import java.io.FilenameFilter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7.  
  8. import android.app.ListActivity;
  9. import android.content.ComponentName;
  10. import android.content.Context;
  11. import android.content.Intent;
  12. import android.content.ServiceConnection;
  13. import android.os.Bundle;
  14. import android.os.IBinder;
  15. import android.os.RemoteException;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.widget.ArrayAdapter;
  19. import android.widget.ListView;
  20.  
  21. public class BackEnd extends ListActivity {
  22.     private static final String MEDIA_PATH = new String("/sdcard/MP3");
  23.     private ArrayList<String> songs = new ArrayList<String>();
  24.     //private MediaPlayer mp = new MediaPlayer();
  25.     //private int curPos;
  26.     private IBEInterface mpInterface;
  27.    
  28.     private ServiceConnection mConnection = new ServiceConnection() {
  29.        
  30.         @Override
  31.         public void onServiceDisconnected(ComponentName arg0) {
  32.             mpInterface = null;
  33.         }
  34.        
  35.         @Override
  36.         public void onServiceConnected(ComponentName arg0, IBinder service) {
  37.             printDebug("Service connectet");
  38.             mpInterface = IBEInterface.Stub.asInterface((IBinder) service);
  39.             printDebug("mpInterface erstellt");
  40.             try {
  41.                 mpInterface.clearPlaylist();
  42.             } catch (RemoteException e) {}
  43.            
  44.             updateSongList(new File(MEDIA_PATH));
  45.         }
  46.     };
  47.  
  48.     private static final FilenameFilter MP3_FILTER
  49.     =  new  FilenameFilter() {
  50.         public boolean accept(File dir, String name) {
  51.             return (name.endsWith(".mp3"));
  52.         }
  53.     };
  54.  
  55.     /** Called when the activity is first created. */
  56.     @Override
  57.     public void onCreate(Bundle savedInstanceState) {
  58.         super.onCreate(savedInstanceState);
  59.         printDebug("App gestartet");
  60.         setContentView(R.layout.songs);
  61.         printDebug("ContentView gesetzt");
  62.         Intent serv = new Intent(this, IBEService.class);
  63.         printDebug("Intent für Service erstellt: " + serv.toString());
  64.         this.bindService(serv, mConnection, Context.BIND_AUTO_CREATE);
  65.         //startService(serv);
  66.         //updateSongList(new File(MEDIA_PATH));
  67.         setListAdapter(new ArrayAdapter<String>(this, R.layout.song_item, songs));
  68.         //Collections.sort(songs);
  69.     }
  70.  
  71.     //TODO
  72.     public void updateSongList(File root) {
  73.         if (root.isFile() && MP3_FILTER.accept(root.getParentFile(), root.getName())) {
  74.             try {
  75.                 songs.add(root.getCanonicalPath());
  76.                 mpInterface.addSongPlaylist(root.getCanonicalPath());
  77.             } catch (IOException e) {}
  78.               catch (RemoteException e) {}
  79.         } else if (root.isDirectory()) {
  80.             for (File x : root.listFiles()) {
  81.                 updateSongList(x);
  82.             }
  83.         }
  84.     }
  85.  
  86.     @Override
  87.     protected void onListItemClick(ListView l, View v, int position, long id) {
  88.          try {
  89.              mpInterface.playFile(position);
  90.      } catch(RemoteException e) {
  91.              Log.e(getString(R.string.app_name), e.getMessage());
  92.      }
  93.     }
  94.    
  95.     private void printDebug (String msg) {
  96.         Log.d(getString(R.string.app_name), msg);
  97.     }
  98.  
  99. //  private void playSong (String songpath) {
  100. //      try {
  101. //          mp.reset();
  102. //          mp.setDataSource(songpath);
  103. //          mp.prepare();
  104. //          mp.start();
  105. //
  106. //          mp.setOnCompletionListener(new OnCompletionListener() {
  107. //              @Override
  108. //              public void onCompletion(MediaPlayer arg0) {
  109. //                  playNextSong();
  110. //              }
  111. //          });
  112. //
  113. //      } catch(IOException e) {}
  114. //  }
  115. //
  116. //  private void playNextSong() {
  117. //      if (++curPos >= songs.size()) {
  118. //          // Last song, just reset currentPosition
  119. //          curPos = 0;
  120. //      } else {
  121. //          // Play next song
  122. //          playSong(MEDIA_PATH + songs.get(curPos));
  123. //      }
  124. //  }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement