Пример Android-приложения с плеером и стримером
Данное приложение может использоваться для публикации WebRTC-видеопотока и воспроизведения любого из следующих типов потоков с Web Call Server:
...
Слева отображается видео с камеры, справа воспроизводится другой поток.
Работа с кодом примера
Для разбора кода возьмем класс StreamingMinActivity.java примера streaming-min, который доступен для скачивания в соответствующей сборке 1.0.1.38.
...
Code Block | ||||
---|---|---|---|---|
| ||||
mPublishButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (mPublishButton.getTag() == null || Integer.valueOf(R.string.action_publish).equals(mPublishButton.getTag())) { ActivityCompat.requestPermissions(StreamingMinActivity.this, new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA}, PUBLISH_REQUEST_CODE); SharedPreferences sharedPref = StreamingMinActivity.this.getPreferences(Context.MODE_PRIVATE); } else { SharedPreferences.Editor editor = sharedPrefmPublishButton.editsetEnabled(false); editor.putString("publish_stream", mPublishStreamView.getText().toString());/** editor.apply(); * Method Stream.stop() is called to unpublish the stream. } else {*/ mPublishButtonpublishStream.setEnabledstop(false); /** publishStream = null; } * Method Stream.stop() is calledView tocurrentFocus unpublish the stream. = getCurrentFocus(); if (currentFocus != null) { */ InputMethodManager inputManager = publishStream.stop((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); publishStream = nullinputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } View currentFocus = getCurrentFocus(); } }); |
6. Публикация потока после предоставления соответствующих прав
Session.createStream(), Stream.publish() код
Code Block | ||||
---|---|---|---|---|
| ||||
case PUBLISH_REQUEST_CODE: { if (currentFocus != null) { InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } }); |
6. Публикация потока после предоставления соответствующих прав
Session.createStream(), Stream.publish() код
Code Block | ||||
---|---|---|---|---|
| ||||
case PUBLISH_REQUEST_CODE: {
if (grantResults.length == 0 ||
grantResults[0] != PackageManager.PERMISSION_GRANTED ||
grantResults[1] != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Permission has been denied by user");
} else {
mPublishButton.setEnabled(false);
/**
* The options for the stream to publish are set.
* The stream name is passed when StreamOptions object is created.
*/
StreamOptions streamOptions = new StreamOptions(mPublishStreamView.getText().toString());
/**
* Uncomment this code to use case WebRTC-as-RTMP. Stream will be republished to your rtmpUrl
*/
//streamOptions.setRtmpUrl("rtmp://192.168.1.100:1935/live2");
/**
* Stream is created with method Session.createStream().
*/
publishStream = session.createStream(streamOptions);
/**
* Callback function for stream status change is added to make appropriate changes in controls of the interface when publishing.
*/
publishStream.on(new StreamStatusEvent() {
@Override
public void onStreamStatus(final Stream stream, final StreamStatus streamStatus) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (StreamStatus.PUBLISHING.equals(streamStatus)) {
mPublishButton.setText(R.string.action_unpublish);
mPublishButton.setTag(R.string.action_unpublish);
} else {
mPublishButton.setText(R.string.action_publish);
mPublishButton.setTag(R.string.action_publish);
}
mPublishButton.setEnabled(true);
mPublishStatus.setText(streamStatus.toString());
}
});
}
});
/**
* Method Stream.publish() is called to publish stream.
*/
publishStream.publish();
Log.i(TAG, "Permission has been granted by user");
}
} |
...
Session.createStream(), Stream.publish() код
Code Block | ||||
---|---|---|---|---|
| ||||
mPlayButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { mPlayButton.setEnabled(false); if (mPlayButton.getTag() == null || Integer.valueOf(R.string.action_play).equals(mPlayButton.getTag())) { /** * The options for the stream to play are set. * The stream name is passed when StreamOptions object is created. */ StreamOptions streamOptions = new StreamOptions(mPlayStreamView.getText().toString()); /** * Stream is created with method Session.createStream(). */ playStream = session.createStream(streamOptions); /** * Callback function for stream status change is added to make appropriate changes in controls of the interface when playing. */ playStream.on(new StreamStatusEvent() { @Override public void onStreamStatus(final Stream stream, final StreamStatus streamStatus) { runOnUiThread(new Runnable() { @Override public void run() { if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED || if (StreamStatus.PLAYING.equals(streamStatus)grantResults[1] != PackageManager.PERMISSION_GRANTED) { Log.i(TAG, "Permission has been denied by user"); } else { mPlayButtonmPublishButton.setText(R.string.action_stopsetEnabled(false); /** * The options for the stream to publish are set. mPlayButton.setTag(R.string.action_stop); * The stream name is passed when StreamOptions object is created. */ StreamOptions }streamOptions else= ifnew StreamOptions(StreamStatus.NOT_ENOUGH_BANDWIDTH.equals(streamStatus)) {mPublishStreamView.getText().toString()); /** * Uncomment this code to use case WebRTC-as-RTMP. Stream will be republished to your rtmpUrl Log.w(TAG, "Not enough bandwidth stream " + stream.getName() + ", consider using lower video resolution or bitrate. " +*/ //streamOptions.setRtmpUrl("rtmp://192.168.1.100:1935/live2"); /** * Stream is created with "Bandwidth " + (Math.round(stream.getNetworkBandwidth() / 1000)) + " " +method Session.createStream(). */ publishStream = session.createStream(streamOptions); "bitrate " + (Math.round(stream.getRemoteBitrate() / 1000))); ... /** * Method Stream.publish() is called to publish stream. } else { */ publishStream.publish(); Log.i(TAG, "Permission has been granted by user"); mPlayButton.setText(R.string.action_play);} } |
7. Воспроизведение потока при нажатии Play
Session.createStream(), Stream.publish() код
Code Block | ||||
---|---|---|---|---|
| ||||
mPlayButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { mPlayButton.setTag(R.string.action_playsetEnabled(false); } if (mPlayButton.getTag() == null || Integer.valueOf(R.string.action_play).equals(mPlayButton.getTag())) { /** mPlayButton.setEnabled(true); * The options for the stream to play are set. mPlayStatus.setText(streamStatus.toString()); * The stream name is passed when StreamOptions object is created. }*/ StreamOptions streamOptions = new StreamOptions(mPlayStreamView.getText().toString()); }); /** } * Stream is created with method }); Session.createStream(). */** playStream = * Method Stream.play() is called to start playback of the stream. session.createStream(streamOptions); ... /** */ * Method playStreamStream.play(); SharedPreferences sharedPref = StreamingMinActivity.this.getPreferences(Context.MODE_PRIVATE); is called to start playback of the stream. SharedPreferences.Editor editor = sharedPref.edit(); */ editorplayStream.putString("play_stream", mPlayStreamView.getText().toString()); editor.apply();... } else { /** * Method Stream.stop() is called to stop playback of the stream. */ playStream.stop(); playStream = null; } View currentFocus = getCurrentFocus(); if (currentFocus != null) { InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } }); |
...