-
Notifications
You must be signed in to change notification settings - Fork 344
ashelke/media drm snippets #826
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.example.compose.snippets.components | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
|
|
||
| object ButtonSnippets { | ||
| // [START android_compose_snippets_button_example] | ||
| @Composable | ||
| fun ButtonExample() { | ||
|
|
||
| } | ||
| // [END android_compose_snippets_button_example] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| plugins { | ||
| id("com.android.library") | ||
| kotlin("android") | ||
| } | ||
|
|
||
| android { | ||
| namespace = "com.example.media.snippets" | ||
| compileSdk = 35 | ||
|
|
||
| defaultConfig { | ||
| minSdk = 21 | ||
| } | ||
|
|
||
| compileOptions { | ||
| sourceCompatibility = JavaVersion.VERSION_17 | ||
| targetCompatibility = JavaVersion.VERSION_17 | ||
| } | ||
|
|
||
| kotlinOptions { | ||
| jvmTarget = "17" | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation("androidx.core:core-ktx:1.15.0") | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||||||||||||||||||||||||||||||
| package com.example.media.snippets.mediaplayer | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import android.media.MediaPlayer | ||||||||||||||||||||||||||||||||||
| import java.util.UUID | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class DRMSnippets { | ||||||||||||||||||||||||||||||||||
| private var mediaPlayer: MediaPlayer? = null | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| fun syncDRM() { | ||||||||||||||||||||||||||||||||||
| // [START android_mediaplayer_drm_sync] | ||||||||||||||||||||||||||||||||||
| mediaPlayer?.apply { | ||||||||||||||||||||||||||||||||||
| setDataSource("https://example.com/video.mp4") | ||||||||||||||||||||||||||||||||||
| setOnDrmConfigHelper { _ -> /* configuration */ } // optional, for custom configuration | ||||||||||||||||||||||||||||||||||
| prepare() | ||||||||||||||||||||||||||||||||||
| drmInfo?.also { | ||||||||||||||||||||||||||||||||||
| prepareDrm(UUID.randomUUID()) | ||||||||||||||||||||||||||||||||||
| getKeyRequest(byteArrayOf(), byteArrayOf(), "", 0, null) | ||||||||||||||||||||||||||||||||||
| provideKeyResponse(byteArrayOf(), byteArrayOf()) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // MediaPlayer is now ready to use | ||||||||||||||||||||||||||||||||||
| start() | ||||||||||||||||||||||||||||||||||
| // ...play/pause/resume... | ||||||||||||||||||||||||||||||||||
| stop() | ||||||||||||||||||||||||||||||||||
| releaseDrm() | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| // [END android_mediaplayer_drm_sync] | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+9
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
I suggest refactoring the function to be self-contained and robust by initializing the fun syncDRM() {
// [START android_mediaplayer_drm_sync]
val mediaPlayer = MediaPlayer()
try {
mediaPlayer.apply {
setDataSource("https://example.com/video.mp4")
setOnDrmConfigHelper { _ -> /* configuration */ } // optional, for custom configuration
prepare()
drmInfo?.also {
try {
prepareDrm(UUID.randomUUID())
getKeyRequest(byteArrayOf(), byteArrayOf(), "", 0, null)
provideKeyResponse(byteArrayOf(), byteArrayOf())
} catch (e: Exception) {
e.printStackTrace()
}
}
// MediaPlayer is now ready to use
start()
// ...play/pause/resume...
stop()
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
mediaPlayer.releaseDrm()
mediaPlayer.release()
}
// [END android_mediaplayer_drm_sync]
} |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // [START android_mediaplayer_drm_async] | ||||||||||||||||||||||||||||||||||
| inner class MyDrmActivity : MediaPlayer.OnDrmInfoListener, MediaPlayer.OnPreparedListener { | ||||||||||||||||||||||||||||||||||
| fun setupAsync(mediaPlayer: MediaPlayer) { | ||||||||||||||||||||||||||||||||||
| mediaPlayer.apply { | ||||||||||||||||||||||||||||||||||
| setOnPreparedListener(this@MyDrmActivity) | ||||||||||||||||||||||||||||||||||
| setOnDrmInfoListener(this@MyDrmActivity) | ||||||||||||||||||||||||||||||||||
| setDataSource("https://example.com/video.mp4") | ||||||||||||||||||||||||||||||||||
| prepareAsync() | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // If the data source content is protected you receive a call to the onDrmInfo() callback. | ||||||||||||||||||||||||||||||||||
| override fun onDrmInfo(mediaPlayer: MediaPlayer, drmInfo: MediaPlayer.DrmInfo) { | ||||||||||||||||||||||||||||||||||
| mediaPlayer.apply { | ||||||||||||||||||||||||||||||||||
| prepareDrm(UUID.randomUUID()) | ||||||||||||||||||||||||||||||||||
| getKeyRequest(byteArrayOf(), byteArrayOf(), "", 0, null) | ||||||||||||||||||||||||||||||||||
| provideKeyResponse(byteArrayOf(), byteArrayOf()) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+43
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The DRM methods try {
mediaPlayer.apply {
prepareDrm(UUID.randomUUID())
getKeyRequest(byteArrayOf(), byteArrayOf(), "", 0, null)
provideKeyResponse(byteArrayOf(), byteArrayOf())
}
} catch (e: Exception) {
e.printStackTrace()
} |
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // When prepareAsync() finishes, you receive a call to the onPrepared() callback. | ||||||||||||||||||||||||||||||||||
| // If there is a DRM, onDrmInfo() sets it up before executing this callback, | ||||||||||||||||||||||||||||||||||
| // so you can start the player. | ||||||||||||||||||||||||||||||||||
| override fun onPrepared(mediaPlayer: MediaPlayer) { | ||||||||||||||||||||||||||||||||||
| mediaPlayer.start() | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| // [END android_mediaplayer_drm_async] | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package com.example.media.snippets.mediaplayer; | ||
|
|
||
| import android.media.MediaPlayer; | ||
| import java.util.UUID; | ||
|
|
||
| public class DRMSnippetsJava { | ||
| private MediaPlayer mediaPlayer; | ||
|
|
||
| public void syncDRM() { | ||
| // [START android_mediaplayer_drm_sync_java] | ||
| try { | ||
| mediaPlayer.setDataSource("https://example.com/video.mp4"); | ||
| mediaPlayer.setOnDrmConfigHelper(null); // optional, for custom configuration | ||
| mediaPlayer.prepare(); | ||
| if (mediaPlayer.getDrmInfo() != null) { | ||
| mediaPlayer.prepareDrm(UUID.randomUUID()); | ||
| mediaPlayer.getKeyRequest(null, null, null, 0, null); | ||
| mediaPlayer.provideKeyResponse(null, null); | ||
| } | ||
|
|
||
| // MediaPlayer is now ready to use | ||
| mediaPlayer.start(); | ||
| // ...play/pause/resume... | ||
| mediaPlayer.stop(); | ||
| mediaPlayer.releaseDrm(); | ||
| } catch (Exception e) { | ||
| // Handle exceptions | ||
| } | ||
| // [END android_mediaplayer_drm_sync_java] | ||
| } | ||
|
Comment on lines
+9
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function has two critical issues:
I recommend initializing public void syncDRM() {
// [START android_mediaplayer_drm_sync_java]
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("https://example.com/video.mp4");
mediaPlayer.setOnDrmConfigHelper(null); // optional, for custom configuration
mediaPlayer.prepare();
if (mediaPlayer.getDrmInfo() != null) {
mediaPlayer.prepareDrm(UUID.randomUUID());
mediaPlayer.getKeyRequest(null, null, null, 0, null);
mediaPlayer.provideKeyResponse(null, null);
}
// MediaPlayer is now ready to use
mediaPlayer.start();
// ...play/pause/resume...
mediaPlayer.stop();
} catch (Exception e) {
e.printStackTrace();
} finally {
mediaPlayer.releaseDrm();
mediaPlayer.release();
}
// [END android_mediaplayer_drm_sync_java]
} |
||
|
|
||
| // [START android_mediaplayer_drm_async_java] | ||
| public class MyDrmActivity implements MediaPlayer.OnDrmInfoListener, MediaPlayer.OnPreparedListener { | ||
| public void setupAsync(MediaPlayer mediaPlayer) { | ||
| try { | ||
| mediaPlayer.setOnPreparedListener(this); | ||
| mediaPlayer.setOnDrmInfoListener(this); | ||
| mediaPlayer.setDataSource("https://example.com/video.mp4"); | ||
| mediaPlayer.prepareAsync(); | ||
| } catch (Exception e) { | ||
| // Handle exceptions | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| } | ||
|
|
||
| // If the data source content is protected you receive a call to the onDrmInfo() callback. | ||
| @Override | ||
| public void onDrmInfo(MediaPlayer mediaPlayer, MediaPlayer.DrmInfo drmInfo) { | ||
| try { | ||
| mediaPlayer.prepareDrm(UUID.randomUUID()); | ||
| mediaPlayer.getKeyRequest(null, null, null, 0, null); | ||
| mediaPlayer.provideKeyResponse(null, null); | ||
| } catch (Exception e) { | ||
| // Handle exceptions | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| } | ||
|
|
||
| // When prepareAsync() finishes, you receive a call to the onPrepared() callback. | ||
| // If there is a DRM, onDrmInfo() sets it up before executing this callback, | ||
| // so you can start the player. | ||
| @Override | ||
| public void onPrepared(MediaPlayer mediaPlayer) { | ||
| mediaPlayer.start(); | ||
| } | ||
| } | ||
| // [END android_mediaplayer_drm_async_java] | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ButtonExamplefunction is currently empty. To make this snippet useful, it should contain a basic implementation of aButton. I've suggested an example using fully qualified names to avoid needing to add imports. You may need to add a dependency onandroidx.compose.material3if it's not already present.Additionally, the file is missing a newline character at the end, which is a common convention.