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 all 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
3 changes: 2 additions & 1 deletion .github/workflows/gradle-pr-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ jobs:
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
distribution: 'zulu'
java-package: jdk+fx
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build and analyze with Gradle
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,27 @@ public static int calculateLuhnChecksum(long num) {
ImageIO.write(image, "png", new File(filename));
}
```
### Play Video

```java
public class PlayVideoSnippet extends Application {

public static void main(String[] args) {
Application.launch(PlayVideoSnippet.class, args);
}

public void start(Stage stage)throws InterruptedException{
var media = new Media(new File(getParameters().getRaw().get(0)).toURI().toString());
var mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
var mediaView = new MediaView(mediaPlayer);
var scene = new Scene(new Group(), media.getWidth(), media.getHeight());
stage.setScene(scene);
((Group) scene.getRoot()).getChildren().add(mediaView);
stage.show();
}
}
```

## 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 "org.sonarqube" version "3.0"
id 'org.cadixdev.licenser' version '0.6.1'
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_17
targetCompatibility = JavaVersion.VERSION_17
}
javafx {
version = "19"
modules = [ 'javafx.controls' , 'javafx.fxml', 'javafx.media']
}

configurations {
checkstyleConfig
Expand Down Expand Up @@ -53,6 +58,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'
testImplementation "org.testfx:testfx-junit5:4.0.16-alpha"

checkstyleConfig dependencies.create("com.puppycrawl.tools:checkstyle:${checkstyle.toolVersion}") {
transitive = false
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/media/PlayVideoSnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 {

/**
* Main method that launch the video player.
*
* @param args : should contain one arg, referring to path of video file to play.
*/
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) {
var media = new Media(new File(getParameters().getRaw().get(0)).toURI().toString());
var mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
var mediaView = new MediaView(mediaPlayer);
var scene = new Scene(new Group(), media.getWidth(), media.getHeight());
stage.setScene(scene);
((Group) scene.getRoot()).getChildren().add(mediaView);
stage.show();
}
}
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());
}
}
12 changes: 10 additions & 2 deletions src/test/java/file/ListFilesInDirectorySnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;


Expand All @@ -47,13 +48,20 @@ void testListFilesInDirectory() {
var files = ListFilesInDirectorySnippet.listFilesInDirectory(
Paths.get("src", "test", "resources").toString()
);
assertEquals(2, files.length);
var filenames = new HashSet<>(Arrays.asList(files[0].toString(), files[1].toString()));
assertEquals(4, files.length);
var filenames = new HashSet<>(Arrays.stream(files).map(File::toString)
.collect(Collectors.toList()));
assertTrue(filenames.contains(
Paths.get("src", "test", "resources", "somelines.txt").toString()
));
assertTrue(filenames.contains(
Paths.get("src", "test", "resources", "someotherlines.txt").toString()
));
assertTrue(filenames.contains(
Paths.get("src", "test", "resources", "video.mp4").toString()
));
assertTrue(filenames.contains(
Paths.get("src", "test", "resources", "video.wmv").toString()
));
}
}
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));
}
}
2 changes: 1 addition & 1 deletion src/test/java/network/HttpPostSnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void testHttpPost() throws IOException, InterruptedException {
var result = HttpPostSnippet.httpPost("https://postman-echo.com/post", arguments);
//This postman endpoint echoes the HTTP headers, request parameters, the contents
//of the request body and the complete URI requested.
var echoedData = "\"data\":\"Hello World\"";
var echoedData = "\"data\": \"Hello World\"";
assertThat(result.body(), containsString(echoedData));
}
}
Binary file added src/test/resources/video.mp4
Binary file not shown.
Binary file added src/test/resources/video.wmv
Binary file not shown.