0

我正在创建一个音板 Android 应用程序,以便更多地了解应用程序背后发生的事情。

目前在一个名为 ChangeSounds 的活动中有三个搜索栏。它们控制音量、平移和播放速度。我编写了一个名为 SoundManager 的 java 文件来处理这些参数。我正处于将 soundManager 的功能链接到 ChangeSounds 活动的阶段,但我不知道应该如何去做。我应该使用意图吗?或者我应该使用广播接收器?任何帮助将不胜感激。

ChangeSounds.java

package com.example.beatpadmaker;

// Importing necessary libraries
import android.app.Activity;
import android.os.Bundle;

public class ChangeSounds extends Activity {

// sets the layout to changesounds
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.changesounds);  
}

更改声音.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/metalbackground"
android:orientation="vertical" >

<!-- Creates the settings text -->
<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/titlesettings"
    android:textColor="#FFFFFF"
    android:textSize="30sp" >
</TextView>

<!-- Creates the volume text -->
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/volumebar"
    android:textColor="#FFFFFF" >
</TextView>

<!-- Creates the seekbar for volume -->
<SeekBar 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:id="@+id/VolBar1" 
    android:max="100" 
    android:progress="100">
</SeekBar>

<!-- Creates the balance text -->
<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/balance"
    android:textColor="#FFFFFF" >
</TextView>

<!-- Creates the seekbar for audio balance -->
<SeekBar 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:max="200" 
    android:id="@+id/BalBar" 
    android:progress="100">
</SeekBar>

<!-- Creates the speed text -->
<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/speed"
    android:textColor="#FFFFFF" >
</TextView>

<!-- Creates the seekbar for speed -->
<SeekBar 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/SpeedBar" 
    android:max="200" 
    android:progress="100">    
</SeekBar>

</LinearLayout>

声音管理器.java

package com.example.beatpadmaker;

//Importing necessary libraries
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {

//Initialising all variables    
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 that is used to setup the audio manager and store the  application context
public SoundManager(Context appContext)
{
  sndPool = new SoundPool(5, 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, 1, 0, rate);    
}   

// Set volume values based on existing volume 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 );
    }

}

// Determine the speed of audio playback
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;
}

// Recalculate volume levels
public void setBalance(float balVal)
{
    balance = balVal;
    setVolume(masterVolume);
}

// Releases the soundpool's contents
public void unloadAll()
{
    sndPool.release();      
}

}

主菜单.java

package com.example.beatpadmaker;

//Importing necessary libraries
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainMenu extends Activity {

//Initializing all variables as well as a tag string for testing my life cycle in logcat
private static final String TAG= MainMenu.class.getSimpleName();
SoundManager snd;
int sound1, sound2, sound3, sound4, sound5, sound6, sound7, sound8, sound9, sound10, sound11, sound12;
OnSeekBarChangeListener barChange;
OnClickListener buttonClick;

//Creating the layout
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu); 

Log.d(TAG, "onCreate() called!");   

// Create an instance of our sound manger
snd = new SoundManager(getApplicationContext()); 

// Set volume rocker mode to media volume
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

// Load the samples from res/raw as oggs
sound1 = snd.load(R.raw.sound1);
sound2 = snd.load(R.raw.sound2);
sound3 = snd.load(R.raw.sound3);
sound4 = snd.load(R.raw.sound4);
sound5 = snd.load(R.raw.sound5);
sound6 = snd.load(R.raw.sound6);
sound7 = snd.load(R.raw.sound7);
sound8 = snd.load(R.raw.sound8);
sound9 = snd.load(R.raw.sound9);
sound10 = snd.load(R.raw.sound10);
sound11 = snd.load(R.raw.sound11);
sound12 = snd.load(R.raw.sound12);

// Create a seek bar handler
barChange = new OnSeekBarChangeListener() 
{
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) 
    {   

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) 
    {  

    }
    //Creating the three cases for the different seekbars to get the progress of volume, balance and speed
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
    {
        switch (seekBar.getId())
        {
         case R.id.VolBar1:
             snd.setVolume((float)progress/100.0f);
             break;

         case R.id.BalBar:
             snd.setBalance((float)progress/100.0f);
             break;

         case R.id.SpeedBar:
             snd.setSpeed((float)progress/100.0f);
             break;
        }
    }
};

}

// 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(sound1);
     break;

     case R.id.button2:
     snd.play(sound2);
     break;

     case R.id.button3:
     snd.play(sound3);
     break;       

     case R.id.button4:
     snd.play(sound4);
     break;       

     case R.id.button5:
     snd.play(sound5);
     break;       

     case R.id.button6:
     snd.play(sound6);
     break;

     case R.id.button7:
     snd.play(sound7);
     break; 

     case R.id.button8:
     snd.play(sound8);
     break;

     case R.id.button9:
     snd.play(sound9);
     break; 

     case R.id.button10:
     snd.play(sound10);
     break; 

     case R.id.button11:
     snd.play(sound11);
     break; 

     case R.id.button12:
     snd.play(sound12);
     break;    
    }

}
4

1 回答 1

0

在 onCreate() 中,使用

SeekBar bar = (SeekBar) findViewById(R.id.volume);

然后在上面设置监听器。

bar.setOnSeekBarChangeListener(barChangeListener);

确保如果你这样做,你定义了侦听器。

private OnSeekBarChangeListener barChangeListener = new OnSeekBarChangeListener() {
    // implement abstract methods 
}
于 2013-04-12T17:24:01.343 回答