2013-12-17 51 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

相关问题