-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAudioCenter.java
executable file
·79 lines (64 loc) · 1.96 KB
/
AudioCenter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.catsknead.androidsoundfix;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
import java.io.IOException;
import java.util.HashSet;
public class AudioCenter extends SoundPool
{
private Activity activity;
private Set<Integer> soundsSet = new HashSet<Integer>();
public AudioCenter( int maxStreams, Activity activity )
{
super( maxStreams, AudioManager.STREAM_MUSIC, 0 );
this.activity = activity;
setOnLoadCompleteListener( new OnLoadCompleteListener() {
@Override
public void onLoadComplete( SoundPool soundPool, int sampleId, int status ) {
soundsSet.add( sampleId );
}
} );
}
public void play( int soundID )
{
if( soundsSet.contains( soundID ) ) {
play( soundID, 1, 1, 1, 0, 1f );
}
}
public void playSound( int soundId )
{
if( ( !soundsSet.contains( soundId ) ) || ( soundId == 0 ) ) {
Log.e( "SoundPluginUnity", "File has not been loaded!" );
return;
}
final int sKey = soundId;
activity.runOnUiThread( new Runnable() {
public void run() {
play( sKey );
}
} );
}
public int loadSound( String soundName )
{
AssetFileDescriptor afd = null;
try {
afd = activity.getAssets().openFd( soundName );
} catch( IOException e ) {
Log.e( "SoundPluginUnity", "File does not exist!" );
return -1;
}
int soundId = load( afd, 1 );
soundsSet.add( soundId );
return soundId;
}
public void unloadSound( int soundId )
{
if( unload( soundId ) ) {
soundsSet.remove( soundId );
} else {
Log.e( "SoundPluginUnity", "File has not been loaded!" );
}
}
}