我只用Android和Java编程了几个星期,试图用吉他调音器来调整你的吉他。一个简单的参考调谐器。我已经使用了很多这个网站代码来构建它。
当我点击其他页面/活动时,如何停止SoundPool。
目前我有两个页面,每个页面有7个声音按钮。单击任何按钮时,它会无限循环,直到您单击另一个按钮,然后循环等等...如果我单击下一页的按钮然后单击一个新按钮,则最后一页的声音仍在播放!当你试图用耳朵调整你的吉他时,这很烦人!
也许只是一个简单的停止按钮来停止整个SoundPool会是一个好主意吗?
这是我的SoundManager代码 包org.gtdb.guitar_tuners;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager
{
private Context pContext;
private SoundPool sndPool;
private float rate = 1.0f;
private float masterVolume = 1.0f;
private float leftVolume = 1.0f;
private float rightVolume = 1.0f;
private float balance = 0.5f;
// Constructor, setup the audio manager and store the app context
public SoundManager(Context appContext)
{
sndPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 100);
pContext = appContext;
}
// Load up a sound and return the id
public int load(int sound_id)
{
return sndPool.load(pContext, sound_id, 1);
}
// Play a sound
public void play(int sound_id)
{
sndPool.play(sound_id, leftVolume, rightVolume, 100, -1, rate);
}
// Set volume values based on existing balance value
public void setVolume(float vol)
{
masterVolume = vol;
if(balance < 1.0f)
{
leftVolume = masterVolume;
rightVolume = masterVolume * balance;
}
else
{
rightVolume = masterVolume;
leftVolume = masterVolume * ( 2.0f - balance );
}
}
public void setSpeed(float speed)
{
rate = speed;
// Speed of zero is invalid
if(rate < 0.01f)
rate = 0.01f;
// Speed has a maximum of 2.0
if(rate > 2.0f)
rate = 2.0f;
}
public void setBalance(float balVal)
{
balance = balVal;
// Recalculate volume levels
setVolume(masterVolume);
}
// Free ALL the things!
public void unloadAll()
{
sndPool.release();
}
}
这是我的活动代码Main.java 包org.gtdb.guitar_tuners;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.widget.LinearLayout;
public class Main extends Activity {
SoundManager snd;
int string1, string2, string3, string4, string5, string6, string7;
OnClickListener buttZonClick;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.adMobUnit);
// Create the adView
// Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
AdView adView = new AdView(this, AdSize.IAB_MRECT, "aaaaaaaaaaaaa");
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
// Create an instance of our sound manger
snd = new SoundManager(getApplicationContext());
// Load the samples from res/raw
string1 = snd.load(R.raw.eadgbe1);
string2 = snd.load(R.raw.eadgbe2);
string3 = snd.load(R.raw.eadgbe3);
string4 = snd.load(R.raw.eadgbe4);
string5 = snd.load(R.raw.eadgbe5);
string6 = snd.load(R.raw.eadgbe6);
string7 = snd.load(R.raw.eadgbestrum);
/** Tuner Link **/
Button b1 = (Button) findViewById(R.id.tuner1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Main.this, Dadgad.class));
}
});
/** Tuner Link **/
Button b2 = (Button) findViewById(R.id.tuner2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Main.this, Daefce.class));
}
});
/** gtdb.org Link **/
ImageButton b7 = (ImageButton) findViewById(R.id.imageButton1);
b7.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://www.gtdb.org/stats/click.php?id=17"));
startActivity(i);
}
});
}
public int count = 0;
// Button listener assigned in XML layout
public void clickHandler(View v) {
int id = v.getId(); // Use the button id to determine which sample
// should be played
switch (id) {
case R.id.button1:
snd.play(1);
break;
case R.id.button2:
snd.play(2);
break;
case R.id.button3:
snd.play(3);
break;
case R.id.button4:
snd.play(4);
break;
case R.id.button5:
snd.play(5);
break;
case R.id.button6:
snd.play(6);
break;
case R.id.button7:
snd.play(7);
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.item1) {
Toast.makeText(Main.this,
"Android Guitar Tuner App from Guitar Tunings Database - www.gtdb.org",
Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
}
为代码道歉,我可能会想到这是一团糟,因为我基本上是Java和Android的新手。我一直在拼凑其他人的代码和观看lynda.com视频。
我的调谐器的当前版本已上市,但它没有声音循环,因此您可以浏览页面并收听。但如果没有循环,你必须按下按钮来调整你的吉他!你可以看到here
答案 0 :(得分:0)
Soundpool.stop(int)
适合您吗?如果是这样,为什么不覆盖Activity.onPause()
方法并将其放在那里?