Pyglet无法加载.wav文件

时间:2019-06-19 12:33:22

标签: python python-3.x audio pyglet

Env:

Ubuntu 18.04
Python 3.6.6
pyglet 1.3.2

问题:

基于pyglet的documentation,我尝试运行以下代码:

import pyglet
pyglet.options["audio"] = ("openal", "pulse", "directsound", "silent")
explosion = pyglet.media.load('explosion.wav')

但是发生了以下异常:
1)如果文件是由ffmpeg -i input.mp3 output.wav

转换的
Traceback (most recent call last):
  File "<path_to_dir>/test_sound.py", line 3, in <module>
    explosion = pyglet.media.load('zxc.wav', streaming=False)
  File "<path_to_env>lib/python3.6/site-packages/pyglet/media/sources/loader.py", line 63, in load
    source = get_source_loader().load(filename, file)
  File "<path_to_env>lib/python3.6/site-packages/pyglet/media/sources/loader.py", line 84, in load
    return WaveSource(filename, file)
  File "<path_to_env>lib/python3.6/site-packages/pyglet/media/sources/riff.py", line 197, in __init__
    raise WAVEFormatException('Not a WAVE file')
pyglet.media.sources.riff.WAVEFormatException: Not a WAVE file

2)或互联网上的多个.wav

Traceback (most recent call last):
  File "<path_to_dir>//test_sound.py", line 3, in <module>
    explosion = pyglet.media.load('explosion.wav', streaming=False)
  File "<path_to_env>lib/python3.6/site-packages/pyglet/media/sources/loader.py", line 63, in load
    source = get_source_loader().load(filename, file)
  File "<path_to_env>lib/python3.6/site-packages/pyglet/media/sources/loader.py", line 84, in load
    return WaveSource(filename, file)
  File "<path_to_env>lib/python3.6/site-packages/pyglet/media/sources/riff.py", line 192, in __init__
    format = wave_form.get_format_chunk()
  File "<path_to_env>lib/python3.6/site-packages/pyglet/media/sources/riff.py", line 172, in get_format_chunk
    for chunk in self.get_chunks():
  File "<path_to_env>lib/python3.6/site-packages/pyglet/media/sources/riff.py", line 108, in get_chunks
    chunk = cls(self.file, name, length, offset)
  File "<path_to_env>lib/python3.6/site-packages/pyglet/media/sources/riff.py", line 153, in __init__
    raise RIFFFormatException('Size of format chunk is incorrect.')
pyglet.media.sources.riff.RIFFFormatException: Size of format chunk is incorrect.

问题:

如何通过pyglet正确运行.wav文件?

1 个答案:

答案 0 :(得分:0)

在示例中,它可能是openal或wav文件的问题。程序声音是否正确播放,例如:

use threads;
use xmlparser;

my $file = "project.xml";

#Call the XML Parser
(my $hash_ref,my $exit_state) = xmlparser::ProjectXmlParser($file);

#Define the number of threads
my $num_of_threads = 2;

# use the initThreads subroutine to create an array of threads.
my @threads = initThreads();

# Loop through the array:   
foreach(@threads)
{
    #Tell each thread to perform our 'sub1()' subroutine.
    $_ = threads->create(\&sub1);
}

# This tells the main program to keep running until all threads have finished.
foreach(@threads)
{
    $_->join();
}

####################### SUBROUTINES ############################

sub initThreads
{
    my @initThreads;
    for(my $i = 1;$i<=$num_of_threads;$i++)
    {
        push(@initThreads,$i);
    }
    return @initThreads;
}

sub sub1 {
    sleep(1);
    print "\nIn the thread\n\n";
}

,您可以共享有问题的wav文件吗?我刚刚使用https://github.com/pyreiz/pyreiz/blob/master/reiz/media/wav/ding.wav在Linux Mint 19,Python 3.7.1和pyglet 1.3.2上进行了测试,并且运行良好。