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

Added snippet to play video using javaFX #141

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
50079cc
Added media files
RohitPatel1122 Oct 27, 2022
e162d99
Added code for play video
RohitPatel1122 Oct 27, 2022
ddc4b03
Added code snippet to README.md
RohitPatel1122 Oct 27, 2022
b3cc0f4
Added code snippet to README.md
RohitPatel1122 Oct 27, 2022
fcd7d33
Merge branch 'feat/media/play-video' of https://github.com/RohitPatel…
RohitPatel1122 Oct 28, 2022
c6d383f
Renamed test files
RohitPatel1122 Oct 28, 2022
041a88b
Updated Test case to addition of new test resources
RohitPatel1122 Oct 28, 2022
2bade33
resolved merge conflicts
RohitPatel1122 Nov 5, 2022
8365718
Fixed checkstyle issues
RohitPatel1122 Nov 5, 2022
64ad316
update as per review comments
RohitPatel1122 Nov 24, 2022
43bf0cc
Merge branch 'master' of https://github.com/iluwatar/30-seconds-of-ja…
RohitPatel1122 Feb 4, 2023
59adaa0
Grdle file fix
RohitPatel1122 Feb 4, 2023
443264a
Merge remote-tracking branch 'origin/master' into feat/media/play-video
RohitPatel1122 Feb 25, 2023
607687a
Fix missing space
RohitPatel1122 Feb 25, 2023
857e523
Fix missing files assertion
RohitPatel1122 Feb 25, 2023
6a4284d
Convert to zuul
RohitPatel1122 Feb 25, 2023
5c44121
Update gradle-pr-builder.yml
RohitPatel1122 Feb 25, 2023
4c3ecfd
Merge pull request #2 from RohitPatel1122/RohitPatel1122-patch-1
RohitPatel1122 Feb 25, 2023
3509f5f
Merge branch 'master' of https://github.com/RohitPatel1122/30-seconds…
RohitPatel1122 Feb 25, 2023
568363e
Convert to zulu
RohitPatel1122 Feb 25, 2023
0344be5
change to zulu
RohitPatel1122 Feb 25, 2023
dc6ddff
Merge branch 'master' of https://github.com/RohitPatel1122/30-seconds…
RohitPatel1122 Feb 25, 2023
a03fcd5
add jdk+fx
RohitPatel1122 Feb 25, 2023
6443268
Merge branch 'master' of https://github.com/RohitPatel1122/30-seconds…
RohitPatel1122 Feb 25, 2023
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
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ Inspired by [30 seconds of code](https://github.com/Chalarangelo/30-seconds-of-c

### Media

[![Capture screen](https://img.shields.io/badge/-Capture%20screen-e1b050)](#capture-screen) [![link](https://img.shields.io/badge/-Repository%20link-969c56?logo=github)](https://github.com/iluwatar/30-seconds-of-java/blob/master/src/main/java/media/CaptureScreenSnippet.java)
[![Capture screen](https://img.shields.io/badge/-Capture%20screen-e1b050)](#capture-screen) [![link](https://img.shields.io/badge/-Repository%20link-969c56?logo=github)](https://github.com/iluwatar/30-seconds-of-java/blob/master/src/main/java/media/CaptureScreenSnippet.java)
[![Play Video](https://img.shields.io/badge/-Play%20video-e1b050)](#play-video) [![link](https://img.shields.io/badge/-Repository%20link-969c56?logo=github)](https://github.com/iluwatar/30-seconds-of-java/blob/master/src/main/java/media/PlayVideoSnippet.java)

### Networking

Expand Down Expand Up @@ -589,6 +590,24 @@ public static int calculateLuhnChecksum(long num) {
ImageIO.write(image, "png", new File(filename));
}
```
### Play Video

```java
public void start(Stage stage)throws InterruptedException{
//args will contain the media url to play. We can add File selector to show popup
Media media = new Media(new File(getParameters().getRaw().get(0)).toURI().toString());
media.setOnError(()->Platform.exit());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
mediaPlayer.setOnEndOfMedia(()->Platform.exit());
MediaView mediaView = new MediaView(mediaPlayer);
Scene scene = new Scene(new Group(),media.getWidth(),media.getHeight());
stage.setScene(scene);
stage.setTitle("Video Player");
((Group)scene.getRoot()).getChildren().add(mediaView);
stage.show();
}
```
RohitPatel1122 marked this conversation as resolved.
Show resolved Hide resolved

## Networking

Expand Down
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id "com.github.hierynomus.license" version "0.15.0"
id "org.sonarqube" version "3.0"
id 'org.openjfx.javafxplugin' version '0.0.13'
}

// Apply the java plugin to add support for Java
Expand All @@ -10,6 +11,10 @@ java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
javafx {
version = "19"
modules = [ 'javafx.controls' , 'javafx.fxml', 'javafx.media']
}

configurations {
checkstyleConfig
Expand Down Expand Up @@ -60,6 +65,7 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.6.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
testCompile "org.testfx:testfx-junit5:4.0.16-alpha"

checkstyleConfig dependencies.create("com.puppycrawl.tools:checkstyle:${checkstyle.toolVersion}") {
transitive = false
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/media/PlayVideoSnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* MIT License
*
* Copyright (c) 2017-2019 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package media;

import java.io.File;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;

/**
* PlayVideoSnippet.
*
*/

public class PlayVideoSnippet extends Application {
private Media media = null;
private MediaPlayer mediaPlayer = null;
private MediaView mediaView = null;
private Scene scene = null;
RohitPatel1122 marked this conversation as resolved.
Show resolved Hide resolved

public static void main(String[] args) {
Application.launch(PlayVideoSnippet.class, args);
}
RohitPatel1122 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Plays video.
*
* @param stage the primary stage for this application,
* onto which the application scene can be set.
* Applications may create other stages, if needed,
* but they will not be primary stages.
*/

@Override
public void start(Stage stage) {
//args will contain the media url to play. We can add File selector to show popup
media = new Media(new File(getParameters().getRaw().get(0)).toURI().toString());
media.setOnError(() -> Platform.exit());
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
mediaPlayer.setOnEndOfMedia(() -> Platform.exit());
mediaView = new MediaView(mediaPlayer);
scene = new Scene(new Group(), media.getWidth(), media.getHeight());
stage.setScene(scene);
stage.setTitle("Video Player");
((Group) scene.getRoot()).getChildren().add(mediaView);
stage.show();
}
RohitPatel1122 marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion src/test/java/file/ListAllFilesSnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ class ListAllFilesSnippetTest {
@Test
void testListAllFiles() {
var files = ListAllFilesSnippet.listAllFiles("src/test/resources");
assertEquals(6, files.size());
assertEquals(8, files.size());
}
}
9 changes: 5 additions & 4 deletions src/test/java/file/ListFilesInDirectorySnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ class ListFilesInDirectorySnippetTest {
@Test
void testListFilesInDirectory() {
var files = ListFilesInDirectorySnippet.listFilesInDirectory(new File("src/test/resources"));
assertEquals(2, files.length);
var filenames = new HashSet<>(Arrays.asList(files[0].toString(), files[1].toString()));
assertTrue(filenames.contains("src/test/resources/somelines.txt"));
assertTrue(filenames.contains("src/test/resources/someotherlines.txt"));
assertEquals(4, files.length);
assertTrue(Arrays.asList(files).contains(new File("src/test/resources/somelines.txt")));
assertTrue(Arrays.asList(files).contains(new File("src/test/resources/someotherlines.txt")));
assertTrue(Arrays.asList(files).contains(new File("src/test/resources/video.mp4")));
assertTrue(Arrays.asList(files).contains(new File("src/test/resources/video.wmv")));
}
}
62 changes: 62 additions & 0 deletions src/test/java/media/PlayVideoSnippetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* MIT License
*
* Copyright (c) 2017-2019 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package media;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import javafx.scene.media.MediaException;
import org.junit.jupiter.api.Test;
import org.testfx.framework.junit5.ApplicationTest;

/*
* Tests for 30 Seconds of Java code library
*
*/

class PlayVideoSnippetTest extends ApplicationTest {
/**
* Tests for {@link PlayVideoSnippet#main(String[] args)}}.
*/

final String pathOfSupportedFile = "src/test/resources/video.mp4";
final String pathOfUnsupportedFile = "src/test/resources/video.wmv";


@Test
void testSupportedVideoFilePlaysSuccessfully() throws Exception {
assertDoesNotThrow(() -> PlayVideoSnippetTest.launch(PlayVideoSnippet.class,
new String[]{pathOfSupportedFile}));
}

@Test
void testUnsupportedVideoFileThrowsError() throws Exception {
RuntimeException exception = assertThrows(RuntimeException.class,
() -> PlayVideoSnippetTest.launch(PlayVideoSnippet.class,
new String[]{pathOfUnsupportedFile}));
assertTrue(exception.getCause().getCause().getClass().equals(MediaException.class));
}
}
Binary file added src/test/resources/video.mp4
Binary file not shown.
Binary file added src/test/resources/video.wmv
Binary file not shown.