簡體   English   中英

JLayer暫停並繼續

[英]JLayer pause and resume

我剛剛開始使用Jlayer庫播放MP3。 效果很好,我可以播放歌曲。 我唯一的問題是實現暫停和恢復方法。 由於我對多線程的了解有限,盡管我讓播放MP3的線程處於等待狀態,但聲音會停止,為了恢復歌曲,我只需要通知線程即可。 這是我得到的:

import java.util.Scanner;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import javazoom.jl.player.Player;

public class MP3 extends Thread{
    private String filename;
    private Player player; 
    private Thread t;
    private volatile boolean continuePlaying = true;

    // constructor that takes the name of an MP3 file
    public MP3(String filename) {
        this.filename = filename;
    }

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

    // play the MP3 file to the sound card
    public void play() {
        try {
            FileInputStream fis     = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
        }

        catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }

    }

    public void run() {
        play();

            try { 
                while (true) { 
                    synchronized(this) {
                    while(!continuePlaying)
                        wait();


                    player.play();
                    }

                }
            }
            catch (Exception e) { 
                System.out.println(e);

        }

    }



    private void pause() throws InterruptedException{

            System.out.println("Pause");
            continuePlaying = false;



    }

    private void resumeSong() throws InterruptedException{
        synchronized(this) {
            System.out.println("Resume");
            continuePlaying = true;
            notify();

        }
    }

    // test client
    public static void main(String[] args) throws InterruptedException{
        String filename = ("Fall To Pieces.mp3");
        MP3 mp3 = new MP3(filename);
        mp3.start();

        Scanner s = new Scanner(System.in);
        s.nextLine();

        mp3.pause();

        s.nextLine();

        mp3.resumeSong();


        try {
        mp3.join();
    } catch (Exception e){
    }
    }

}

但是由於某種原因,wait()不會執行任何操作,並且程序甚至無法到達notify()。 為什么會這樣呢?

我已經閱讀了關於此的先前的SO問題,但是我無法使其工作。 我也有興趣了解為什么這段代碼無法使用,因此我可以進一步理解多線程。 謝謝!

這里已經很晚了,如果我讀錯你的代碼,請原諒。 但據我所知,您可以使用continuePlaying = true;啟動線程continuePlaying = true; 而run方法只是調用play(); 否初始化新​​玩家,然后直接進入while (true)循環,直到必須退出點。 仍然停留在它的無盡循環中的那個線程無法更改continuePlaying,即使您啟動另一個MP3線程來訪問volatile變量,它也將進入相同的循環,然后才能進行任何更改。 因此,永遠不會到達wait()。 稍后,您嘗試從自身內部通知()您的等待線程。 這有點自相矛盾,因為它正等待被通知,並且處於等待狀態,無所事事,更不用說通知自己了。 它根本無法做任何事情,除非得到通知,包括通知自己或其他人。 我想說的是,您應該處理wait(),但尤其要從正在處理/等待的線程外部進行notify()。

另外,您的player.play(); 處於一個奇怪的位置。 此刻,玩家僅應在線程被暫停(等待)至少一次之后才開始播放,因為該線程處於while(!continuePlaying)條件之后。

因此,對於您的情況,我將評估程序與其他線程(甚至測試的主線程)中的方法一起使用,該方法在該線程上調用wait()和notify()並進行同步。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM