I have file location in 05-11 22:43:41.793: D/Play audio(9996): /storage/emulated/0/QRSpeech/sounds/Introduction_to_Android_1.3gpp
I want to play this file using MediaPlayer like the code below :
mediaPlayer.setDataSource(tempDestFile);
mediaPlayer.prepare();
mediaPlayer.start();
where the tempDestFile is file path as printed above.
No sound is heard ? why ?
i've had this problem as well and tried several things. Try experimenting with MediaPlayer.create(Uri uri). Get your media's path and, if it's a string, convert it to Uri.
Another reason it could not be working is a context issue. Can you post the full code where you're using the MediaPlayer?
Related
Android 11,
I'm trying to create a publicly accessible folder I can store my media files within, which will contain 1 folder and 1 text file per game type (there could be many), I do not want any other application to have access to the root folder for the exception of file explorers, as the user 'could' have content inside the folder that is R18 restricted or might not, I'm not in control of what content goes in there, it would be nice if I could provide that so that a child doesn't in mistakenly bump into the content while looking for pictures on the device for barnie.
I found some code on GitHub that did just the above on Android 10 and works flawlessly for Android 10, but cannot find anything but blue pills about it when it comes to Android 11, help in the form of example code would be great, yes I know targeting only Android 11 limits me but I'd rather live with the limit than play with many different versions of code.
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
}
startActivityForResult(intent, OPEN_FOLDER_REQUEST_CODE)
EDIT 2
After some hacking around, because 'startActivityForResult(Intent!, Int): Unit' is deprecated. Deprecated in Java, I end up with this, results is a URI, can I now use this to pass files to other API calls now?
pref = getSharedPreferences("myPref", Context.MODE_PRIVATE)
var userFolderData: String? = pref.getString("userFolderData", "")
if (userFolderData=="") {
val getUserFolderData =
registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) {
mytools.debug(it.toString())
pref.edit().putString("userFolderData",it.toString()).apply()
userFolderData = it.toString()
}
getUserFolderData.launch("".toUri())
}
mytools.debug("userFolderData = ${userFolderData}")
EDIT 3
So I ran a test on the URI returned; always the same story not matter what I do, yes that's pretty ugly hack but it's the easiest way I can find to test it.
ivtitleimage.setImageURI("content://com.android.externalstorage.documents/document/primary%3ATest%2FMelsDeck%2Fbendover01.jpg".toUri())
31833-31833/com.example.cardgamexxx E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cardgamexxx, PID: 31833
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cardgamexxx/com.example.cardgamexxx.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord{5a390cf 31833:com.example.cardgamexxx/u0a741} (pid=31833, uid=10741) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
EDIT 4
Right, so user selects folder/creates one with registerForActivityResult code above, I place files in it with Cx File Explorer and automagically drum roll! ...
I do not have read access to the file placed there, anyway thought I'd comment here as I don't want people stumbling on the post and thinking this works, because it doesn't.
On an Android 11 device your app can only create folders on root of external storage in the classic way if it has requested 'all files acces'.
Or it can use Storage Access Framework and for instance ACTION_OPEN_DOCUMENT_TREE to let the user create a folder in root of external storage and then select it.
Currently I am working on some code based on VLCJ to play video content, which is working pretty fine, but I am struggling hard making the setSpu() method work.
Just to mention, when it comes to load an external subtitle, in a file apart from the video file, it is working fine. The problem appears when I try to play subtitles contained in the media file. (e.g. subs contained into a MKV file).
I read carefully GitHub post "setSpu not working #278", and I think that maybe the problem is that I am not invoking the setSpu() method correctly.
To make it simple, I am trying to make it works on the example "uk.co.caprica.vlcj.test.basic.TestPlayer".
On TestPlayer.java class, I loaded all native vlc required libs and configured the mediaPath, and mediaPlayer, so if I execute the class, the media player is built properly, and the video starts playing.
Now, to try make the subtitle work, I reused the button "subTitlesButton" on "PlayerControlsPanel.java". First of all, as the spu to be set is the ID of the TrackDescription, I added the following code, and executed to get the spuDescriptions list:
subTitlesButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(mediaPlayer.getSpuDescriptions());
}
});
When the Sub-titles button is pressed, the following output is get:
spuDescriptions=[TrackDescription[id=-1,description=Deshabilitar], TrackDescription[id=3,description=Pista 1 - [Español]], TrackDescription[id=4,description=Pista 2 - [Inglés]], TrackDescription[id=5,description=Pista 3 - [Español]]]
So, to keep it simple, I just tried to add the following code and execute it:
subTitlesButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(mediaPlayer.getSpuDescriptions());
mediaPlayer.setSpu(3); // TrackDescription[id=3,description=Track 1 - [Spanish]]
}
});
The expected resault would be the subtitle "Track 1 - [Spanish]" with ID=3 to appear on screen, but nothing happens. The video goes on and is being played properly, but the sub-title is not shown.
All the other buttons, work fine when you pressed them, you get the expected result (pause, stop, play, fastforward, rewind, and so on)... so I dont get the point on why media.setSpu() is not working there.
Would be much appreciated some help :)
Thanks in advance.
EDITED The exact problem was that all subtitles contained in the media file (video.mkv) were UTF8 text encoded. I tried to re-mount the video.mkv file with mkvmerge, but this program allways converts SRT files to UTF8 text format.
WORKAROUND convert the SRT files to ASS subtitles format. If the video.mkv contains .ASS subtitles format, the subtitles are always loaded properly by VLC and also by vlcj libs.
Thanks a lot in advance for all the help provided.
If this question can be distilled down to how to use external SPU files with non-ASCII characters, you can try this:
Suppose you have some filename for your external SPU file, the filename containing non-ASCII characters, let's call this spuFileName...
Try:
String asciiFileName = new File(spuFileName)
.toURI()
.toASCIIString();
Or:
String asciiFileName = new File(spuFileName)
.toURI()
.toASCIIString()
.replaceFirst("file:/", "file:///");
Then use asciiFileName instead when you specify the SPU file for vlcj.
If I remember correctly, LibVLC requires ASCII strings on its API. This problem can also show itself if you try and play a video with a filename that contains non-ASCII characters (vlcj detects this and handles it automatically).
But I'm not sure if this really is your problem as given the partial log you posted it looks like VLC has indeed detected the SPU tracks correctly.
On the other hand, if this suggestion does actually work, vlcj could be changed to handle this case (an external SPU file) automatically.
When actually selecting SPU for display, whether the SPU are in a separate file or contained within the video itself, the only thing that matters is the id of the SPU track. vlcj passes this id directly to the LibVLC API method. The fact that the track description strings are not being encoded directly does not matter.
In earlier versions of VLC, this id was actually the index of the SPU track - so 0, 1, 2, 3 and so on.
With the current version of VLC (this was changed around February 2013, I think this means VLC 2.1+) this was fixed to use the actual SPU track identifiers.
So depending on your version of VLC, if the track identifiers are not working for you try just passing an index instead.
I'm newbie using vlcj and would like to know what options could be used to record audio only. In another situation, record only video.
I'm recording audio and video together, using the options below, but the project requires providing options to record audio or video regardless.
String[] options = {
":sout=#transcode{vcodec=mp1v,vb=4096,scale=1,acodec=mpga,ab=128,channels=2,samplerate=44100}:duplicate{dst=file{mux=mpeg1,dst=" + fileName + "},dst=display}", ":input-slave=dshow://hw:0,0" };
I searched in google and also in stackoverflow, but have not found a solution for this specific case.
I appreciate any help possible.
Tks,
acsnrj
You can specify "dummy" for the video or audio codec.
This vlcj example transcodes only audio:
https://github.com/caprica/vlcj/blob/vlcj-2.4.1/src/test/java/uk/co/caprica/vlcj/test/rip/RipAudioTest.java
The relevant code from that example is:
mediaPlayer.playMedia(args[0], "sout=#transcode{acodec=mp3,channels=2,ab=192,samplerate=44100,vcodec=dummy}:standard{dst=" + args[1] + ",mux=raw,access=file}");
you can add "--no-video" option to playMedia function to avoid processing video.
I am trying to use the youtube video gwt api.
The youtube-player works but how can I stop videos? I didnt find a command for that...
I created my player following:
protected YouTubeEmbeddedPlayer _youTubeEmbeddedPlayer;
_youTubeEmbeddedPlayer = new YouTubeEmbeddedPlayer(youTubeVideoID);
That´s the YouTube Player:
https://code.google.com/p/gwt-youtube-api/wiki/EmbededPlayer
By using YouTubePlayerWrapper.
You can stop video by calling the method
youTubePlayerWrapper.stopVideo();
This answer may not resolve the problem mentioned with the same library. In fact I tried multiple different library with every library having some issues. So end up creating my own wrapper. I have made it public check it if you can use it https://github.com/pandurangpatil/gwt-youtube
I'm experimenting with JavaFX making a small game.
I want to add sound.
How?
I tried MediaPlayer with media defined with relative source attribute like:
attribute media = Media{
source: "{__FILE__}/sound/hormpipe.mp3"
}
attribute player = MediaPlayer{
autoPlay:true
media:media
}
It doesn't play.
I get
FX Media Object caught Exception com.sun.media.jmc.MediaUnavailableException: Media unavailable: file: ... Sound.class/sound/hormpipe.mp3
Just a guess, but is that file "hornpipe.mp3" and not "hormpipe.mp3" (with an m)?
var player = javafx.scene.media.MediaPlayer {
repeatCount: javafx.scene.media.MediaPlayer.REPEAT_FOREVER
media: Media { source: "{\_\_DIR\_\_}clip.wav"
};
};
player.play();
You have to incluye the audio file in the build/compiled directory so Netbeans can pack it into the jar file.
Just a guess, but I think your {__FILE__} will expand to the name of your file. Try replacing it with {__DIR__}.
Also note that {__DIR__} includes the trailing /, so try this instead:
attribute media = Media{
source: "{__DIR__}sound/hormpipe.mp3"}
EDIT: I did some digging, and apparently, the source of a Media object has to be either a remote URL, or an absolute file path, since media files aren't allowed in JARs (something I hope gets changed with future releases, since I really like JavaFX and want to be able to make desktop apps with it). See: JavaFX FAQs.
This worked for me:
MediaPlayer audio = new MediaPlayer(
new Media(
new File("file.mp3").toURI().toString()));
Source file should be in project's root directory (not src, not dist).
OK, having used this question to get MP3 audio working (kinda), I've learned the following (not much).
1) Audio for compressed formats is very platform dependent. My continually upgraded Mint 17.1->18 machine plays mp3 fine using Media and MediaPlayer. Fresh installs of Mint 18 won't (with the dev tools).
So use .wav files.
Media sound=new Media(new File("noises/roll.wav").toURI().toString());
MediaPlayer mediaPlayer=new MediaPlayer(sound);
mediaPlayer.play();
2) One of the things you need to be aware of with Media/MediaPlayer is that in order to play multiple times (repeatedly or all at once ie, on a button press/whatever in a game) you have to spawn N number of MediaPlayer objects, and each one will play once and then stop.
So use javafx.scene.media.AudioClip
AudioClip soundMyNoise = new AudioClip(new File("noises/roll.wav").toURI().toString());
soundMyNoise.play();
AudioClip also has its issues, which include storing the raw audio data in RAM all at once instead of buffering. So there is the possibility of excessive memory use.
No matter which method you end up going with, one thing to be critically aware of was mentioned by daevon earlier - the path issue. With NetBeans, you have NetBeansProjects/yourproject/src/yourproject/foo.java. The sounds in the example above go in NetBeansProjects/yourproject/noises/roll.wav