如何将声音保存为 Ringtone/Notification?

How to Save Sound as Ringtone/Notification?

大家好,不确定是否可以将 as3 用于 Android Air App。我已经做了一个星期,取得了一些进展,但不是我想要的。

到目前为止,当用户在声音上滑动时,它会调出存储位置以将声音保存在 phone 上。唯一的问题是它不允许我将它保存为铃声或通知。只是将其保存在设备上;

我希望用户能够单击保存并将其保存到铃声中,以便他们将其设置为铃声等...

如有任何帮助,我们将不胜感激。这是我目前所拥有的:

我正在将声音保存为 wav。这仍然有一些问题,因为一旦文件在设备上并且我尝试播放它,它播放它但然后崩溃并说无法读取文件。

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;
        }

如您所见,我正在使用 FileReference 保存到设备。但我知道必须有一种方法可以保存为铃声。提前致谢。

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

这就是 FileReference 所做的。 加载到您的应用程序或保存到设备存储。
AS3 中没有内置方法将音频设置为 Android铃声/通知。您可能会考虑寻找 ANE(Android 本机扩展)。

I am saving the sounds as wav.

为什么将 MP3 扩展名放在带有 WAV (PCM) 数据的文件上?参见:f.save( wav, "Panda.Mp3" );

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

我不知道这是否能解决您的问题,但考虑到 var snd:Sound = new sndPanda();...
如果 sndPanda 是 MP3 数据则使用 :

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" );