I’m trying to learn about these issues and I need support.
I have a video camera and I want to take images from this camera. Video H264
I can access the image by typing the rtsp address in VLC player.
rtsp://admin:[email protected]:554/Profile1/Media.smp
I’m trying to make an apk that will work on an android TV. I want to watch the image coming from rtsp address on the screen.
language : java
Build configuration language : Groovy DSL(build.gradle)
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.video.cam">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.media3.ui.PlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:show_buffering="when_playing"
app:resize_mode="fit" />
</FrameLayout>
MainActivity.java
package com.video.cam;
import android.os.Bundle;
import androidx.annotation.OptIn;
import androidx.appcompat.app.AppCompatActivity;
import androidx.media3.common.MediaItem;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.rtsp.RtspMediaSource;
import androidx.media3.ui.PlayerView;
public class MainActivity extends AppCompatActivity {
private PlayerView playerView;
private ExoPlayer player;
@OptIn(markerClass = UnstableApi.class)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playerView = findViewById(R.id.player_view);
player = new ExoPlayer.Builder(this).build();
playerView.setPlayer(player);
String videoUrl = "rtsp://admin:[email protected]:554/Profile1/Media.smp";
MediaItem mediaItem = new MediaItem.Builder().setUri(videoUrl).build();
RtspMediaSource.Factory mediaSourceFactory = new RtspMediaSource.Factory();
mediaSourceFactory.setForceUseRtpTcp(true);
RtspMediaSource mediaSource = mediaSourceFactory.createMediaSource(mediaItem);
player.setMediaSource(mediaSource);
player.prepare();
player.play();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (player != null) {
player.release();
}
}
}
build.gradle(Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:8.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(app)
apply plugin: 'com.android.application'
android {
namespace 'com.video.cam'
compileSdkVersion 33
defaultConfig {
applicationId "com.video.cam"
minSdkVersion 21
targetSdkVersion 33
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'androidx.core:core-ktx:1.13.1'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.media3:media3-exoplayer-rtsp:1.3.1'
implementation 'androidx.media3:media3-exoplayer:1.3.1'
implementation 'androidx.media3:media3-ui:1.3.1'
}
When I try to compile the project I get the following error.
java.lang.NullPointerException
Can you please help me with this work?
Mustafa TOPAL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.