简体   繁体   中英

Get MediaPlayer & AudioManager to play through earpiece instead of speaker?

Hey I'm trying to play a R.raw file thru my earpiece instead of the main phone speaker on my app but everyhting I throw at it isn't working. WIht my current code... it just plays very light static through the earpiece and not the R.raw I want.

Here is my code below and I set it in the manifest: <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

 AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            am.setSpeakerphoneOn(false);
        am.setMode(AudioManager.MODE_IN_CALL); 

        MediaPlayer tellSecret = MediaPlayer.create(this, R.raw.secret);
    tellSecret.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
    tellSecret.start();

Any thoughts?

尝试将音频管理器和媒体播放器模式设置为AudioManager.STREAM_MUSIC。

do not use 'create' api for media player, instead use 'setDataSource' and 'prepare' apis seperately. Also call 'setAudioStreamType' api on mediaplayer instance with STREAM_VOICE_CALL before calling 'setDataSource'.

In the end after starting media player, call setSpeakerphoneOn(false) on audio manager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM