2013-03-02 15 views
5

Tôi đang sử dụng AudioManager để phát âm thanh qua tai nghe ngay trước khi cuộc gọi được đặt để tôi sử dụng bộ mã hóa AudioManager (MODE_IN_CALL).AudioManager MODE_IN_CALL interferences

Tôi không gặp phải bất kỳ sự cố nào ngoại trừ Galaxy S3 nơi sau khi phát nhạc đúng cách, nhạc chuông có vẻ rất bị méo và rất ồn ào. Tôi đã đọc tài liệu về setMode:

The audio mode encompasses audio routing AND the behavior of the telephony 
layer. Therefore this method should only be used by applications that replace 
the platform-wide management of audio settings or the main telephony application. 
In particular, the MODE_IN_CALL mode should only be used by the telephony 
application when it places a phone call, as it will cause signals from the 
radio layer to feed the platform mixer. 

Vì vậy, tôi nghi ngờ có thể có tín hiệu từ bộ trộn nền cho lớp phát sóng radio.

Vì vậy, các mã tôi đang sử dụng là như thế này:

am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
speakerWasOn = am.isSpeakerphoneOn(); 
speakerPrevMode = am.getMode(); 
bthA2dpWasOn = am.isBluetoothA2dpOn(); 
bthScoWasOn = am.isBluetoothScoOn(); 

if (BluetoothBR.bthOn && BluetoothBR.conectarBluetooth()) { 
    am.setMode(AudioManager.STREAM_VOICE_CALL); 
    am.setBluetoothA2dpOn(true); 
    am.setBluetoothScoOn(true); 

} else { 
    am.setSpeakerphoneOn(false); 
    am.setMode(AudioManager.MODE_IN_CALL); 
} 

Sau đó, tôi sử dụng MediaPlayer để chơi các tập tin .mp3 trong một thread tách:

private OnPreparedListener opl = new OnPreparedListener() { 
    public void onPrepared(MediaPlayer mp) { 
    try { 
     mp.setVolume(1.0f, 1.0f); 
     mp.start(); 

    } catch (Throwable e) { 
     e.printStackTrace(); 
     mp.release(); 
    } 
    } 
}; 

private OnCompletionListener ocl = new OnCompletionListener() { 
    public void onCompletion(MediaPlayer mp) { 
    stop(); 
    } 
}; 

public void run() { 
    synchronized (mp) { 
    FileInputStream fis = null; 
    try { 
     fis = new FileInputStream(getMp3FilePath()); 

     mp.setDataSource(fis.getFD()); 
     mp.setOnPreparedListener(opl); 
     mp.setOnCompletionListener(ocl); 
     mp.prepare(); 
     fis.close(); 

    } catch (Exception e) { 
     mp.release(); 
     if (fis != null) { 
     try { 
      fis.close(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     } 
    } 
    } 
} 

public void stop() { 
    if (mp != null) { 
    try { 
     mp.stop(); 
     mp.release(); 
     mp.setVolume(0.5f, 0.5f); 
     mp.reset(); 

    } catch (Exception ignored) { 
    } 
    } 

    if (BluetoothBR.bthOn) { 
    BluetoothBR.desconectarBluetooth(); 
    } 
} 

Và khi âm nhạc được thực hiện tôi gọi:

am.setSpeakerphoneOn(speakerWasOn); 
am.setMode(speakerPrevMode); 
am.setBluetoothA2dpOn(bthA2dpWasOn); 
am.setBluetoothScoOn(bthScoWasOn); 

Điều này chỉ xảy ra trên Samsung Galaxy S3 (afaik) và đã được thử nghiệm ở S2, Huawei, SonyEricsson , và những người khác và hoạt động chính xác.

Bất kỳ ý tưởng nào? Cảm ơn

UPDATE:

Tôi đã phát hiện ra rằng tất cả hoạt động tốt nếu thread đợi 5 giây khi kết thúc âm nhạc và sau khi cài đặt AudioManager tình trạng ban đầu.

am.setSpeakerphoneOn(speakerWasOn); 
am.setMode(speakerPrevMode); 
am.setBluetoothA2dpOn(bthA2dpWasOn); 
am.setBluetoothScoOn(bthScoWasOn); 

long ctime = System.currentTimeMillis(); 
while (System.currentTimeMillis() - ctime < 5000); 
+0

STREAM_VOICE_CALL KHÔNG PHẢI là một chế độ, giá trị của nó là 0 vì vậy bạn đang thực sự đặt chế độ thành MODE_NORMAL cũng là 0. – behelit

Trả lời

1

Tôi đã gặp vấn đề tương tự. Khi tôi nhận xét ra dòng này

am.setMode(AudioManager.MODE_IN_CALL); 

Tất cả đều tốt cho tôi.