2013-12-17 32 views
1

我一直致力於播放某些音頻的應用程序。我目前正在播放音頻作爲STREAM_MUSIC類型的流,這工作得很好。我希望能夠通過設備上的硬件音量控制來控制音量。當我在應用程序中時,硬件按鈕不起任何作用,音量不會改變,並且不會彈出。然後按主頁按鈕,以便應用程序在後臺運行硬件音量按鈕。它們只在我的應用程序在後臺運行時才起作用。當我的應用程序運行時,硬件音量按鈕不起作用

我試圖在我的onCreate()方法中使用代碼this.setVolumeControlStream(AudioManager.STREAM_MUSIC);,這並沒有改變應用程序的行爲,我仍然面臨同樣的問題。

我也一直在DROID 2,DROID RAZR,三星Galaxy s3,三星Galaxy s4,聯想平板電腦和一個根源DROID 2上進行測試,但它們都表現相同。

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
    setVolumeControlStream(audio.STREAM_MUSIC); //this line should set up the hardware 
    //buttons to control the volume 
    if (QtApplication.m_delegateObject != null && QtApplication.onCreate != null) { 
     QtApplication.invokeDelegateMethod(QtApplication.onCreate, savedInstanceState); 
     return; 
    } 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    try { 
     m_activityInfo = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA); 
    } catch (NameNotFoundException e) { 
     e.printStackTrace(); 
     finish(); 
     return; 
    } 

    if (null == getLastNonConfigurationInstance()) { 
     // if splash screen is defined, then show it 
     if (m_activityInfo.metaData.containsKey("android.app.splash_screen")) 
      setContentView(m_activityInfo.metaData.getInt("android.app.splash_screen")); 
     startApp(true); 
    } 
} 

回答

2

你可能會去嘗試,並監聽音量按鍵和修改音頻的音量流的方式。抵制衝動。 Android提供了方便的setVolumeControlStream()方法來將音量按鍵指向您指定的音頻流。

看看這個,我想可能會有所幫助:
Use Hardware Volume Keys to Control Your App’s Audio Volume

編輯:

oncreate方法,你必須做的:

this.setVolumeControlStream(AudioManager.STREAM_MUSIC); //if you use AudioManager.STREAM_MUSIC to load the sound 
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { 
     @Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, 
      int status) { 
     loaded = true; 
     } 
    }); 
    soundPool.load(this, R.raw.sound1, 1); // your sound 
+0

我已嘗試在我的onCreate()方法中使用setVolumeControlStream(),並且它沒有改變任何東西。 – kobihudson

+0

如果您向我們展示您已經編寫的代碼,我們會很有幫助。 – Dyna

+0

我只是將一些代碼添加到原始文章中。 – kobihudson

相關問題