Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Android Sound Latency Fix

Discussion in 'Assets and Asset Store' started by Catsknead, Apr 19, 2015.

  1. DudoTheStampede

    DudoTheStampede

    Joined:
    Mar 16, 2015
    Posts:
    81
    Thanks for the great fix!
    I'm working on a rhythm game and it work like a charme!

    I've only a problem... I'd like to play some music with this plugin too but after about 5 seconds, the music stops playing! Any hint? :/
     
  2. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    613
  3. DudoTheStampede

    DudoTheStampede

    Joined:
    Mar 16, 2015
    Posts:
    81
  4. VedSarkar

    VedSarkar

    Joined:
    Mar 20, 2016
    Posts:
    1
  5. cethak

    cethak

    Joined:
    Oct 23, 2015
    Posts:
    1
    Hello man ty for everything. But i cant find the example code in the zip. Also i made everything in the pdf but it doesnt work. Here is the error:

    NullReferenceException: Object reference not set to an instance of an object
    AudioCenter.loadSound (System.String soundName) (at Assets/Plugins/Android/AudioCenter.cs:58)

    Edit: My unity version is 5.4.1f1.

    Thansk again
     
    Last edited: Sep 30, 2016
  6. lloydg

    lloydg

    Joined:
    Mar 23, 2015
    Posts:
    36
    Hi @VedSarkar and @cethak,

    I have attached what should be the original plugin, taken from one of my projects using it.

    Here is the java source code for the plugin:

    Code (Java):
    1. package com.catsknead.androidsoundfix;
    2.  
    3. import android.content.Context;
    4. import android.content.res.AssetFileDescriptor;
    5. import android.content.res.AssetManager;
    6. import android.media.SoundPool;
    7. import android.media.SoundPool.OnLoadCompleteListener;
    8. import android.util.Log;
    9. import com.unity3d.player.UnityPlayerActivity;
    10. import java.io.IOException;
    11. import java.util.HashSet;
    12.  
    13. public class AudioCenter
    14.   extends SoundPool
    15. {
    16.   public UnityPlayerActivity activity;
    17.   private Context mContext;
    18.   private int soundId;
    19.   private HashSet<Integer> soundsSet;
    20.  
    21.   public AudioCenter(int maxStreams, Context context, UnityPlayerActivity activity)
    22.   {
    23.     this(maxStreams, 3, 0, context);
    24.     this.activity = activity;
    25.     this.soundsSet = new HashSet();
    26.     setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener()
    27.     {
    28.       public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
    29.       {
    30.         AudioCenter.this.soundsSet.add(Integer.valueOf(sampleId));
    31.       }
    32.     });
    33.   }
    34.  
    35.   public AudioCenter(int maxStreams, int streamType, int srcQuality, Context context)
    36.   {
    37.     super(maxStreams, streamType, srcQuality);
    38.     this.mContext = context;
    39.   }
    40.  
    41.   public void play(int soundID)
    42.   {
    43.     if (this.soundsSet.contains(Integer.valueOf(soundID))) {
    44.       play(soundID, 1.0F, 1.0F, 1, 0, 1.0F);
    45.     }
    46.   }
    47.  
    48.   public void playSound(int soundKey)
    49.   {
    50.     if ((!this.soundsSet.contains(Integer.valueOf(soundKey))) || (soundKey == 0))
    51.     {
    52.       Log.e("SoundPluginUnity", "File has not been loaded!");
    53.       return;
    54.     }
    55.     final int sKey = soundKey;
    56.    
    57.     this.activity.runOnUiThread(new Runnable()
    58.     {
    59.       public void run()
    60.       {
    61.         AudioCenter.this.play(sKey);
    62.       }
    63.     });
    64.   }
    65.  
    66.   public int loadSound(String soundName)
    67.   {
    68.     AssetFileDescriptor afd = null;
    69.     try
    70.     {
    71.       afd = this.mContext.getAssets().openFd(soundName);
    72.     }
    73.     catch (IOException e)
    74.     {
    75.       Log.e("SoundPluginUnity", "File does not exist!");
    76.       return 0;
    77.     }
    78.     this.soundId = load(afd, 1);
    79.     if (this.soundsSet.contains(Integer.valueOf(this.soundId)))
    80.     {
    81.       Log.e("SoundPluginUnity", "File has been already loaded!");
    82.       return this.soundId;
    83.     }
    84.     return this.soundId;
    85.   }
    86.  
    87.   public void unloadSound(int soundIdArg)
    88.   {
    89.     if (unload(soundIdArg))
    90.     {
    91.       this.soundsSet.remove(Integer.valueOf(soundIdArg));
    92.       return;
    93.     }
    94.     Log.e("SoundPluginUnity", "File has not been loaded!");
    95.   }
    96. }
    97.  
     

    Attached Files:

  7. GiyomuGames

    GiyomuGames

    Joined:
    Jan 12, 2015
    Posts:
    80
    Thanks for the plugin guys, works like a charm :)
     
  8. mandal

    mandal

    Joined:
    Jan 10, 2016
    Posts:
    1
    It's working but after implementing my Game is crashing all the time on the phone. I don't know why?

    Update:
    It was an error on my end due to nullreferenceexception. After fixing it this plugin works like a charm. just add dontDestroyOnLoad or else u might miss some sounds if u load scenes regularly.

    If anyone is having any issues implementing this plugin let me know.
     
    Last edited: Oct 4, 2017
  9. krish9

    krish9

    Joined:
    Oct 16, 2017
    Posts:
    4
    Link is not working please fix it
     
  10. Sagrell

    Sagrell

    Joined:
    Feb 22, 2018
    Posts:
    5
    Hey, there is a mistake in your plugin. SoundPool.unload doesnt free space in the pool. So u gonna get out of memory. Instead use SoundPool.release. If someone had a problem with the disappearance of sound, u're welcome)
     
  11. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    I found that OpenSL ES is a bit faster than SoundPool on Android. SoundPool is already fast and easier to work with too, but if you need even lower latency you could check a solution Native Audio I just made : http://exceed7.com/native-audio

    Also I have an article detailed every advantages and disadvantages of each way (SoundPool, MediaPlayer, AudioTrack, OpenSL ES, Superpowered, AAudio)
    https://gametorrahod.com/androids-native-audio-primer-for-unity-developers-65acf66dd124

    It also works with iOS which it uses OpenAL. It is faster than AVAudioPlayer, AudioToolbox, and SystemSound.
     
    Last edited: Sep 4, 2018
  12. Masius

    Masius

    Joined:
    May 1, 2015
    Posts:
    1
    Hey there, sorry for bringing this up. How do you load a file through this method? I understand how your plugin works and how to implement it but I don't know from which folder is it trying to load the files from. Do I need to have a seperate folder inside my device with the audio files or is it a specific folder (like Resources) in my Unity project? Thanks
     
  13. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    It only search for a target folder that comes from "StreamingAssets" special Unity folder. Depending on platform the files in this special folder will be copied as-is to a fixed place. Native program does not know about Unity and its asset so this is the only way to send files from Unity to native.

    You can confirm the destination of StreamingAssets by debug logging `
    Application.streamingAssetsPath` in the real device build.
     
    Masius likes this.
  14. artemzaevskiy_unity

    artemzaevskiy_unity

    Joined:
    Oct 23, 2021
    Posts:
    1
    Hey everyone! Can you help me please. I really don’t understand how to use this plugin. I lost lot of time to try understand but nothing. Maybe someone can help me and maybe can send video how to import it. I TIRED BLYAT
     
  15. GiyomuGames

    GiyomuGames

    Joined:
    Jan 12, 2015
    Posts:
    80
    This plugin should not be necessary anymore. Sound plays correctly on Android in Unity since a couple years ago.