DRMPlayer.java

/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */
package com.nokia.example.mmapi.mediasampler.model;

import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;

import com.nokia.example.mmapi.mediasampler.MediaSamplerMIDlet;
import com.nokia.example.mmapi.mediasampler.viewer.MediaList;

/**
 * Player with DRM protected audio.
 */
public class DRMPlayer implements CommandListener, PlayerListener {

    private Player player;
    private MediaSamplerMIDlet midlet;
    private final Command stopCommand;
    private MediaList list;

    public DRMPlayer(MediaSamplerMIDlet midlet, MediaList list) {
        this.midlet = midlet;
        this.list = list;
        stopCommand = new Command("Stop", Command.ITEM, 2);
    }

    /**
     * Releases resources
     *
     */
    public void releaseResources() {
        discardPlayer();
    }

    public void commandAction(Command cmd, Displayable disp) {
        if (cmd == stopCommand) {
            discardPlayer();
        }
    }

    public void playerUpdate(final Player p, final String event, final Object eventData) {
        // queue a call to updateEvent in the user interface event queue
        Display display = Display.getDisplay(midlet);
        display.callSerially(new Runnable() {

            public void run() {
                DRMPlayer.this.updateEvent(p, event, eventData);
            }
        });
    }

    public void stop() {
        if (player != null) {
            player.close();
        }
    }

    // Called in case of exception to make sure invalid players are closed
    private void discardPlayer() {
        if (player != null) {
            player.close();
            player = null;
        }
        list.removeCommand(stopCommand);
    }

    private void updateEvent(Player p, String event, Object eventData) {
        if (event.equals(END_OF_MEDIA)) {
            discardPlayer();
        } else if (event.equals(CLOSED)) {
            player = null;
            list.removeCommand(stopCommand);
        }
    }

    public void playDRMprotected(String file, String type) {
        if (file == null) {
            midlet.alertError("No file specified for type " + type);
        } else {
            try {
                player = Manager.createPlayer(file);
                player.addPlayerListener(this);
                player.realize();
                player.start();
                list.addCommand(stopCommand);
            } catch (IllegalArgumentException iae) {
                discardPlayer();
                midlet.alertError("IllegalArgumentException: " + iae.getMessage() + " file: " + file);
            } catch (IOException ioe) {
                discardPlayer();
                midlet.alertError("IOException: " + ioe.getMessage() + " file: " + file);
            } catch (MediaException me) {
                discardPlayer();
                midlet.alertError("MediaException: " + me.getMessage() + " file: " + file);
            } catch (SecurityException se) {
                discardPlayer();
                midlet.alertError("SecurityException: " + se.getMessage() + " file: " + file);
            }
        }
    }
}