Advertisement
anta40

AudioPlayer.java

Sep 7th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 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.io.IOUtilities;
  14. import net.rim.device.api.media.protocol.ByteArrayInputStreamDataSource;
  15. import net.rim.device.api.ui.component.Dialog;
  16.  
  17. public class AudioPlayer {
  18.  
  19.     private static final AudioPlayer player_instance = new AudioPlayer();
  20.     private FileHolder holder;
  21.     private Player player;
  22.     private boolean run;
  23.    
  24.     public static synchronized AudioPlayer getInstance(){
  25.         return player_instance;
  26.     }
  27.    
  28.     public void setHolder(FileHolder holder){
  29.         this.holder = holder;
  30.     }
  31.    
  32.     public void play(){
  33.         try {
  34.             String audio_path = "file:///" + holder.getPath() + holder.getFileName();
  35.             FileConnection fc = (FileConnection) Connector.open(audio_path, Connector.READ);
  36.             InputStream is = fc.openInputStream();
  37.             byte[] rawData = IOUtilities.streamToBytes(is);
  38.             playEncryptedAudio(rawData);
  39.             is.close();
  40.             fc.close();
  41.             }
  42.             catch (IOException ioex){
  43.                
  44.             }
  45.     }
  46.    
  47.     private void playEncryptedAudio(byte[] data) throws IOException {
  48.         TEA tea = new TEA("Hello world I'm a potato".getBytes());
  49.         byte[] decrypted_data = tea.decrypt(data);
  50.         ByteArrayInputStream stream = new ByteArrayInputStream(decrypted_data);
  51.         ByteArrayInputStreamDataSource source = new ByteArrayInputStreamDataSource(stream, "audio/mpeg");
  52.  
  53.         try {
  54.             player = Manager.createPlayer(source);
  55.             player.start();
  56.             run = true;
  57.         }
  58.         catch (MediaException me){
  59.             Dialog.alert("MediaException: "+me.getMessage());
  60.             run = false;
  61.         }
  62.     }
  63.    
  64.     public void stop(){
  65.         try {
  66.             player.stop();
  67.             run = false;
  68.         }
  69.         catch (MediaException me){
  70.             Dialog.alert("MediaException: "+me.getMessage());
  71.             run = false;
  72.         }
  73.     }
  74.    
  75.     public boolean isRunning(){
  76.         return run;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement