2017-06-29 37 views
0

當使用Android的澳新VolumeShaper,我試圖用MediaPlayer的創建:MediaPlayer.createVolumeShaper拋出IllegalArgumentException:無效的配置或操作:-19

// Create a VolumeShaper configuration 
VolumeShaper.Configuration volumeShaperConfig = 
    new VolumeShaper.Configuration.Builder() 
        .setDuration(3000) 
        .setCurve(new float[] {0.f, 1.f}, new float[] {0.f, 1.f}) 
        .setInterpolatorType(VolumeShaper.Configuration.INTERPOLATOR_TYPE_LINEAR) 
        .build(); 
mVolumeShaper = mMediaPlayer.createVolumeShaper(configuration); 
mMediaPlayer.setDataSource(context, uri); 
mMediaPlayer.prepareAsync(); 

當我嘗試運行它,然而,它拋出一個異常:

Caused by: java.lang.IllegalArgumentException: invalid configuration or operation: -19 
    at android.media.VolumeShaper.applyPlayer(VolumeShaper.java:189) 
    at android.media.VolumeShaper.<init>(VolumeShaper.java:54) 
    at android.media.MediaPlayer.createVolumeShaper(MediaPlayer.java:1392) 

回答

0

爲了創建一個VolumeShaper,該MediaPlayer對象必須是在「初始化」狀態,這要求它setDataSource後發生。 (參見:MediaPlayer state diagram)。

在這種情況下,它爲改變代碼做的順序作爲簡單:

mMediaPlayer.setDataSource(context, uri); 
mMediaPlayer.prepareAsync(); 
mVolumeShaper = mMediaPlayer.createVolumeShaper(configuration); 

也有可能推遲VolumeShaper的創作,直到呼籲MediaPlayer.start(),值得注意的是,使用上面的配置,音量將開始靜音,因此您需要在開始播放或輸出無聲時應用VolumeShaper

要做到這一點,就用這個:

public void play() { 
    mMediaPlayer.start(); 
    mVolumeShaper.apply(VolumeShaper.Operation.PLAY); 
} 

使其靜音,暫停或軌道在結束之前,只是將其應用於相反,像這樣:

public void setMuted(boolean muted) { 
    if (muted) { 
     mVolumeShaper.apply(VolumeShaper.Operation.REVERSE); 
    } else { 
     mVolumeShaper.apply(VolumeShaper.Operation.PLAY); 
    } 
} 
相關問題