androidactionscript-3airwavfilereference

How to Save Sound as Ringtone/Notification?


Hey everyone so Not sure if this is possible using as3 for Android Air App. I have been at it for a week now with some progress but not what I would want it to be.

So far I have it to where when the user swipes on the sound it brings up the storage location to save the sound on the phone. Only problem is it doesn't let me save it as a ringtone or notification. Just saves it on the device;

I would like for the user to be able to click save and have it save to the ringtones for them to set it as the ringtone etc...

Any help would be appreciated. Here is what I have so far:

I am saving the sounds as wav. Still having some problems with this because once file is on the device and I try to play it, it plays it but then crashes and says can't read file.

var snd:Sound = new sndPanda();

        var sndBytes:ByteArray = new ByteArray();
        sndBytes.endian = Endian.LITTLE_ENDIAN;

        snd.extract( sndBytes, 44100 * 2 );

        var wav:ByteArray = encode( sndBytes );

        var f:FileReference = new FileReference(); //save file to phone
        f.save( wav, "Panda.Mp3" ); 



        function encode( data:ByteArray ):ByteArray
        {
            var channels:uint = 2;
            var bits:uint = 16;
            var rate:uint = 44100;

            var bytes: ByteArray = new ByteArray();
            bytes.endian = Endian.LITTLE_ENDIAN;

            bytes.writeUTFBytes( 'RIFF' );
            bytes.writeInt( uint( data.length + 44 ) );
            bytes.writeUTFBytes( 'WAVE' );
            bytes.writeUTFBytes( 'fmt ' );
            bytes.writeInt( uint( 16 ) );
            bytes.writeShort( uint( 1 ) );
            bytes.writeShort( channels );
            bytes.writeInt( rate );
            bytes.writeInt( uint( rate * channels * ( bits / 8 ) ) );
            bytes.writeShort( uint( channels * ( bits / 8 ) ) );
            bytes.writeShort( bits );
            bytes.writeUTFBytes( 'data' );
            bytes.writeInt( data.length );
            data.position = 0;
            var length:int = data.length - 4;
            while (data.position < length) { // could be better :-)
                bytes.writeShort(uint(data.readFloat() * 65536));
            }
            bytes.position = 0;

            return bytes;
        }

As u can see I am using FileReference to save to device. But I know there has to be a way to save as ringtone. Thanks in advance.


Solution

  • Only problem is it doesn't let me save it as a ringtone or notification. Just saves it on the device

    That's what FileReference does. load into your app or save into device storage.
    There is no built-in way in AS3 to set audio as an Android ringtone / notification. You might consider seeking out an ANE (Android Native Extension).

    I am saving the sounds as wav.

    Why put MP3 extension on a file with WAV (PCM) data? See : f.save( wav, "Panda.Mp3" );.

    I try to play it, it plays it but then crashes and says can't read file.

    I don't know if this will fix your issue, but considering var snd:Sound = new sndPanda();...
    If sndPanda is MP3 data then use :

    var snd:Sound = new sndPanda();
    var sndBytes:ByteArray = new ByteArray();
    
    var totalSamples : int = int( Math.floor ( (snd.length/1000) * 44100) );
    
    sndBytes.endian = Endian.LITTLE_ENDIAN;
    snd.extract( sndBytes, totalSamples );
    
    var wav:ByteArray = encode( sndBytes );
    var f:FileReference = new FileReference(); //save file to phone
    
    f.save( wav, "Panda.Mp3" );