Hi Alex,

At first, you are on windows and used the prebuklt libSDL binaries
shipped with Alien::SDL?

If yes, mp3 should be supported, but only files without VBR. So please
try a few mp3 files and maybe check how it is encoded.

Then, to play an mp3 file from a database, do somethind like:

use DBI;
use SDL;
use SDL::Mixer;
use SDL::Mixer::Channels;
use SDL::Mixer::Samples;
use SDL::RWOps;

$dbh->prepare("SELECT music FROM ...");
$sth = $dbh->execute();
if( $row = $sth->fetchrow_hashref ) {
    $rwops = SDL::RWOps->new_const_mem( $row->{music} );
    $chunk = SDL::Mixer::Samples::load_WAV_RW( $rwops, 0 );
    SDL::Mixer::Channels::play_channel( -1, $chunk, 1 );
}

Cheers, FROGGS


Am 22.04.2012 18:35, schrieb Alex:
> Dear all!
>
> It took me quite some time, but I'm moving forward. 
> Here are some screens from my vocabulary trainer:
> https://picasaweb.google.com/104129024313554475104/Vokabeltrainer?authuser=0
> &authkey=Gv1sRgCKLyn6SSwIvCgwE&feat=directlink
>
> Now I want to play  a vocable audio file (that is of course, stored in a
> database :)). I can't use SDL::Mixer::Music for that, because there is
> already some background music playing. AFAIK I need to use
> SDL::Mixer::Samples.
> I tried it, but there occurred an error when using mp3 files: "Cannot load
> music file [music/terminal.mp3]: Unrecognized sound file type at
> play_effect.pl line 34"
>
> Here is the script:
> [code]
> #!perl
> use strict;
> use warnings;
> use utf8;
>
> use SDL;
> use SDL::Event;
> use SDL::Events;
> use SDLx::App;
> use SDLx::Controller;
> use SDL::Mixer;
> use SDL::Mixer::Music;
> use SDL::RWOps;
> use SDL::Mixer::Samples;
> use SDL::Mixer::Channels;
> SDL::init(SDL_INIT_AUDIO);
> SDL::Mixer::init( MIX_INIT_MP3 | MUS_MP3 );
>
> my $app = SDLx::App->new(
>       title => 'play mp3 effect',
>       exit_on_quit => 1,
> );
>
> unless( SDL::Mixer::open_audio( 44100, AUDIO_S16SYS, 2, 4096 ) == 0 ) {
>       Carp::croak "Cannot open audio: ".SDL::get_error(); 
> }
>
> my $file = 'music/terminal.mp3';
> #my $file = 'music/br_crossing_bell_dop.r.wav';
> #my $file = 'music/Windows Logon Sound.wav';
> my $sample = SDL::Mixer::Samples::load_WAV( $file );
>
> unless( $sample ) {
>       Carp::croak "Cannot load music file [$file]: " . SDL::get_error();
> }
>
> my $playing_channel = SDL::Mixer::Channels::play_channel( -1, $sample, 0 );
>
> $app->run();
>
> SDL::Mixer::Music::halt_music();
> SDL::Mixer::close_audio;
> exit(0);
> [/code]
>
> There also occurred some other errors using .wav files:
> - Cannot load music file [music/br_crossing_bell_dop.r.wav]: Complex WAVE
> files not supported at play_effect.pl line 34
> - Cannot load music file [music/Windows Logon Sound.wav]: MPEG Layer 3 data
> not supported play_effect.pl line 34
>
> But, I only want to play mp3 files.
>
> Maybe you can hint me at the solution for this problem? How can I play an
> mp3 file once, e.g. on a button click event?
>
> Best regards,
> Alex
>
> -----Ursprüngliche Nachricht-----
> Von: breno [mailto:oainikus...@gmail.com] 
> Gesendet: Dienstag, 29. November 2011 06:34
> An: Alexander Becker
> Cc: sdl-devel@perl.org
> Betreff: Re: How to play an mp3 file from a database
>
> On Mon, Nov 28, 2011 at 8:13 PM, Alexander Becker <alexanderbec...@gmx.net>
> wrote:
>> Dear all!
>>
> Hi there!
>
>> I just tried the example code of the SDL Manual where you play some music.
>> By the SDL manual, I refer to the one that is hidden at the bottom of 
>> the sdl.perl.org page, so that you really have to search for it in 
>> order to find it - and even then you have to get along with an ugly github
> interface.
> There's a new project website under way, but we're missing people to work in
> it. Please join #sdl in irc.perl.org if you want to help us get it right :)
>
>> So, in general: Is there a way to play mp3 files?
> Yup.
>
> If your libsdl was compiled with mp3 support, all you have to do
> (iirc) is set the MIX_INIT_MP3 (for effects) and MUS_MP3 (for audio) flags
> when you call SDL::Mixer::init():
>
>   SDL::Mixer::init( MIX_INIT_MP3 | MUS_MP3 );
>
> then use load_MUS() and play_music() from SDL::Mixer::Music to play
> mp3 files as background music, or load_WAV() from SDL::Mixer::Samples to
> play mp3 effects (yes, the function is named load_WAV() but plays different
> formats too if they're available).
>
> The documentation in SDL::Mixer, SDL::Mixer::Music and SDL::Mixer::Samples
> should be helpful, as some of the test files (t/mixer_music.t comes to
> mind).
>
>> And in particular: is there a way to play mp3 files that are in a
> variable?
>> Or do I have to work with temporary files?
>>
> Not sure. I *think* SDL::Mixer::quick_load_WAV( $buffer ) might do the right
> thing. You'll have to test it though.
>
> Cheers,
>
> breno 
>

Reply via email to