### Complete Streaming Activity Implementation
Source: https://context7.com/takusemba/rtmppublisher/llms.txt
A full example demonstrating UI integration, publisher initialization, and lifecycle-aware streaming controls.
```kotlin
class StreamingActivity : AppCompatActivity(), PublisherListener {
private lateinit var publisher: Publisher
private lateinit var glView: GLSurfaceView
private lateinit var publishButton: Button
private lateinit var cameraButton: ImageButton
private lateinit var statusLabel: TextView
private val rtmpUrl = "rtmp://live.example.com/app/stream_key"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_streaming)
glView = findViewById(R.id.gl_surface_view)
publishButton = findViewById(R.id.btn_publish)
cameraButton = findViewById(R.id.btn_camera)
statusLabel = findViewById(R.id.status_label)
// Initialize publisher - lifecycle is automatically managed
publisher = Publisher.Builder(this)
.setGlView(glView)
.setUrl(rtmpUrl)
.setSize(720, 1280)
.setAudioBitrate(64000)
.setVideoBitrate(2000000)
.setCameraMode(CameraMode.BACK)
.setListener(this)
.build()
publishButton.setOnClickListener {
if (publisher.isPublishing) {
publisher.stopPublishing()
} else {
publisher.startPublishing()
}
}
cameraButton.setOnClickListener {
publisher.switchCamera()
}
}
// No need to override onResume/onPause for camera management
// The library automatically handles lifecycle events
override fun onStarted() {
publishButton.text = "Stop Streaming"
statusLabel.text = "LIVE"
statusLabel.setTextColor(Color.RED)
cameraButton.isEnabled = true
}
override fun onStopped() {
publishButton.text = "Start Streaming"
statusLabel.text = "Ready"
statusLabel.setTextColor(Color.GRAY)
}
override fun onFailedToConnect() {
Toast.makeText(this, "Failed to connect to server", Toast.LENGTH_SHORT).show()
publishButton.text = "Retry"
}
override fun onDisconnected() {
Toast.makeText(this, "Connection lost", Toast.LENGTH_SHORT).show()
publishButton.text = "Reconnect"
statusLabel.text = "Disconnected"
}
}
```
--------------------------------
### Manage RTMP Streaming with Publisher Interface
Source: https://context7.com/takusemba/rtmppublisher/llms.txt
Use the Publisher interface to initialize, start, stop, and monitor RTMP streams. Requires a GLSurfaceView and a valid RTMP server URL.
```kotlin
// Creating a Publisher with all configuration options
val publisher: Publisher = Publisher.Builder(this)
.setGlView(glSurfaceView) // Required: GLSurfaceView for camera preview
.setUrl("rtmp://your-server.com/live/stream-key") // Required: RTMP server URL
.setSize(Publisher.Builder.DEFAULT_WIDTH, Publisher.Builder.DEFAULT_HEIGHT) // 720x1280
.setAudioBitrate(Publisher.Builder.DEFAULT_AUDIO_BITRATE) // 6400 bps
.setVideoBitrate(Publisher.Builder.DEFAULT_VIDEO_BITRATE) // 100000 bps
.setCameraMode(Publisher.Builder.DEFAULT_MODE) // CameraMode.BACK
.setListener(publisherListener)
.build()
// Start streaming
publisher.startPublishing()
// Check if currently streaming
if (publisher.isPublishing) {
// Stream is active
}
// Switch between front and back camera during streaming
publisher.switchCamera()
// Stop streaming
publisher.stopPublishing()
```
--------------------------------
### Control RTMP Streaming
Source: https://github.com/takusemba/rtmppublisher/blob/master/README.md
Methods to start, stop, and switch cameras during RTMP streaming. Also includes setting up a listener for streaming events.
```kotlin
// start publishing!
publisher.startPublishing()
// switch camera between front and back
publisher.switchCamera()
// stop publishing!
publisher.stopPublishing()
```
```kotlin
publisher.setOnPublisherListener(object: PublisherListener {
override fun onStarted() {
// do something
}
override fun onStopped() {
// do something
}
override fun onFailedToConnect() {
// do something
}
override fun onDisconnected() {
// do something
}
})
```
--------------------------------
### Create Publisher Instance
Source: https://github.com/takusemba/rtmppublisher/blob/master/README.md
Instantiate the Publisher using the Builder pattern. Ensure you provide the GLView, RTMP URL, and other necessary configurations.
```kotlin
val publisher: Publisher = Publisher.Builder(this)
.setGlView(glView)
.setUrl(rtmpUrl)
.setSize(Publisher.Builder.DEFAULT_WIDTH, Publisher.Builder.DEFAULT_HEIGHT)
.setAudioBitrate(Publisher.Builder.DEFAULT_AUDIO_BITRATE)
.setVideoBitrate(Publisher.Builder.DEFAULT_VIDEO_BITRATE)
.setCameraMode(Publisher.Builder.DEFAULT_MODE)
.setListener(this)
.build()
```
--------------------------------
### Configure Publisher with Builder Defaults and Custom Settings
Source: https://context7.com/takusemba/rtmppublisher/llms.txt
Access default builder constants or override them to create custom stream configurations for varying bandwidth and quality needs.
```kotlin
// Default configuration values
Publisher.Builder.DEFAULT_WIDTH = 720 // Video width in pixels
Publisher.Builder.DEFAULT_HEIGHT = 1280 // Video height in pixels
Publisher.Builder.DEFAULT_AUDIO_BITRATE = 6400 // Audio bitrate in bits per second
Publisher.Builder.DEFAULT_VIDEO_BITRATE = 100000 // Video bitrate in bits per second
Publisher.Builder.DEFAULT_MODE = CameraMode.BACK // Default camera
// Custom high-quality configuration example
val highQualityPublisher = Publisher.Builder(activity)
.setGlView(glView)
.setUrl(rtmpUrl)
.setSize(1080, 1920) // Full HD resolution
.setAudioBitrate(128000) // 128 kbps audio
.setVideoBitrate(4000000) // 4 Mbps video
.setCameraMode(CameraMode.FRONT) // Front camera for selfie streaming
.setListener(listener)
.build()
// Low bandwidth configuration example
val lowBandwidthPublisher = Publisher.Builder(activity)
.setGlView(glView)
.setUrl(rtmpUrl)
.setSize(480, 640) // Lower resolution
.setAudioBitrate(32000) // 32 kbps audio
.setVideoBitrate(500000) // 500 kbps video
.build()
```
--------------------------------
### Configure CameraMode for Streaming
Source: https://context7.com/takusemba/rtmppublisher/llms.txt
Select between front and back cameras using the CameraMode enum during publisher initialization or runtime.
```kotlin
// Use back camera (default)
val publisher = Publisher.Builder(activity)
.setGlView(glView)
.setUrl(rtmpUrl)
.setCameraMode(CameraMode.BACK)
.build()
// Use front camera for selfie streaming
val selfiePublisher = Publisher.Builder(activity)
.setGlView(glView)
.setUrl(rtmpUrl)
.setCameraMode(CameraMode.FRONT)
.build()
// Switch camera during live stream
cameraToggleButton.setOnClickListener {
publisher.switchCamera() // Toggles between FRONT and BACK
}
```
--------------------------------
### Implement PublisherListener Interface
Source: https://context7.com/takusemba/rtmppublisher/llms.txt
Handle streaming state changes by implementing the PublisherListener interface in an Activity.
```kotlin
class MainActivity : AppCompatActivity(), PublisherListener {
override fun onStarted() {
// Called when streaming successfully starts
Log.d("Stream", "Streaming started")
runOnUiThread {
statusText.text = "LIVE"
publishButton.text = "Stop"
}
}
override fun onStopped() {
// Called when streaming is intentionally stopped
Log.d("Stream", "Streaming stopped")
runOnUiThread {
statusText.text = "Offline"
publishButton.text = "Go Live"
}
}
override fun onFailedToConnect() {
// Called when initial connection to RTMP server fails
Log.e("Stream", "Failed to connect to server")
runOnUiThread {
Toast.makeText(this, "Connection failed. Check URL and network.", Toast.LENGTH_LONG).show()
}
}
override fun onDisconnected() {
// Called when connection is lost during streaming
Log.e("Stream", "Disconnected from server")
runOnUiThread {
Toast.makeText(this, "Stream disconnected unexpectedly", Toast.LENGTH_SHORT).show()
// Optionally attempt to reconnect
retryConnection()
}
}
}
```
--------------------------------
### Add RtmpPublisher Dependency to Gradle
Source: https://context7.com/takusemba/rtmppublisher/llms.txt
Include this dependency in your app's build.gradle file to add the RtmpPublisher library. Requires minimum SDK 18 and AndroidX AppCompat.
```groovy
// In your app's build.gradle
dependencies {
implementation 'com.github.takusemba:rtmppublisher:1.1.3'
}
```
```xml
// Required permissions in AndroidManifest.xml
//
//
//
```
```xml
// Layout XML for GLSurfaceView
//
```
--------------------------------
### Add RtmpPublisher Dependency
Source: https://github.com/takusemba/rtmppublisher/blob/master/README.md
Include this dependency in your app's build.gradle file to use the RtmpPublisher library.
```groovy
dependencies {
compile 'com.github.takusemba:rtmppublisher:x.x.x'
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.