如何播放ArrayList中存储的所有音乐文件
我有一个 ArrayList,其中包含许多声音文件作为元素。 foreach 循环迭代此集合,并播放每个音符。
问题是,当程序运行时,只播放最后一个音符,但在调试时,它会遍历所有元素并播放每个元素。
声音表示为我的“MusicNote”类的对象。我无法理解问题是什么,因为调试时它工作得很好。
I've got an ArrayList which contains a number of sound files as elements. A foreach loop iterates this collection, and plays each note.
The problem is that when the program is run, only the last note plays, but when debugging, it goes through all the elements and each one is played.
The sound is represented as an object of my 'MusicNote' class. I cant understand what the problem is, as when debugging, it works perfectly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据 http://msdn.microsoft.com/en-us /library/system.media.soundplayer.aspx
player.Play()
启动一个新线程,这意味着它会在文件播放结束之前很久返回...使用
player .PlaySync()
相反 - 要么您的主线程或(因为它是阻塞的)在一个单独的线程上。备注:在调试多线程程序时,有时会有不同的行为 - 特别是。当你走过...
According to http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx
player.Play()
starts a new thread which means that it returns long before playing the file is finished...Use
player.PlaySync()
instead - either in your main thread or (since it is blocking) on a separate thread.Remark: When debugging multi-threaded programs sometimes behave differently - esp. when you step through...
我已经挖掘了一些旧代码,并且已经像这样实现了它(在 WP7 中),
您可以尝试调用:
问题是 SoundPlayer.Play() 是异步的,因此它不会阻止您的 foreach
I've digged through some old code and I've implemented it like this (in WP7)
you can try calling:
The problem is that SoundPlayer.Play() is asynchronic, so it is not going to block your foreach
似乎原因是因为在循环的每次迭代中,您都在有机会播放之前设置文件。只有最后的迭代才有机会加载和播放。
如果您想同时播放剪辑,我认为您需要在多个线程中执行此操作,其中每个线程都播放剪辑。
如果你想按顺序播放它,我会考虑构建一个播放列表,然后加载它。
it seems that the reasonis because on each iteration of the loop you are setting the file before it has a chance play. Only the final iteration has a chance to load and play.
if you want to play the clips simultaneously, i think you'll need to do this in multiple threads where each thread plays the clip.
if you want to play it sequentially i would look into building a playlist, and then loading that.
您需要初始化输出以防止跳过开始的开销。我不确定如何在游戏引擎外部环境中做到这一点。也许你可以添加一个假音符来适应固定长度或错误长度的间隙。
我希望这有帮助!我知道这不是一个很好的答案,但你不能责怪一个尝试正确的人;)
PS看看杰森的播放列表想法,这很好
You need to initialize the output to prevent the overhead from skipping the start. I'm not sure how you can do this though outside game engine environment. Maybe you could add a fake note to fit the gap of fixed length or error length.
I hope this helps! I understand it's not a great answer but you can't blame a guy for trying right ;)
P.S. Look at jasons playlist idea, it's nice
noteList
中有多少项? “调试时”是什么意思? “当程序运行时”是什么意思?此外,
Play()
< /a> 是异步的,所以我猜所有的声音都是同时播放的,所以看起来好像只播放了一个。您可以调用PlaySync()
相反。但是它会阻塞,因此您可能希望在单独的线程上执行此操作。How many items are in the
noteList
? What do you mean by "when debugging"? What do you mean by "when the program is run"?Furthermore,
Play()
is asynchronously, so I guess all sounds are played simultaneously so it seems like only one is played. You can callPlaySync()
instead. It will block however, so you might want to do it on a separate thread.什么是注释?你用什么玩?它们是什么类型的文件?
很可能您调用播放,它开始播放第一个音符,但它不会阻止转到下一个音符,并且第一个音符被取消,依此类推,直到允许完成的最后一个音符,因为什么也没有发生在它之后。在演奏结束后等待,看看这是否是问题所在,如果是,那么您需要找到一种方法来等待音符完成,然后再进入下一个。
What is note? What are you using to play? What kind of files are they?
It may well be that you call play, and it starts to play the first note, but it doesn't block goes to the next note and the first note is canceled and so on until the last note that is allowed to complete because nothing comes after it. Place a wait after the play to see if that is the problem, if it is then you need to find a way to wait for the notes to finish before going to the next.