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

プレーヤーを停止する前に、まずプレーヤーオブジェクト'p'がnullであるかどうかを確認します。

お気に入り

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

プレーヤーオブジェクトがnullの場合、ローカルとしてプレーヤーオブジェクトを作成していると思います。

したがって、次のようなプレーヤーオブジェクトを作成します

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 に答える
1

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();
于 2011-11-21T11:06:21.993 に答える