有一个代码可以通过ExoPlayer 2播放来自互联网的音频。当来电声音被静音为0时,以及当通话结束时,如何恢复到原来的声音?
public class Player extends AppCompatActivity {
private String stream;
private boolean isPlay;
private ImageButton btnPLayPause;
BandwidthMeter bandwidthMeter;
TrackSelector trackSelector;
SimpleExoPlayer player;
private SimpleExoPlayerView simpleExoPlayerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
btnPLayPause = (ImageButton) findViewById(R.id.btnPLayPause);
stream = "http://site.ru/radio.mp3";
bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory streamSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(streamSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, new DefaultLoadControl());
simpleExoPlayerView = new SimpleExoPlayerView(this);
simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view);
simpleExoPlayerView.setUseController(true);
simpleExoPlayerView.requestFocus();
simpleExoPlayerView.setPlayer(player);
Uri streamUri = Uri.parse(stream);
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "yourApplicationName"), bandwidthMeter);
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(streamUri, dataSourceFactory, extractorsFactory, null, null);
player.prepare(mediaSource);
player.setPlayWhenReady(true);
isPlay = true;
if (player.getPlayWhenReady() == true) {
btnPLayPause.setImageResource(R.drawable.icon_stop);
}
}
@Override
protected void onDestroy() {
NotificationManager notifManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();
super.onDestroy();
player.release();
}
public void onPlayPause(View view) {
if (isPlay == true) {
if (player.getPlayWhenReady() == true) {
player.setPlayWhenReady(false);
isPlay = false;
btnPLayPause.setImageResource(R.drawable.icon_play);
}
} else if (isPlay == false) {
if (player.getPlayWhenReady() == false) {
player.setPlayWhenReady(true);
isPlay = true;
btnPLayPause.setImageResource(R.drawable.icon_stop);
}
}
}
}
希望给出类似示例的示例或链接
答案 0 :(得分:0)
你需要实现PhoneStateListener,类似:
class TeleListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// CALL_STATE_IDLE;
setVolumeOn();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK; call ends
break;
case TelephonyManager.CALL_STATE_RINGING:
// CALL_STATE_RINGING
silent();
break;
default:
break;
}
}
}
silent()和setVolumeOn()取决于你,基本上你必须将当前的音量保持在silent()并静音音频,然后在setVolumeOn()上恢复它
这是你如何注册(开始听):
private TeleListener teleListener;
private TelephonyManager telephonyMgr;
public void start() {
teleListener = new TeleListener();
telephonyMgr.listen(teleListener, PhoneStateListener.LISTEN_CALL_STATE);
}
取消注册(停止听):
public void stop() {
telephonyMgr.listen(teleListener, PhoneStateListener.LISTEN_NONE);
}