DataLine中isRunning()和isActive()有什么区别?

时间:2012-07-08 14:58:13

标签: java audio

DataLineisRunning()isActibe()之间的区别是什么?当line实际上与数据一起使用时,两个标志似乎都被设置。

更新

那里有两对方法:

1)打开/关闭

2)开始/停止

还有三面旗帜

1)isOpen

2)isRunning

3)isActive

有人可以推断他们之间和/或彼此之间的某些关系吗?例如,显然,isRunning暗示isOpen,isActive意味着isOpen,但isActive意味着isRunning,反之亦然?或者他们是独立的?

我猜在调用isOpen后,Line会立即变为open()。因此,isOpen用于检查是否已调用open()

我能否以某种方式检查我是否打过start()?也许有些isActiveisRunning就是为了这个?但是哪一个?

更新1

以下代码用于播放默认系统声音输入到默认系统声音输出的音频。

public static void main(String[] args) throws LineUnavailableException, InterruptedException {

    AudioFormat audioFormat = new AudioFormat(16000f, 16, 1, true, false);

    final SourceDataLine outputLine;
    final TargetDataLine inputLine;


    outputLine = AudioSystem.getSourceDataLine(audioFormat);
    inputLine = AudioSystem.getTargetDataLine(audioFormat);

    Thread player = new Thread("player") {
        @Override
        public void run() {

            byte[] b = new byte[2048];
            int readen;

            log.info("Starting transfer");

            while( !inputLine.isOpen() ) {
                try {
                    log.info("TargetDataLine is closed, waiting");
                    sleep(1000);
                } catch (InterruptedException e) {
                    interrupt();
                }
            }

            while( !inputLine.isRunning() ) {
                try {
                    log.info("TargetDataLine is not running, waiting");
                    sleep(100);
                } catch (InterruptedException e) {
                    interrupt();
                }
            }

            while((readen = inputLine.read(b,0,b.length))>=0 && !isInterrupted()) {
                outputLine.write(b,0,readen);
            }

        }
    };
    player.start();

    outputLine.open();
    outputLine.start();

    inputLine.open();
    inputLine.start();

    player.join();

}

代码的输出如下:

15:20:01.369 [player] INFO  tests.DataLineFlags01 - Starting transfer
15:20:01.371 [player] INFO  tests.DataLineFlags01 - TargetDataLine is closed, waiting
15:20:02.371 [player] INFO  tests.DataLineFlags01 - TargetDataLine is not running, waiting
15:20:02.471 [player] INFO  tests.DataLineFlags01 - TargetDataLine is not running, waiting
15:20:02.571 [player] INFO  tests.DataLineFlags01 - TargetDataLine is not running, waiting
15:20:02.671 [player] INFO  tests.DataLineFlags01 - TargetDataLine is not running, waiting
15:20:02.771 [player] INFO  tests.DataLineFlags01 - TargetDataLine is not running, waiting
...

这证明在调用start()后TargetDataLine不会进入“运行”状态。

isActive()标志也是如此。

这意味着

(1)无法知道是否调用start() (2)isRunning()isActive()标志无用

这是对的吗?可能它们对SourceDataLine个对象很有用吗?

更新2

以下申请

package testing.Test_TargetDataLine_01;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App 
{

private static Logger log = LoggerFactory.getLogger(App.class);

private final AudioFormat audioFormat;
private final TargetDataLine targetDataLine;

private final Thread observer = new Thread("observer") {

    {
        setDaemon(true);
    }

    public void run() {

        while( !isInterrupted() ) {
            if( targetDataLine != null ) {
                log.info("targetDataLine: Opened={}, Active={}, Running={}", targetDataLine.isOpen(), targetDataLine.isActive(), targetDataLine.isRunning());
            }
            else {
                log.info("targetDataLine: is null");
            }
            try {
                sleep(10);
            } catch (InterruptedException e) {
            }
        }

    };
};

public App() throws LineUnavailableException, InterruptedException {

    byte[] buffer = new byte[160*2]; // 1/100 of second 

    log.info("App starting");

    log.info("Observer starting");
    observer.start();

    log.info("Sleeping 500");
    Thread.sleep(500);

    log.info("Creating targetDataLine");
    audioFormat = new AudioFormat(16000, 16, 1, true, false);
    targetDataLine = AudioSystem.getTargetDataLine(audioFormat);

    log.info("Sleeping 500");
    Thread.sleep(500);

    log.info("Opening targetDataLine");
    targetDataLine.open();

    log.info("Sleeping 500");
    Thread.sleep(500);

    log.info("Starting targetDataLine");
    targetDataLine.start();

    log.info("Sleeping 500");
    Thread.sleep(500);

    log.info("Reading target data line for 5 seconds");
    for(int i=0; i<50; ++i) {

        targetDataLine.read(buffer, 0, buffer.length);
        log.info("Stage " + i);
    }

    log.info("Stopping targetDataLine");
    targetDataLine.stop();

    log.info("Sleeping 500");
    Thread.sleep(500);

    log.info("Closing targetDataLine");
    targetDataLine.close();

    log.info("Sleeping 500");
    Thread.sleep(500);

    //log.info("Interrupting observer");
    //observer.interrupt();

}



public static void main( String[] args ) throws LineUnavailableException, InterruptedException
{
    new App();
}
}

产生以下输出:

20:58:23,360 0    [main] INFO  testing.Test_TargetDataLine_01.App  - App starting
20:58:23,362 2    [main] INFO  testing.Test_TargetDataLine_01.App  - Observer starting
20:58:23,362 2    [main] INFO  testing.Test_TargetDataLine_01.App  - Sleeping 500
20:58:23,362 2    [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: is null
20:58:23,372 12   [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: is null
...
20:58:23,842 482  [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: is null
20:58:23,852 492  [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: is null
20:58:23,862 502  [main] INFO  testing.Test_TargetDataLine_01.App  - Creating targetDataLine
20:58:23,862 502  [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: is null
20:58:23,872 512  [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: is null
...
20:58:23,912 552  [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: is null
20:58:23,922 562  [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: is null
20:58:23,929 569  [main] INFO  testing.Test_TargetDataLine_01.App  - Sleeping 500
20:58:23,933 573  [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
20:58:23,943 583  [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
...
20:58:24,413 1053 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
20:58:24,423 1063 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
20:58:24,430 1070 [main] INFO  testing.Test_TargetDataLine_01.App  - Opening targetDataLine
20:58:24,433 1073 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
20:58:24,443 1083 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
...
20:58:24,483 1123 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
20:58:24,493 1133 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
20:58:24,501 1141 [main] INFO  testing.Test_TargetDataLine_01.App  - Sleeping 500
20:58:24,503 1143 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
20:58:24,513 1153 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
...
20:58:24,983 1623 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
20:58:24,993 1633 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
20:58:25,002 1642 [main] INFO  testing.Test_TargetDataLine_01.App  - Starting targetDataLine
20:58:25,002 1642 [main] INFO  testing.Test_TargetDataLine_01.App  - Sleeping 500
20:58:25,003 1643 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
20:58:25,013 1653 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
...
20:58:25,483 2123 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
20:58:25,493 2133 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
20:58:25,502 2142 [main] INFO  testing.Test_TargetDataLine_01.App  - Reading target data line for 5 seconds
20:58:25,502 2142 [main] INFO  testing.Test_TargetDataLine_01.App  - Stage 0
20:58:25,502 2142 [main] INFO  testing.Test_TargetDataLine_01.App  - Stage 1
20:58:25,503 2143 [main] INFO  testing.Test_TargetDataLine_01.App  - Stage 2
20:58:25,503 2143 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=true, Running=true
20:58:25,503 2143 [main] INFO  testing.Test_TargetDataLine_01.App  - Stage 3
20:58:25,503 2143 [main] INFO  testing.Test_TargetDataLine_01.App  - Stage 4
...
20:58:25,512 2152 [main] INFO  testing.Test_TargetDataLine_01.App  - Stage 48
20:58:25,512 2152 [main] INFO  testing.Test_TargetDataLine_01.App  - Stage 49
20:58:25,512 2152 [main] INFO  testing.Test_TargetDataLine_01.App  - Stopping targetDataLine
20:58:25,513 2153 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=true, Running=true
20:58:25,521 2161 [main] INFO  testing.Test_TargetDataLine_01.App  - Sleeping 500
20:58:25,523 2163 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
20:58:25,533 2173 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
...
20:58:26,003 2643 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
20:58:26,013 2653 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=true, Active=false, Running=false
20:58:26,021 2661 [main] INFO  testing.Test_TargetDataLine_01.App  - Closing targetDataLine
20:58:26,023 2663 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
20:58:26,026 2666 [main] INFO  testing.Test_TargetDataLine_01.App  - Sleeping 500
20:58:26,033 2673 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
20:58:26,043 2683 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
...
20:58:26,513 3153 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false
20:58:26,523 3163 [observer] INFO  testing.Test_TargetDataLine_01.App  - targetDataLine: Opened=false, Active=false, Running=false

这表明两个标志是无法区分的。

2 个答案:

答案 0 :(得分:1)

如果您查看API文档,它会说:

isRunning(): 指示线路是否正在运行。默认值为false。当响应于start方法的调用而呈现第一个数据时,开放行开始运行,并且一直持续到呈现停止以响应停止的调用或因为回放完成。

isActive(): 指示线路是否正在进行活动I / O(例如播放或捕获)。当非活动行变为活动状态时,它会向其侦听器发送START事件。同样,当活动行变为非活动状态时,它会发送STOP事件。

查看start()方法,我们发现start()允许该行变为非活动状态,因此我们可以说isRunning()告诉我们该行是否可以变为活动状态,isActive() 1}}告诉我们该行是否正在使用(即数据是以播放或录制的形式传输)

所以,简而言之,

isRunning() - 我们在使用此行之前是否需要致电start()

isActive() - 这条线现在正在使用吗?

如果必须知道何时调用start(),请使用:

final HashSet<Line> openLines = new HashSet<Line>();
...
DataLine myLine = getDataLine();
myLine.addListener(new LineListener(){
    public void update(LineEvent evt)
    {
        if(evt.getType().equals(LineEvent.Type.START)) openLines.add(evt.getLine());
        else if(evt.getType().equals(LineEvent.Type.STOP)) openLines.remove(evt.getLine());
    }
});

...
public boolean isLineStarted(DataLine l)
{
    return openLines.contains(l);
}

答案 1 :(得分:1)

为了简化Ryan的答案: isRunning()只是检查线路是“在线”还是“正在运行”,或者基本上处于活动状态

isActive()检查线路是否正在写入或读取信息,无论是输入还是输出

Dateline JavaDoc:http://docs.oracle.com/javase/1.5.0/docs/api/javax/sound/sampled/DataLine.html

isActive JavaDoc:http://docs.oracle.com/javase/1.5.0/docs/api/javax/sound/sampled/DataLine.html#isActive%28%29

isRunning()JavaDoc:http://docs.oracle.com/javase/1.5.0/docs/api/javax/sound/sampled/DataLine.html#isRunning%28%29