setLoopCount(-1) 时黑莓无法停止音频;

发布于 2024-12-16 21:36:29 字数 874 浏览 0 评论 0原文

我有以下代码:

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();
    }

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

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

I have the following code:

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();

Then when I call 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();
    }

In short, it doesn't stop the audio.

I would be very grateful if someone could help me out on this one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〃温暖了心ぐ 2024-12-23 21:36:29

试试这个

// 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");
    }
}

Try this

// 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");
    }
}
南城追梦 2024-12-23 21:36:29

在停止播放器之前,首先检查播放器对象“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");

并定义类级别玩家变量。

first check player object 'p' is null or not before stopping the player.

like

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

if player object is null then i guess you are creating the player object as a local.

so create player object like

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

and define the class level player variable.

神经暖 2024-12-23 21:36:29

感谢 Vivek,我注意到一个非常小的错误导致它无法工作。一如既往,这非常简单。

虽然 Player 被定义为类级别变量:

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();

Thanks to Vivek, I noticed a very small mistake which prevented this from working. As always, it was extremely simple.

Although Player was defined as a Class level variable:

static Player p;

the problem was of course the following, causing a conflict:

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

So now the code is:

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();

Of course to stop it, I simply used:

p.stop();
p.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文