我将来自用户的一条文本消息作为输入。根据文本消息中的字母,我正在维护一个数组(在代码中命名为持续时间),该数组存储必须播放Audio1的时间。
我或者在特定位置循环播放两个音频文件并停止它们。
第一音频文件播放到第一个字母的持续时间,然后第二音频文件用持续时间值设置位置值,并播放到持续时间2秒。每当该值适当增加时。
但当我播放时,它有时会停在正确的位置(比如前两次迭代),但有时会超过设置的位置,并开始完全播放音频文件,直到结束。
为什么会发生这种情况?
我正在使用 eclipse 来编写和运行 java 代码。
我的java代码。
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import javax.sound.*;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Stegano implements LineListener{
/**
* @param args
*/
static long[] duration;
static boolean playCompleted1;
static boolean playCompleted2;
static int ch;
static float rate,r;
//public static AudioFormat.Encoding PCM_SIGNED;
public static AudioFormat getOutFormat(AudioFormat inFormat){
ch = inFormat.getChannels();
rate = inFormat.getSampleRate();
r = rate;
// convertToFrames();
return new AudioFormat(AudioFormat.Encoding.PCM_SIGNED ,7200,16,ch,ch*2,rate,inFormat.isBigEndian());
}
/*public static void convertToFrames(){
int i ;
for(i=0;i<duration.length;i++){
duration[i] = (r*duration[i])/1000;
System.out.println(duration[i]);
}
}*/
void convertToAudio(){
final File file1 = new File("E:/Jeena_Jeena_wapking.wav");
final File file2 = new File("E:/Jeena_Jeena_wapking.wav");
try {
AudioInputStream in1 = AudioSystem.getAudioInputStream(file1);
AudioInputStream in2 = AudioSystem.getAudioInputStream(file2);
AudioFormat inForm1 = getOutFormat(in1.getFormat());
AudioFormat inForm2 = getOutFormat(in2.getFormat());
DataLine.Info info1 = new DataLine.Info(Clip.class,inForm1);
DataLine.Info info2 = new DataLine.Info(Clip.class,inForm2);
Clip clip1 = (Clip)AudioSystem.getLine(info1);
Clip clip2 = (Clip)AudioSystem.getLine(info2);
long d = 0;
int i;
long dt = clip1.getMicrosecondLength();
System.out.println("Duration : " + (float)((dt / 1000000)/60.0) + "min");
for(i=0;i< duration.length;i++){
System.out.println("i" + i);
in1 = AudioSystem.getAudioInputStream(file1);
inForm1 = getOutFormat(in1.getFormat());
info1 = new DataLine.Info(Clip.class,inForm1);
clip1 = (Clip)AudioSystem.getLine(info1);
clip1.addLineListener(this);
clip1.open(in1);
clip1.setMicrosecondPosition(d);
long pos = 0;
clip1.start();
while(true){
pos = clip1.getMicrosecondPosition();
System.out.println("pos 1: " + pos);
if( pos == (d + duration[i])){
System.out.println("posFinal : " + pos);
clip1.stop();
break;
}
}
while (!playCompleted1) {
// wait for the playback completes
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
clip1.close();
playCompleted1 = false;
System.out.println("PLayback1 completed");
try {
Thread.sleep(8000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
d = d+pos;
in2 = AudioSystem.getAudioInputStream(file2);
inForm2 = in2.getFormat();
info2 = new DataLine.Info(Clip.class,inForm2);
clip2 = (Clip)AudioSystem.getLine(info2);
clip2.addLineListener(this);
clip2.open(in2);
clip2.setMicrosecondPosition(d);
d = d+2000000;
//pos = 0;
clip2.start();
while(true){
pos = clip2.getMicrosecondPosition();
System.out.println("pos 2: " + pos);
if( pos == d){
System.out.println("posFinal : " + pos);
clip2.stop();
break;
}
}
while (!playCompleted1) {
// wait for the playback completes
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
clip2.close();
System.out.println("PLayback2 completed");
try {
Thread.sleep(8000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
//d = pos + 2000000;
playCompleted1 = false;
// playCompleted2 = false;
}
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LineUnavailableException ex) {
System.out.println("Audio line for playing back is unavailable.");
ex.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(LineEvent event) {
LineEvent.Type type = event.getType();
if (type == LineEvent.Type.START) {
System.out.println("Playback started.");
} else if (type == LineEvent.Type.STOP) {
playCompleted1 = true;
System.out.println("Playback completed.");
/* if(playCompleted1 == true){
playCompleted2 = true;
System.out.println("Playback2 completed.");
}*/
}
}
public static void convertMsgToAudio(String msg){
int len = msg.length();
duration = new long[len];
msg = msg.toUpperCase();
System.out.println("Msg 2 : " + msg);
int i;
//char ch;
for(i=0;i<msg.length();i++){
if(msg.charAt(i) == 'A'){
duration[i] = 500000;
}
else if (msg.charAt(i) == 'B'){
duration[i] = 510000;
}
else if (msg.charAt(i) == 'C'){
duration[i] = 520000;
}
else if (msg.charAt(i) == 'D'){
duration[i] = 530000;
}
else if (msg.charAt(i) == 'E'){
duration[i] = 540000;
}
else if (msg.charAt(i) == 'F'){
duration[i] = 550000;
}
else if (msg.charAt(i) == 'G'){
duration[i] = 560000;
}
else if (msg.charAt(i) == 'H'){
duration[i] = 570000;
}
else if (msg.charAt(i) == 'I'){
duration[i] = 580000;
}
else if (msg.charAt(i) == 'J'){
duration[i] = 600000;
}
else if (msg.charAt(i) == 'K'){
duration[i] = 650000;
}
else if (msg.charAt(i) == 'L'){
duration[i] = 660000;
}
else if (msg.charAt(i) == 'M'){
duration[i] = 680000;
}
else if (msg.charAt(i) == 'N'){
duration[i] = 700000;
}
else if (msg.charAt(i) == 'O'){
duration[i] = 750000;
}
else if (msg.charAt(i) == 'P'){
duration[i] = 800000;
}
else if (msg.charAt(i) == 'Q'){
duration[i] = 850000;
}
else if (msg.charAt(i) == 'R'){
duration[i] = 900000;
}
else if (msg.charAt(i) == 'S'){
duration[i] = 950000;
}
else if (msg.charAt(i) == 'T'){
duration[i] = 9800000;
}
else if (msg.charAt(i) == 'U'){
duration[i] = 970000;
}
else if (msg.charAt(i) == 'V'){
duration[i] = 1200000;
}
else if (msg.charAt(i) == 'W'){
duration[i] = 1300000;
}
else if (msg.charAt(i) == 'X'){
duration[i] = 1400000;
}
else if (msg.charAt(i) == 'Y'){
duration[i] = 960000;
}
else if (msg.charAt(i) == 'Z'){
duration[i] = 1600000;
}
else if (msg.charAt(i) == ' '){
duration[i] = 1000000;
}
}
/*for(i=0;i<duration.length;i++){
System.out.println(duration[i]);
}*/
Stegano s = new Stegano();
s.convertToAudio();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
String msg;
System.out.print("Enter message: ");
msg = s.nextLine();
s.close();
System.out.println("Msg " + msg);
convertMsgToAudio(msg);
}
}
请帮忙。我是新手
这是你想要的吗?
pos = clip1.getMicrosecondPosition();
System.out.println("pos 1: " + pos);
if( pos == (d + duration[i])){
System.out.println("posFinal : " + pos);
clip1.stop();
break;
}
当你检查pos==(d持续时间[i])
时,你假设clip1.get微秒位置()
将总是递增1。如果它递增超过1,它可能会跳过检查,永远不会终止循环。尝试将该行更改为
if( pos >= (d + duration[i])){
进一步检查剪辑 2
也是如此。
我有listview,它包含一个带有播放按钮和搜索栏的音频文件列表。 当我单击listview的播放按钮时,我想播放一个音频文件。我成功地实现了这一点,但当我单击列表中的另一个播放按钮时,两个音频文件正在连续播放,它将继续所有单击播放按钮。我如何限制媒体播放器在一个位置播放,如果我点击另一个图标,它必须停止旧的媒体播放器,并开始播放新的。有人能说我怎么实现这个吗?。我有相同的按钮播放和暂停。
音频的加载方式请参考:声音资源 使用 AudioSource 组件播放 在 层级管理器 上创建一个空节点 选中空节点,在 属性检查器 最下方点击 添加组件 -> 其他组件 -> AudioSource 来添加 AudioSource 组件 将 资源管理器 中所需的音频资源拖拽到 AudioSource 组件的 Clip 中,如下所示: 然后根据需要对 AudioSource 组件的其他参数项进行设
在网上研究了几天后,我来这里寻求帮助。我目前正在为朋友们开发一个简短的2D游戏(实际上只是为了好玩),几天前我了解了剪辑。在这个游戏中,玩家可以收集物体(几乎就像马里奥中的硬币一样)。我的问题是,我在收集硬币时会发出非常短的声音(50kB约1秒),如果玩家在1秒钟内收集3个硬币,那么剪辑会使游戏延迟。如果先例剪辑已经结束,那么就没有延迟。但是如果先例剪辑没有结束,那么尝试再次播放剪辑会使游戏延迟(
问一下Xamarin.Forms便携里面Xamarin.Forms的音频怎么播放 正如我所知,有依赖服务,我看到了一些示例,只有iOS和Android,但没有Windows Phone 8.1/Windows 8.1和UWP。