Front page | perl.sdl.devel |
Postings from November 2011
Re: How to play an mp3 file from a database
Thread Previous
|
Thread Next
From:
Tobias Leich
Date:
November 30, 2011 04:18
Subject:
Re: How to play an mp3 file from a database
Message ID:
4ED61F00.9090201@froggs.de
Hi, can you please check what version of libSDL_mixer do you have installed?
http://sdl.perl.org/SDL-Mixer.html#linked_version
In case you have 1.2.7 or better we can add support for RWOps objects.
So you dont need temp files.
Cheers, FROGGS
Am 29.11.2011 20:37, schrieb Alexander Becker:
> Hi breno,
>
> thank you for the quick response. It helped.
> I had a look into mixer_samples.t and line 106 states, that quick_load_WAV
> is not (yet? Who do I have to bribe with what to get it?) implemented.
>
> Here is a working piece of code using temporary files. Of course, you need a
> database containing audio files.
>
> Best regards,
> Alex
>
> [code]
> #!perl
>
> use strict;
> use warnings;
> use utf8;
> use DBI;
> use SQL::Abstract::Limit;
> use File::Temp;
> use SDL;
> use SDL::Audio;
> use SDL::Mixer;
> use SDL::Mixer::Samples;
> use SDL::Mixer::Channels;
> use SDL::Mixer::Music;
> SDL::init(SDL_INIT_AUDIO);
> SDL::Mixer::init( MIX_INIT_MP3 | MUS_MP3 );
>
> unless( SDL::Mixer::open_audio( 44100, AUDIO_S16SYS, 2, 4096 ) == 0 ) {
> Carp::croak "Cannot open audio: ".SDL::get_error();
> }
>
> my $buffer = get_audio_buffer();
>
> # -- write audio buffer to temp file
> my $fh = File::Temp->new( UNLINK => 1, SUFFIX => '.mp3' );
> $fh->unlink_on_destroy( 1 );
> print $fh $buffer;
> my $fname = $fh->filename;
>
> # close the handle or the temfile will have some sort of lock on it and
> cannot be read
> $fh->close();
>
> my $background_music = SDL::Mixer::Music::load_MUS( $fname );
>
> unless( $background_music ) {
> Carp::croak "Cannot load music file [buffer from DB]: " .
> SDL::get_error();
> }
>
> SDL::Mixer::Music::play_music( $background_music,0 );
>
> sleep(6);
>
> SDL::Mixer::Music::halt_music();
> SDL::Mixer::close_audio;
> exit(0);
>
>
>
> sub get_audio_buffer {
> my $sql = SQL::Abstract::Limit->new( limit_dialect => 'LimitXY' );;
>
> my $table = 'audio_files';
> my @fields = (qw/audio/);
> my %where = (
> id => 2,
> );
> my @order = ();
> my $limit = 1;
> my $offset = 0;
> my ( $stmt, @bind ) = $sql->select( $table, \@fields, \%where, \@order,
> $limit, $offset );
> my $database = 'database_name';
> my $db_host = 'your_host';
> my $db_port = '3306';
> my $dsn = "DBI:mysql:database=$database;host=$db_host;port=$db_port";
> my $username = 'username';
> my $password = 'password';
> my $dbh = DBI->connect($dsn, $username, $password) or die('Cannot
> connect to DB: ' . DBI->errstr());
> my $sth = $dbh->prepare( $stmt );
> $sth->execute( @bind );
>
> my ($buffer) = $sth->fetchrow_array();
>
> return $buffer;
> } # /get_audio_buffer
> [/code]
>
> -----Ursprüngliche Nachricht-----
> Von: breno [mailto:oainikusama@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 < > 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
> -----
> eMail ist virenfrei.
> Von AVG überprüft - www.avg.de
> Version: 10.0.1411 / Virendatenbank: 2092/4046 - Ausgabedatum: 29.11.2011
>
Thread Previous
|
Thread Next