【发布时间】:2018-11-11 02:55:07
【问题描述】:
我在让我的音板应用程序正常运行时遇到了一些问题。我已经切换到使用这种方法而不是使用 MediaPlayer,因为 soundpool 是为这种应用程序设计的。然而,我遇到了这个应用程序的问题,当我点击一个按钮时,有时会播放声音,但不是一直播放。感觉很随意。
我有一个 OnLoadCompleteListener,它的状态为“0”,根据 SoundPool 类文档,这意味着成功。不过,情况似乎并非如此。我已经为应用程序中的按钮创建了所有正确的 OnClick 属性。我有大约 40 多种 .MP3 格式的声音,它们的平均容量都在 50 到 100kbs 之间。
我也在这个应用程序前面加载了一个启动画面,这让声音有更多时间在后台加载(我假设)。我已经在多个模拟器和实时设备上尝试过这个。这些声音似乎在 Nexus 设备上播放的频率更高,但在我迄今为止测试过的任何三星 Galaxies 和 OnePlus 设备上都没有。
感觉我已经非常接近完成这项工作了,所以任何简单的指针都会对我有很大帮助。再次感谢。这是我目前所拥有的:
package g.convery.cat_sounds;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.media.AudioAttributes;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.Toast;
import android.media.AudioAttributes;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.AdRequest;
public class MainActivity extends AppCompatActivity {
public static final String TAG = "Status";
public static final String TAG1 = "Destroy";
//Sounds
private int meow1, meow2, meow3, meow4, meow5, meow6, meow7, meow8, meow9, meow10, meow11, meow12, purr1,
purr2, purr3, purr4, purr5, purr6, purr7, purr8, purr9, purr10, purr11, purr12, purr13, purr14, purr15, purr16, purr17, purr18, catcrazy1,
catcrazy2, catcrazy3, catcrazy4, catcrazy5, catcrazy6, catcrazy7, catcrazy8, catcrazy9, catcrazy10, catcrazy11, catcrazy12, catcrazy13, catcrazy14,
catcrazy15, catcrazy16, catcrazy17, catcrazy18, catcrazy19, catcrazy20, catcrazy21;
//Load Soundpool and tabHost
private SoundPool soundPool;
boolean loaded = false;
TabHost tabHost;
public InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = findViewById(R.id.tabHost);
tabHost.setup();
//Tab1
TabHost.TabSpec spec = tabHost.newTabSpec(getResources().getString(R.string.tab_one_title));
spec.setContent(R.id.tab1);
spec.setIndicator(getResources().getString(R.string.tab_one_title));
tabHost.addTab(spec);
//Tab 2
spec = tabHost.newTabSpec(getResources().getString(R.string.tab_two_title));
spec.setContent(R.id.tab2);
spec.setIndicator(getResources().getString(R.string.tab_two_title));
tabHost.addTab(spec);
//Tab 3
spec = tabHost.newTabSpec(getResources().getString(R.string.tab_three_title));
spec.setContent(R.id.tab3);
spec.setIndicator(getResources().getString(R.string.tab_three_title));
tabHost.addTab(spec);
//Event
tabHost.setOnTabChangedListener(new AnimationTabListener(this, tabHost));
// Ads - not added yet
// MobileAds.initialize(this, "ca-app-pub-9834454250090170~1138603969");
// mInterstitialAd = new InterstitialAd(this);
// mInterstitialAd.setAdUnitId("ca-app-pub-9834454250090170/2106989133");
// mInterstitialAd.loadAd(new AdRequest.Builder().build());
// Create SoundPool Builder and regular SoundPool if using an old API
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
soundPool = new SoundPool.Builder()
.setMaxStreams(10)
.setAudioAttributes(audioAttributes)
.build();
} else {
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
}
// Check to see if sounds have been loaded
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
if(sampleId !=0) {
Log.e(TAG, "loading sound tracks (status=" + status + ")");
}
}
});
//Load sounds to soundPool
meow1 = soundPool.load(this, R.raw.meow1, 1);
meow2 = soundPool.load(this, R.raw.meow2, 1);
meow3 = soundPool.load(this, R.raw.meow3, 1);
meow4 = soundPool.load(this, R.raw.meow4, 1);
meow5 = soundPool.load(this, R.raw.meow5, 1);
meow6 = soundPool.load(this, R.raw.meow6, 1);
meow7 = soundPool.load(this, R.raw.meow7, 1);
meow8 = soundPool.load(this, R.raw.meow8, 1);
meow9 = soundPool.load(this, R.raw.meow9, 1);
meow10 = soundPool.load(this, R.raw.meow10, 1);
meow11 = soundPool.load(this, R.raw.meow11, 1);
meow12 = soundPool.load(this, R.raw.meow12, 1);
purr1 = soundPool.load(this, R.raw.purr1, 1);
purr2 = soundPool.load(this, R.raw.purr2, 1);
purr3 = soundPool.load(this, R.raw.purr3, 1);
purr4 = soundPool.load(this, R.raw.purr4, 1);
purr5 = soundPool.load(this, R.raw.purr5, 1);
purr6 = soundPool.load(this, R.raw.purr6, 1);
purr7 = soundPool.load(this, R.raw.purr7, 1);
purr8 = soundPool.load(this, R.raw.purr8, 1);
purr9 = soundPool.load(this, R.raw.purr9, 1);
purr10 = soundPool.load(this, R.raw.purr10, 1);
purr11 = soundPool.load(this, R.raw.purr11, 1);
purr12 = soundPool.load(this, R.raw.purr12, 1);
purr13 = soundPool.load(this, R.raw.purr13, 1);
purr14 = soundPool.load(this, R.raw.purr14, 1);
purr15 = soundPool.load(this, R.raw.purr15, 1);
purr16 = soundPool.load(this, R.raw.purr16, 1);
purr17 = soundPool.load(this, R.raw.purr17, 1);
purr18 = soundPool.load(this, R.raw.purr18, 1);
catcrazy1 = soundPool.load(this, R.raw.catcrazy1, 1);
catcrazy2 = soundPool.load(this, R.raw.catcrazy2, 1);
catcrazy3 = soundPool.load(this, R.raw.catcrazy3, 1);
catcrazy4 = soundPool.load(this, R.raw.catcrazy4, 1);
catcrazy5 = soundPool.load(this, R.raw.catcrazy5, 1);
catcrazy6 = soundPool.load(this, R.raw.catcrazy6, 1);
catcrazy7 = soundPool.load(this, R.raw.catcrazy7, 1);
catcrazy8 = soundPool.load(this, R.raw.catcrazy8, 1);
catcrazy9 = soundPool.load(this, R.raw.catcrazy9, 1);
catcrazy10 = soundPool.load(this, R.raw.catcrazy10, 1);
catcrazy11 = soundPool.load(this, R.raw.catcrazy11, 1);
catcrazy12 = soundPool.load(this, R.raw.catcrazy12, 1);
catcrazy13 = soundPool.load(this, R.raw.catcrazy13, 1);
catcrazy14 = soundPool.load(this, R.raw.catcrazy14, 1);
catcrazy15 = soundPool.load(this, R.raw.catcrazy15, 1);
catcrazy16 = soundPool.load(this, R.raw.catcrazy16, 1);
catcrazy17 = soundPool.load(this, R.raw.catcrazy17, 1);
catcrazy18 = soundPool.load(this, R.raw.catcrazy18, 1);
catcrazy19 = soundPool.load(this, R.raw.catcrazy19, 1);
catcrazy20 = soundPool.load(this, R.raw.catcrazy20, 1);
catcrazy21 = soundPool.load(this, R.raw.catcrazy21, 1);
} // onCreate
// Play sounds if loaded
public void playSound (View v){
switch (v.getId()) {
case R.id.Meow1:
if (loaded){
soundPool.play(meow1, 1, 1, 0, 0, 1);
Log.d("TAG1", "Button 1 pressed");}
soundPool.autoPause();
break;
case R.id.Meow2:
if (loaded){
soundPool.play(meow2, 1, 1, 0, 0, 1);
Log.d("TAG1", "Button 2 pressed");}
break;
case R.id.Meow3:
if (loaded){
soundPool.play(meow3, 1, 1, 0, 0, 1);
Log.d("TAG1", "Button 3 pressed");}
soundPool.autoPause();
break;
case R.id.Meow4:
if (loaded){
soundPool.play(meow3, 1, 1, 0, 0, 1);
Log.d("TAG1", "Button 4 pressed");}
soundPool.autoPause();
break;
case R.id.Meow5:
soundPool.play(meow5, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Meow6:
soundPool.play(meow6, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Meow7:
soundPool.play(meow7, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Meow8:
soundPool.play(meow8, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Meow9:
soundPool.play(meow9, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Meow10:
soundPool.play(meow10, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Meow11:
soundPool.play(meow11, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Meow12:
soundPool.play(meow12, 1, 1, 0, 0, 1);
soundPool.autoPause();
Log.d("TAG1", "Button 12 pressed");
break;
case R.id.Purr1:
soundPool.play(purr1, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr2:
soundPool.play(purr2, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr3:
soundPool.play(purr3, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr4:
soundPool.play(purr4, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr5:
soundPool.play(purr5, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr6:
soundPool.play(purr6, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr7:
soundPool.play(purr7, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr8:
soundPool.play(purr8, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr9:
soundPool.play(purr9, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr10:
soundPool.play(purr10, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr11:
soundPool.play(purr11, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr12:
soundPool.play(purr12, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr13:
soundPool.play(purr13, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr14:
soundPool.play(purr14, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr15:
soundPool.play(purr15, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr16:
soundPool.play(purr16, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr17:
soundPool.play(purr17, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.Purr18:
soundPool.play(purr18, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy1:
soundPool.play(catcrazy1, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy2:
soundPool.play(catcrazy2, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy3:
soundPool.play(catcrazy3, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy4:
soundPool.play(catcrazy4, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy5:
soundPool.play(catcrazy5, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy6:
soundPool.play(catcrazy6, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy7:
soundPool.play(catcrazy7, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy8:
soundPool.play(catcrazy8, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy9:
soundPool.play(catcrazy9, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy10:
soundPool.play(catcrazy10, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy11:
soundPool.play(catcrazy11, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy12:
soundPool.play(catcrazy12, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy13:
soundPool.play(catcrazy13, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy14:
soundPool.play(catcrazy14, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy15:
soundPool.play(catcrazy15, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy16:
soundPool.play(catcrazy16, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy17:
soundPool.play(catcrazy17, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy18:
soundPool.play(catcrazy18, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy19:
soundPool.play(catcrazy19, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy20:
soundPool.play(catcrazy20, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
case R.id.catCrazy21:
soundPool.play(catcrazy21, 1, 1, 0, 0, 1);
soundPool.autoPause();
break;
}
}
// When sound is complete - Destroy - Never seems to be called
@Override
protected void onDestroy(){
super.onDestroy();
soundPool.release();
soundPool= null;
Log.d("TAG1", "Destroyed!!");
}
} // Public Class
【问题讨论】:
标签: java android android-studio soundpool