Advertisement
anta40

AudioPlayer.java

Sep 20th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package com.csl.liteaudioplayer.util;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. import javax.microedition.io.Connector;
  8. import javax.microedition.io.file.FileConnection;
  9. import javax.microedition.media.Manager;
  10. import javax.microedition.media.MediaException;
  11. import javax.microedition.media.Player;
  12.  
  13. import net.rim.device.api.crypto.CryptoTokenException;
  14. import net.rim.device.api.crypto.CryptoUnsupportedOperationException;
  15. import net.rim.device.api.crypto.TripleDESDecryptorEngine;
  16. import net.rim.device.api.crypto.TripleDESKey;
  17. import net.rim.device.api.io.IOUtilities;
  18. import net.rim.device.api.media.protocol.ByteArrayInputStreamDataSource;
  19. import net.rim.device.api.ui.component.Dialog;
  20.  
  21. public class AudioPlayer {
  22.  
  23.     private static final AudioPlayer self = new AudioPlayer();
  24.     private FileHolder holder;
  25.     private Player player;
  26.     private boolean run;
  27.     private byte[] rawData;
  28.     private byte[] decryptedData;
  29.    
  30.     public static synchronized AudioPlayer getInstance(){
  31.         return self;
  32.     }
  33.    
  34.     public void setHolder(FileHolder holder){
  35.         this.holder = holder;
  36.     }
  37.    
  38.     public void play(){
  39.         try {
  40.             String audio_path = "file:///" + holder.getPath() + holder.getFileName();
  41.             FileConnection fc = (FileConnection) Connector.open(audio_path, Connector.READ);
  42.             InputStream is = fc.openInputStream();
  43.             rawData = IOUtilities.streamToBytes(is);
  44.             playEncryptedAudio(rawData);
  45.             is.close();
  46.             fc.close();
  47.         }
  48.         catch (IOException ioex){
  49.                
  50.         }
  51.     }
  52.    
  53.    
  54.    
  55.     // TEA code is taken from http://www.winterwell.com/software/TEA.php
  56.     private void playEncryptedAudio(byte[] data) throws IOException {
  57. //      TEA tea = new TEA("Hello world I'm a potato".getBytes());
  58. //      byte[] decrypted_data = tea.decrypt(data);
  59.         byte[] decrypted_data = XXTEA.decrypt(data, "12345".getBytes());
  60.         ByteArrayInputStream stream = new ByteArrayInputStream(decrypted_data);
  61.        
  62.         ByteArrayInputStreamDataSource source = new ByteArrayInputStreamDataSource(stream, "audio/mpeg");
  63.  
  64.         try {
  65.             player = Manager.createPlayer(source);
  66.             player.start();
  67.             run = true;
  68.         }
  69.         catch (MediaException me){
  70.             Dialog.alert("MediaException: "+me.getMessage());
  71.             run = false;
  72.         }
  73.     }
  74.    
  75.     public void stop(){
  76.         try {
  77.             player.stop();
  78.             run = false;
  79.         }
  80.         catch (MediaException me){
  81.             Dialog.alert("MediaException: "+me.getMessage());
  82.             run = false;
  83.         }
  84.     }
  85.    
  86.     public boolean isRunning(){
  87.         return run;
  88.     }
  89.    
  90.    
  91.     /*
  92.      * References:
  93.      * http://stackoverflow.com/questions/3423076/progress-bar-for-http-request-blackberry
  94.      * http://supportforums.blackberry.com/t5/Java-Development/FileConnection-out-of-resource-problem/td-p/435606
  95.      * http://dev.telnic.org/trac/browser/apps/blackberry/trunk/org/not/java/io/FilterInputStream.java?rev=17
  96.      */
  97.     protected class EncryptedAudioStream extends InputStream {
  98.  
  99.         public int read() throws IOException {
  100.             return 0;
  101.         }
  102.        
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement