gpt4 book ai didi

android - Android停止跨多个 Activity 的SoundPool循环。或在更改页面时停止所有播放声音

转载 作者:行者123 更新时间:2023-12-02 23:59:54 24 4
gpt4 key购买 nike

我现在仅用Android和Java编程了几周,试图制作一个吉他调音器来通过耳朵来调音。一个简单的引用调谐器。我已经使用了很多此网站代码来构建它。

单击另一个页面/ Activity 时如何使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();
}

}

这是我的 Activity 代码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

最佳答案

Soundpool.stop(int)对您有用吗?如果是这样,为什么不覆盖Activity.onPause()方法并将其放在那里呢?

关于android - Android停止跨多个 Activity 的SoundPool循环。或在更改页面时停止所有播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8663307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com