2013-07-03 32 views
-1

找不到如何提問Steve Pomeroy發佈的「用Android播放任意音」的問題,於是開始了一個here關於「用Android玩任意音」

是否有任何需要添加到xml文件的代碼? 無法讓SIM卡發聲。

public class PlaySound extends Activity { 
    // originally from http://marblemice.blogspot.com/2010/04/generate-and-play-tone-in-android.html 
    // and modified by Steve Pomeroy <[email protected]> 
    private final int duration = 3; // seconds 
    private final int sampleRate = 8000; 
    private final int numSamples = duration * sampleRate; 
    private final double sample[] = new double[numSamples]; 
    private final double freqOfTone = 440; // hz 

private final byte generatedSnd[] = new byte[2 * numSamples]; 

Handler handler = new Handler(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 

    // Use a new tread as this can take a while 
    final Thread thread = new Thread(new Runnable() { 
     public void run() { 
      genTone(); 
      handler.post(new Runnable() { 

       public void run() { 
        playSound(); 
       } 
      }); 
     } 
    }); 
    thread.start(); 
} 

void genTone(){ 
    // fill out the array 
    for (int i = 0; i < numSamples; ++i) { 
     sample[i] = Math.sin(2 * Math.PI * i/(sampleRate/freqOfTone)); 
    } 

    // convert to 16 bit pcm sound array 
    // assumes the sample buffer is normalised. 
    int idx = 0; 
    for (final double dVal : sample) { 
     // scale to maximum amplitude 
     final short val = (short) ((dVal * 32767)); 
     // in 16 bit wav PCM, first byte is the low order byte 
     generatedSnd[idx++] = (byte) (val & 0x00ff); 
     generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8); 

    } 
} 

void playSound(){ 
    final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 
      sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
      AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length, 
      AudioTrack.MODE_STATIC); 
    audioTrack.write(generatedSnd, 0, generatedSnd.length); 
    audioTrack.play(); 
} 
} 

是有這樣的:

activity android:soundEffectsEnabled="true" 

uses-permission android:name="android.permission.WRITE_SETTINGS"

需要被添加,以便上述代碼將使聲音在模擬器如Eclipse?

我已添加活動android:soundEffectsEnabled =「true」並使用權限android:name =「android.permission.WRITE_SETTINGS」/,但仍不會發出聲音。

認爲這是聲音的持續時間,因爲當持續時間設置爲10而不是1時,它發出嘟嘟聲,但很短。但是,在第三次運行之後,會拋出inflateException。

持續時間超過500會導致內存不足錯誤,這是通過例外情況。然而,100的持續時間仍然只會發出非常短的嘟嘟聲,大麥可以聽到它,鼠標點擊更響。

持續時間超過250是記憶擁抱。

的15K 已經從100改變了freqOfTone高達55000.

10的持續時間,使點擊作爲250 10 generatedSnd.length的持續時間,只要使一個點擊作爲generatedSnd.length,只要

仍然不知道如何使聲音更長。

回答

0

後 audioTrack.play加入

int x = 0; 
    // Montior playback to find when done 
    do 
    {              
     if (audioTrack != null) 
      x = audioTrack.getPlaybackHeadPosition(); 
     else 
      x = numSamples;    
    } 
    while (x<numSamples); 

    // Track play done. Release track. 
    if (audioTrack != null) audioTrack.release(); 

(); 在首次運行後停止短按操作。

現在我必須找出爲什麼它不工作時,我改變了freqOfTone。