001: /*
002: *
003: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025:
026: package com.sun.mmedia;
027:
028: import com.sun.midp.log.Logging;
029: import com.sun.midp.log.LogChannels;
030: import com.sun.mmedia.*;
031: import java.lang.*;
032: import javax.microedition.media.*;
033: import javax.microedition.media.control.*;
034:
035: /**
036: * Native volume control implementation
037: */
038: public class DirectVolume implements VolumeControl {
039:
040: // VolumeControl functions
041: protected native int nGetVolume(int hNative);
042:
043: protected native int nSetVolume(int hNative, int level);
044:
045: protected native boolean nIsMuted(int hNative);
046:
047: protected native boolean nSetMute(int hNative, boolean mute);
048:
049: private int _level = -1;
050: private int _mute = -1;
051: private int _hNative;
052: private BasicPlayer _player;
053:
054: DirectVolume(BasicPlayer player, int hNative) {
055: _player = player;
056: _hNative = hNative;
057: }
058:
059: void setToThisPlayerLevel() {
060: if (_level == -1)
061: return;
062: nSetVolume(_hNative, _level);
063: }
064:
065: void setToPlayerMute() {
066: if (_mute == -1)
067: return;
068: nSetMute(_hNative, _mute == 1);
069: }
070:
071: public int getLevel() {
072: if (_level == -1) {
073: _level = nGetVolume(_hNative);
074: }
075: return _level;
076: }
077:
078: public int setLevel(int level) {
079: if (level < 0) {
080: if (Logging.REPORT_LEVEL <= Logging.ERROR) {
081: Logging.report(Logging.ERROR, LogChannels.LC_MMAPI,
082: "volume level " + level
083: + " is negative, change to 0");
084: }
085: level = 0;
086: } else if (level > 100) {
087: if (Logging.REPORT_LEVEL <= Logging.ERROR) {
088: Logging.report(Logging.ERROR, LogChannels.LC_MMAPI,
089: "volume level " + level
090: + " is too big, change to 100");
091: }
092: level = 100;
093: }
094:
095: // Volume value is same. just return.
096: if (_level == level) {
097: return _level;
098: }
099:
100: // If this player is not started yet, this new volume will be setted
101: // when this player start
102: if (_player.state == Player.STARTED) {
103: if (-1 == nSetVolume(_hNative, level)) {
104: if (Logging.REPORT_LEVEL <= Logging.ERROR) {
105: Logging.report(Logging.ERROR, LogChannels.LC_MMAPI,
106: "set volume failed volume=" + _level);
107: }
108: }
109: }
110:
111: _level = level;
112: _player.sendEvent(PlayerListener.VOLUME_CHANGED, this );
113:
114: return _level;
115: }
116:
117: public boolean isMuted() {
118: if (_mute != -1)
119: return (_mute == 1);
120: if (true == nIsMuted(_hNative)) {
121: _mute = 1;
122: return true;
123: } else {
124: _mute = 0;
125: return false;
126: }
127: }
128:
129: public void setMute(boolean mute) {
130: if (_player.state == Player.STARTED) {
131: nSetMute(_hNative, mute);
132: }
133: _mute = mute ? 1 : 0;
134: }
135: }
|