我在尝试在WPF应用程序中播放声音时出现问题。当我从其实际文件位置引用声音时,就像这样,
private void playSound()
{
//location on the C: drive
SoundPlayer myNewSound = new SoundPlayer(@"C:\Users\...\sound.wav");
myNewSound.Load();
myNewSound.Play();
}
它工作正常。但是,我最近将相同的声音导入到我的项目中,当我尝试这样做时,
private void playSound()
{
//location imported in the project
SoundPlayer myNewSound = new SoundPlayer(@"pack://application:,,,/sound.wav");
myNewSound.Load();
myNewSound.Play();
}
它会产生错误并且声音不会播放。如何播放导入到项目中的声音文件?
答案 0 :(得分:1)
对我来说,最简单/最快捷的方法是将已添加文件的构建操作更改为资源,然后执行以下操作:
SoundPlayer player = new SoundPlayer(Properties.Resources.sound_file);//sound_file is name of your actual file
player.Play();
答案 1 :(得分:0)
您使用pack Uri
作为参数,但它需要Stream
或filepath
。
当您将文件添加到项目中时,请更改其Build Action to Content
和Copy To Output Directory to Always
。
using (FileStream stream = File.Open(@"bird.wav", FileMode.Open))
{
SoundPlayer myNewSound = new SoundPlayer(stream);
myNewSound.Load();
myNewSound.Play();
}
答案 2 :(得分:0)
你可以用反射来做。
将文件的属性 Build Action 设置为 Embedded Resource 。
然后您可以阅读:
import java.util.*;
public class f {
// abcd
static int target = 0b1111;
// ab,ac,acd,cd
static int[] groups = {0b1100,0b1010,0b1011,0b0011};
// check if sets cover target
// for example 1100 and 0011 would cover 1111
static boolean covers(boolean[] A){
int or = 0;
for(int i=0;i<A.length;i++){
if(A[i]) or = or | groups[i];
}
int t = target;
while (t>0){
if(t%2!=1 || or%2!=1)
return false;
t = t>>1;
or = or>>1;
}
return true;
}
// go through all choices
static void combos(boolean A[],int i,int j){
if(i>j){
if(covers(A)) System.out.println(Arrays.toString(A));
return;
}
combos(A,i+1,j);
A[i]=!A[i];
combos(A,i+1,j);
}
public static void main(String args[]){
boolean[] A = new boolean[groups.length];
combos(A,0,groups.length-1);
}
}