在响铃时通过蓝牙耳机播放声音? - android

时间:2012-08-18 02:53:28

标签: android bluetooth

我正试图在手机响铃时通过蓝牙耳机播放声音(即代表人名的小wav文件 - 不是自动生成的)。基本上它会取代蓝牙设备的默认铃声,并用呼叫者的名字替换它!......好吧,无论如何......它不起作用。

我已经能够在活动中直接在耳机上播放wav文件,但是当移动到广播接收器的onReceive功能时,当手机响铃时,它无法通过耳机播放相同的声音。

我觉得我一定错过了什么。我可以做些什么来修改这段代码吗?

提前致谢

public void onReceive(Context context, Intent intent) {

    mContext = context;
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

    if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
    {
        System.out.println("Its Ringing");

        AudioManager am = (AudioManager) mContext.getSystemService(mContext.AUDIO_SERVICE); 
        am.setBluetoothScoOn(true); 
        am.startBluetoothSco();
        am.setMode(AudioManager.MODE_NORMAL); 

        try{
            AssetFileDescriptor afd = mContext.getAssets().openFd("my.wav");
            MediaPlayer player = new MediaPlayer();
            player.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);

            player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());

            player.setOnPreparedListener(new OnPreparedListener(){
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });

            player.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
            player.prepareAsync();

        }catch(IOException ioe){
            System.err.println(ioe.getLocalizedMessage());
        }}}

1 个答案:

答案 0 :(得分:0)

您应该在接到电话之前准备/设置媒体播放器,因为这可能需要一些时间。另外,删除am.setMode(AudioManager.MODE_NORMAL);,可以将其替换为am.setMode(AudioManager.STREAM_VOICE_CALL);但它可能没有必要,因为如果您的BT设备配对/设置,您应该已经处于该模式。

相关问题