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.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 player_instance = new AudioPlayer();
- private FileHolder holder;
- private Player player;
- private boolean run;
- public static synchronized AudioPlayer getInstance(){
- return player_instance;
- }
- 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();
- byte[] rawData = IOUtilities.streamToBytes(is);
- playEncryptedAudio(rawData);
- is.close();
- fc.close();
- }
- catch (IOException ioex){
- }
- }
- private void playEncryptedAudio(byte[] data) throws IOException {
- TEA tea = new TEA("Hello world I'm a potato".getBytes());
- byte[] decrypted_data = tea.decrypt(data);
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement