android - 如何将声音保存为铃声/通知?

标签 android actionscript-3 air wav filereference

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

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

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

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

我正在将声音保存为 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 native 扩展)。

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

关于android - 如何将声音保存为铃声/通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38176001/

相关文章:

android - Android 上的 AIR 应用程序崩溃

android - Webview 不会在 Webview 中打开链接

flash - OOP 问题 : Extending a class, 覆盖函数和类似 jQuery 的语法

apache-flex - Actionscript 3 - 解析 yyyy-mm-dd hh :mm:ss to a Date object? 的最快方法

actionscript-3 - ActionScript 3 Dictionary 是 HashMap 吗?

actionscript-3 - 在 adobe air 中解压本地文件

ios - 适用于iOS的AS3 AIR-StageVideo无法正常工作

java - 为什么 Android 的 MediaStore.Audio.Album.ALBUM_ID 会导致 IllegalStateException 错误?

android - 协调器布局重叠底部按钮

android - 查询电话号码