1

我有以下代码:

InputStream stream = (InputStream)this.getClass().getResourceAsStream("/preview.mp3");
            Player p = Manager.createPlayer(stream, "audio/mpeg");
            p.realize();
            p.prefetch();
            p.setLoopCount(-1);
            VolumeControl volume = (VolumeControl) p.getControl("VolumeControl");
            volume.setLevel(1);
            p.start();

然后当我调用 stopAlarm()

// Stop playing music
    void stopAlarm()
    {
        try
        {
            // Tell player to stop playing
            p.stop();

        }
        catch (Exception e)
        {
            Dialog.alert("Error stopping music");
        }

        // Then release the data and close out the player
        p.deallocate();
        p.close();
    }

简而言之,它不会停止音频。

如果有人可以帮助我解决这个问题,我将不胜感激。

4

3 回答 3

1

多亏了 Vivek,我注意到一个非常小的错误,它阻止了它的工作。与往常一样,它非常简单。

尽管 Player 被定义为 Class 级别的变量:

static Player p;

问题当然是以下,导致冲突:

Player p = Manager.createPlayer(stream, "audio/mpeg");

所以现在的代码是:

InputStream stream = (InputStream)this.getClass().getResourceAsStream("/preview.mp3");
p = Manager.createPlayer(stream, "audio/mpeg");
p.realize();
p.prefetch();
p.setLoopCount(-1);
VolumeControl volume = (VolumeControl) p.getControl("VolumeControl");
volume.setLevel(1);
p.start();

当然要阻止它,我只是使用:

p.stop();
p.close();
于 2011-11-21T11:06:21.993 回答
1

在停止播放器之前首先检查播放器对象“p”是否为空。

喜欢

if(p != null)
{
    p.stop();
}
else
{
  Dialog.alert("p is null"); //to identify player is null or not.
}

如果播放器对象为空,那么我猜您正在将播放器对象创建为本地对象。

所以创建像这样的播放器对象

p = Manager.createPlayer(stream, "audio/mpeg");

并定义类级别的玩家变量。

于 2011-11-19T10:22:25.073 回答
1

尝试这个

// Stop playing music
void stopAlarm() {
    try {
        p.stop();
        Thread.sleep(50);
        p.close();
        Thread.sleep(50);
        p.deallocate();
    } catch (Exception e) {
        Dialog.alert("Error stopping music");
    }
}
于 2011-11-18T11:34:35.643 回答