java另外启动线程播放wav声音

搜了一个mp3文件,首先转成wav 用mp3Towav.sh脚本

#!/bin/bash

folder=/home/qzc/workspace/
for file in $(find "$folder" -type f -iname "*.mp3")
do
    name=$(basename "$file" .mp3)
    dir=$(dirname "$file")
    echo ffmpeg -i "$file" -acodec pcm_s16le -ac 1 -ar 16000 "$dir"/"$name".wav
    ffmpeg -i "$file" -acodec pcm_s16le -ac 1 -ar 16000 "$dir"/"$name".wav
done

然后是示例代码

package com.ewivt.redline.dms.jc5000;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

public class AudioPlay {
    //private String path; //带路径的音频文件
    public static AudioInputStream audioStream;
    public static AudioFormat audioFormat;
    public static SourceDataLine sourceDataLine;
    public static boolean playing = false; //当前播放状态
    public static boolean replay = false; //是否继续播放

    public AudioPlay(String path, int times) {
        for (int i = 0; i < 2; i++) {
            System.out.println("i" + i);
            playMusicStopLast(path);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        final String path = "/home/qzc/workspace/11.wav";
        System.out.println("i 0");
        new Thread() {
            public void run() {
                playMusicStopLast(path);
            }
        }.start();
        System.out.println("i 1");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("i 2");
        final String path1 = "/home/qzc/workspace/22.wav";
        new Thread() {
            public void run() {
                playMusicStopLast(path1);
            }
        }.start();
    }

    //停止上一个播放的新播放
    public static void playMusicStopLast(String path) {
        replay = false;
        while (playing) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        playMusic(path);
    }

    public static void playMusic(String path) {
        try {
            replay = true;
            playing = true;
            int count;
            byte buf[] = new byte[1024];
            //获取音频输入流
            audioStream = AudioSystem.getAudioInputStream(new File(path));
            //获取音频的编码格式
            audioFormat = audioStream.getFormat();
            DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);
            sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
            sourceDataLine.open(audioFormat);
            sourceDataLine.start();
            //播放音频
            while (replay && (count = audioStream.read(buf, 0, buf.length)) != -1) {
                sourceDataLine.write(buf, 0, count);
            }
            //播放结束,释放资源
            sourceDataLine.drain();
            sourceDataLine.close();
            audioStream.close();
            playing = false;
        } catch (UnsupportedAudioFileException ex) {
            ex.printStackTrace();
        } catch (LineUnavailableException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

另外线程播放好处是后一个播放能停止前面一个播放,否则只能等前面播放完成才能播放后面一个

你可能感兴趣的:(编程日志java,java)