Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.csl.liteaudioplayer.util;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.microedition.io.Connector;
- import javax.microedition.io.file.FileConnection;
- import javax.microedition.media.Manager;
- import javax.microedition.media.MediaException;
- import javax.microedition.media.Player;
- import net.rim.device.api.crypto.CryptoTokenException;
- import net.rim.device.api.crypto.CryptoUnsupportedOperationException;
- import net.rim.device.api.crypto.TripleDESDecryptorEngine;
- import net.rim.device.api.crypto.TripleDESKey;
- import net.rim.device.api.io.IOUtilities;
- import net.rim.device.api.media.protocol.ByteArrayInputStreamDataSource;
- import net.rim.device.api.ui.component.Dialog;
- public class AudioPlayer {
- private static final AudioPlayer self = new AudioPlayer();
- private FileHolder holder;
- private Player player;
- private boolean run;
- private byte[] rawData;
- private byte[] decryptedData;
- public static synchronized AudioPlayer getInstance(){
- return self;
- }
- public void setHolder(FileHolder holder){
- this.holder = holder;
- }
- public void play(){
- try {
- String audio_path = "file:///" + holder.getPath() + holder.getFileName();
- FileConnection fc = (FileConnection) Connector.open(audio_path, Connector.READ);
- InputStream is = fc.openInputStream();
- rawData = IOUtilities.streamToBytes(is);
- playEncryptedAudio(rawData);
- is.close();
- fc.close();
- }
- catch (IOException ioex){
- }
- }
- // TEA code is taken from http://www.winterwell.com/software/TEA.php
- private void playEncryptedAudio(byte[] data) throws IOException {
- // TEA tea = new TEA("Hello world I'm a potato".getBytes());
- // byte[] decrypted_data = tea.decrypt(data);
- byte[] decrypted_data = XXTEA.decrypt(data, "12345".getBytes());
- ByteArrayInputStream stream = new ByteArrayInputStream(decrypted_data);
- ByteArrayInputStreamDataSource source = new ByteArrayInputStreamDataSource(stream, "audio/mpeg");
- try {
- player = Manager.createPlayer(source);
- player.start();
- run = true;
- }
- catch (MediaException me){
- Dialog.alert("MediaException: "+me.getMessage());
- run = false;
- }
- }
- public void stop(){
- try {
- player.stop();
- run = false;
- }
- catch (MediaException me){
- Dialog.alert("MediaException: "+me.getMessage());
- run = false;
- }
- }
- public boolean isRunning(){
- return run;
- }
- /*
- * References:
- * http://stackoverflow.com/questions/3423076/progress-bar-for-http-request-blackberry
- * http://supportforums.blackberry.com/t5/Java-Development/FileConnection-out-of-resource-problem/td-p/435606
- * http://dev.telnic.org/trac/browser/apps/blackberry/trunk/org/not/java/io/FilterInputStream.java?rev=17
- */
- protected class EncryptedAudioStream extends InputStream {
- public int read() throws IOException {
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement