Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and fixed BUG java.lang.NullPointerException: MediaSource.Factory#setDrmSessionManagerProvider #1085

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ group 'com.jhomlala.better_player.better_player'
version '1.0-SNAPSHOT'

buildscript {
ext.exoPlayerVersion = "2.17.1"
ext.lifecycleVersion = "2.4.0-beta01"
ext.annotationVersion = "1.2.0"
ext.workVersion = "2.7.0"
ext.exoPlayerVersion = "2.18.1"
ext.lifecycleVersion = "2.6.0-alpha02"
ext.annotationVersion = "1.4.0"
ext.workVersion = "2.7.1"
ext.coreVersion = "1.6.0"
ext.gradleVersion = "7.0.2"
ext.kotlinVersion = "1.5.31"
ext.gradleVersion = "7.3.0"
ext.kotlinVersion = "1.7.10"

repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:4.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
Expand All @@ -32,7 +32,7 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 31
compileSdkVersion 33

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -48,7 +48,7 @@ android {
}

defaultConfig {
minSdkVersion 16
minSdkVersion 21
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
81 changes: 44 additions & 37 deletions android/src/main/kotlin/com/jhomlala/better_player/BetterPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
Expand Down Expand Up @@ -32,7 +31,6 @@ import com.google.android.exoplayer2.drm.FrameworkMediaDrm
import com.google.android.exoplayer2.drm.UnsupportedDrmException
import com.google.android.exoplayer2.drm.DummyExoMediaDrm
import com.google.android.exoplayer2.drm.LocalMediaDrmCallback
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.ClippingMediaSource
import com.google.android.exoplayer2.ui.PlayerNotificationManager.MediaDescriptionAdapter
Expand All @@ -42,6 +40,7 @@ import android.support.v4.media.session.PlaybackStateCompat
import android.support.v4.media.MediaMetadataCompat
import android.util.Log
import android.view.Surface
import androidx.annotation.RequiresApi
import androidx.lifecycle.Observer
import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource
import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource
Expand All @@ -51,14 +50,12 @@ import com.google.android.exoplayer2.source.hls.HlsMediaSource
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory
import io.flutter.plugin.common.EventChannel.EventSink
import androidx.media.session.MediaButtonReceiver
import androidx.work.Data
import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.drm.DrmSessionManagerProvider
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector.SelectionOverride
import com.google.android.exoplayer2.trackselection.TrackSelectionOverrides
import com.google.android.exoplayer2.trackselection.TrackSelectionOverride
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultDataSource
import com.google.android.exoplayer2.util.Util
Expand Down Expand Up @@ -214,6 +211,7 @@ internal class BetterPlayer(
return title
}

@RequiresApi(Build.VERSION_CODES.M)
@SuppressLint("UnspecifiedImmutableFlag")
override fun createCurrentContentIntent(player: Player): PendingIntent? {
val packageName = context.applicationContext.packageName
Expand Down Expand Up @@ -387,13 +385,13 @@ internal class BetterPlayer(
if (lastPathSegment == null) {
lastPathSegment = ""
}
type = Util.inferContentType(lastPathSegment)
type = Util.inferContentTypeForExtension(lastPathSegment)
} else {
type = when (formatHint) {
FORMAT_SS -> C.TYPE_SS
FORMAT_DASH -> C.TYPE_DASH
FORMAT_HLS -> C.TYPE_HLS
FORMAT_OTHER -> C.TYPE_OTHER
FORMAT_SS -> C.CONTENT_TYPE_SS
FORMAT_DASH -> C.CONTENT_TYPE_DASH
FORMAT_HLS -> C.CONTENT_TYPE_HLS
FORMAT_OTHER -> C.CONTENT_TYPE_OTHER
else -> -1
Comment on lines 385 to 395
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed to:

        val type: Int = if (formatHint == null) {
            Util.inferContentType(uri)
        } else {
            when (formatHint) {
                FORMAT_SS -> C.CONTENT_TYPE_SS
                FORMAT_DASH -> C.CONTENT_TYPE_DASH
                FORMAT_HLS -> C.CONTENT_TYPE_HLS
                FORMAT_OTHER -> C.CONTENT_TYPE_OTHER
                else -> -1
            }
        }

Because Util.inferContentTypeForExtension(lastPathSegment) works incorrectly for the segment, it should be the extension of file/uri (e.g. mpd or m3u8). Please, fix the PR.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, it is for ExoPlayer 2.18.1 (Android) and above.

}
}
Expand All @@ -407,28 +405,38 @@ internal class BetterPlayer(
drmSessionManager?.let { drmSessionManager ->
drmSessionManagerProvider = DrmSessionManagerProvider { drmSessionManager }
}

return when (type) {
C.TYPE_SS -> SsMediaSource.Factory(
C.CONTENT_TYPE_SS -> SsMediaSource.Factory(
DefaultSsChunkSource.Factory(mediaDataSourceFactory),
DefaultDataSource.Factory(context, mediaDataSourceFactory)
)
.setDrmSessionManagerProvider(drmSessionManagerProvider)
.createMediaSource(mediaItem)
C.TYPE_DASH -> DashMediaSource.Factory(
).apply {
if (drmSessionManagerProvider != null) {
setDrmSessionManagerProvider(drmSessionManagerProvider!!)
}
}.createMediaSource(mediaItem)
C.CONTENT_TYPE_DASH -> DashMediaSource.Factory(
DefaultDashChunkSource.Factory(mediaDataSourceFactory),
DefaultDataSource.Factory(context, mediaDataSourceFactory)
)
.setDrmSessionManagerProvider(drmSessionManagerProvider)
.createMediaSource(mediaItem)
C.TYPE_HLS -> HlsMediaSource.Factory(mediaDataSourceFactory)
.setDrmSessionManagerProvider(drmSessionManagerProvider)
.createMediaSource(mediaItem)
C.TYPE_OTHER -> ProgressiveMediaSource.Factory(
).apply {
if (drmSessionManagerProvider != null) {
setDrmSessionManagerProvider(drmSessionManagerProvider!!)
}
}.createMediaSource(mediaItem)
C.CONTENT_TYPE_HLS -> HlsMediaSource.Factory(mediaDataSourceFactory)
.apply {
if (drmSessionManagerProvider != null) {
setDrmSessionManagerProvider(drmSessionManagerProvider!!)
}
}.createMediaSource(mediaItem)
C.CONTENT_TYPE_OTHER -> ProgressiveMediaSource.Factory(
mediaDataSourceFactory,
DefaultExtractorsFactory()
)
.setDrmSessionManagerProvider(drmSessionManagerProvider)
.createMediaSource(mediaItem)
).apply {
if (drmSessionManagerProvider != null) {
setDrmSessionManagerProvider(drmSessionManagerProvider!!)
}
}.createMediaSource(mediaItem)
else -> {
throw IllegalStateException("Unsupported type: $type")
}
Expand Down Expand Up @@ -508,15 +516,16 @@ internal class BetterPlayer(
val audioComponent = exoPlayer?.audioComponent ?: return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
audioComponent.setAudioAttributes(
AudioAttributes.Builder().setContentType(C.CONTENT_TYPE_MOVIE).build(),
AudioAttributes.Builder().setContentType(C.AUDIO_CONTENT_TYPE_MOVIE).build(),
!mixWithOthers
)
} else {
audioComponent.setAudioAttributes(
AudioAttributes.Builder().setContentType(C.CONTENT_TYPE_MUSIC).build(),
AudioAttributes.Builder().setContentType(C.AUDIO_CONTENT_TYPE_MUSIC).build(),
!mixWithOthers
)
}

}

fun play() {
Expand Down Expand Up @@ -707,14 +716,12 @@ internal class BetterPlayer(
if (mappedTrackInfo != null) {
val builder = trackSelector.parameters.buildUpon()
.setRendererDisabled(rendererIndex, false)
.setTrackSelectionOverrides(
TrackSelectionOverrides.Builder().addOverride(
TrackSelectionOverrides.TrackSelectionOverride(
mappedTrackInfo.getTrackGroups(
rendererIndex
).get(groupIndex)
)
).build()
.addOverride(
TrackSelectionOverride(
mappedTrackInfo.getTrackGroups(rendererIndex).get(groupIndex),
mappedTrackInfo.getTrackGroups(rendererIndex)
.indexOf(mappedTrackInfo.getTrackGroups(rendererIndex).get(groupIndex))
)
)

trackSelector.setParameters(builder)
Expand Down Expand Up @@ -771,8 +778,8 @@ internal class BetterPlayer(
//Clear cache without accessing BetterPlayerCache.
fun clearCache(context: Context?, result: MethodChannel.Result) {
try {
context?.let { context ->
val file = File(context.cacheDir, "betterPlayerCache")
context?.let {
val file = File(it.cacheDir, "betterPlayerCache")
deleteDirectory(file)
}
result.success(null)
Expand Down
6 changes: 3 additions & 3 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 31
compileSdkVersion 33

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand All @@ -38,8 +38,8 @@ android {

defaultConfig {
applicationId "com.jhomlala.better_player_example"
minSdkVersion 16
targetSdkVersion 31
minSdkVersion 21
targetSdkVersion 33
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
Expand Down
4 changes: 2 additions & 2 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
ext.kotlinVersion = "1.5.31"
ext.gradleVersion = "7.0.2"
ext.kotlinVersion = "1.7.10"
ext.gradleVersion = "7.3.0"
ext.multidexVersion = "2.0.1"
repositories {
google()
Expand Down
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip

3 changes: 2 additions & 1 deletion lib/src/core/better_player_controller.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:io';

import 'package:better_player/better_player.dart';
import 'package:better_player/src/configuration/better_player_controller_event.dart';
import 'package:better_player/src/core/better_player_utils.dart';
Expand Down Expand Up @@ -1260,7 +1261,7 @@ class BetterPlayerController {
///cache started for given [betterPlayerDataSource] then it will be ignored.
Future<void> stopPreCache(
BetterPlayerDataSource betterPlayerDataSource) async {
return VideoPlayerController?.stopPreCache(betterPlayerDataSource.url,
return VideoPlayerController.stopPreCache(betterPlayerDataSource.url,
betterPlayerDataSource.cacheConfiguration?.key);
}

Expand Down
3 changes: 1 addition & 2 deletions lib/src/hls/hls_parser/drm_init_data.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:collection/collection.dart';
import 'package:flutter/cupertino.dart';

import 'scheme_data.dart';

Expand All @@ -19,5 +18,5 @@ class DrmInitData {
}

@override
int get hashCode => hashValues(schemeType, schemeData);
int get hashCode => Object.hash(schemeType, schemeData);
}
3 changes: 1 addition & 2 deletions lib/src/hls/hls_parser/hls_track_metadata_entry.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:better_player/src/hls/hls_parser/variant_info.dart';
import 'package:collection/collection.dart';
import 'package:flutter/rendering.dart';

class HlsTrackMetadataEntry {
HlsTrackMetadataEntry({this.groupId, this.name, this.variantInfos});
Expand Down Expand Up @@ -28,5 +27,5 @@ class HlsTrackMetadataEntry {
}

@override
int get hashCode => hashValues(groupId, name, variantInfos);
int get hashCode => Object.hash(groupId, name, variantInfos);
}
4 changes: 1 addition & 3 deletions lib/src/hls/hls_parser/scheme_data.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'dart:typed_data';

import 'package:flutter/material.dart';

class SchemeData {
SchemeData({
// @required this.uuid,
Expand Down Expand Up @@ -49,7 +47,7 @@ class SchemeData {
}

@override
int get hashCode => hashValues(
int get hashCode => Object.hash(
/*uuid, */
licenseServerUrl,
mimeType,
Expand Down
4 changes: 1 addition & 3 deletions lib/src/hls/hls_parser/variant_info.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'package:flutter/material.dart';

class VariantInfo {
VariantInfo({
this.bitrate,
Expand Down Expand Up @@ -41,6 +39,6 @@ class VariantInfo {
}

@override
int get hashCode => hashValues(
int get hashCode => Object.hash(
bitrate, videoGroupId, audioGroupId, subtitleGroupId, captionGroupId);
}
Loading