Playing video files from custom local URL using Unity VideoPlayer component - android

I am trying to load a video file at runtime and play that video using the Unitys Video player component.The video is located in a folder on the mobile. i receive the URL to the file location(this is tested correct). While looking for the file Unity even finds the file at location but is not able to play it. I receive the following error :
AndroidVideoMedia: Error opening extractor: -10002
There is nothing on the internet regarding this issue as well. One page said something about changing gradle version.
Please note that this works fine on the editor. I have also noticed if putting a file in the persistent data path of the app, then loading and playing is fine. But if the file is kept in any other custom folder, then even after finding the video Unity is not able to play it.
Following is a snippet of the code :
void Start()
{
StartCoroutine("LoadVideoRoutine");
}
IEnumerator LoadVideoRoutine()
{
if(video==null)
yield return null;
string root = _arManager.VideoURLToPlay;
print("This is video url : " + root);
if(!File.Exists(root))
{
print("no video");
yield return null;
}
else if(File.Exists(root))
{
print("Found vid");
video.url = root;
video.Prepare();
// video.Play();
}
while(!video.isPrepared)
{
print("preparing");
yield return null;
}
print("playing");
video.Play();
// video.url = root;
// video.Play();
}
Following is a sample URL where the video is present :
"/storage/emulated/0/MyApp/RaceRecordings/3D2A4AF9-A6EB-4D48-92D2-1B2A6ADC968A.mp4"
In the logs, I got the "Found vid" log indicating that unity finds the file. But immediately when I am setting the URL I got the error.
I am not sure what this is and would appreciate some help in this regard.

Maybe try remove the .mp4. Maybe it doesn't belong to the URL but I don't know much about it.

use thisMagic Exo player library.
can play online and offline video and handle initializing and deint the payer on its own

Related

android mediaplayer with api url

I am trying to run media through android mediaplayer with some API link (thus doesn't have .mp3 in the end of url) as follows:
android code :
headers.put("token", token_value);
myPlayer.setDataSource(
getApplicationContext(),
Uri.parse(url),//url : http://basedomain.com/api/getmp3
headers
);
backend laravel code :
$song_path = "abc.mp3";
$path = storage_path().DIRECTORY_SEPARATOR."somefolder".DIRECTORY_SEPARATOR.$song_path;
$response = new BinaryFileResponse($path);
return $response;
I am getting the binary response from laravel code(tested through postman), however android code is not playing the music from laravel response.
android issue list :
1) W/MediaPlayer: Couldn't open http://basedomain.com/api/getmp3: java.io.FileNotFoundException: No content provider: http://basedomain.com/api/getmp3
2) E/MediaPlayer: error (1, -2147483648)
I've just tried the following code in Android studio targeting Android SDK 28 and it works fine. Make sure your API is actually returning the mp3 file.
Android MediaPlayer docs
try {
MediaPlayer player = new MediaPlayer();
player.setDataSource("https://yourdomain/yourmusic.mp3");
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
Try testing your code using a url to a known public mp3 file to verify that the MediaPlayer code is correct. Once that is working, you can narrow the issue down to either the token not being set correctly or your server not returning the mp3 file correctly. Maybe the response doesn't indicate it is an mp3 file correctly and thus the media player doesn't know how to handle it.
I finally found the reason behind this issue. My backend laravel api call was a POST request. Changing the same to Get request worked for me.
Also changing request from POST to GET was not an issue as my request parameters were sent through the header. (This was done because android mediaplayer's setDataSource method only accepts headers and not body parameters).
In short android mediaplayer is expecting us to use GET request only.

How To: Android OpenCV VideoCapture with File

I am trying to pass a video to the OpenCV VideoCapture class. However when I call the VideoCapture.isOpened() method it always returns false. I have tried two methods:
Saving the video file to the internal memory, context.getFilesDir() => /data/data/package_name/files/VideoToAnalyze/Recording.mp4
and also one to environment.getExternalStorageDirectory() => sdcard/appName/Recording.mp4.
Nothing seems to work here. My question is how do I pass a video file (or what is the correct file path) to a VideoCapture OpenCV object? I've posted some code below as an example. Note that I don't get an error. The file is always found/exists, but when I call isOpened() I always get false.
UPDATE:
So it looks like everyone on the web is saying that OpenCV (I'm using 3.10) is lacking a ffmpeg backend and thus cannot process videos. I'm wondering does anyone know this as a fact ?? And is there a work around. Almost all other alternatives to process videos frame by frame is deathly slow.
String x = getApplicationContext().getFilesDir().getAbsolutePath();
File dir = new File(x + "/VideoToAnalyze");
if(dir.isDirectory()) {
videoFile = new File(dir.getAbsolutePath() + "/Recording1.mp4");
} else {
// handle error
}
if(videoFile.exits(){
String absPath = videoFile.getAbsolutePath();
VideoCapture vc = new VideoCapture();
try{
vc.open(absPath);
} catch (Exception e) {
/// handle error
}
if(!vc.isOpened(){
// this code is always hit
Log.v("VideoCapture", "failed");
} else {
Log.v("VideoCapture", "opened");
.....
Its an old question but nevertheless I had same issue.
Opencv for Android only supports MJPEG codec in AVI container. See this
Just to close this .. I downloaded JavaCV .. included .so files into the android project and then used FFMPEGFrameGrabber.

Can't figure out how to play sound android cordova

Background: using android cordova, phonegap-plugin-push, the file plugin, and the media plugin.
I'm trying to play a sound in /res/raw when a notification comes in when the app is open. I figured out how to do it in iOS, but my android code isn't working. Here it is:
mediaUrl = cordova.file.applicationDirectory + "/res/raw/" + data.sound;
alert(mediaUrl)
var media = new Media(mediaUrl, function (e) {
media.release();
}, function (err) {
console.log("media err", err);
});
media.play();
Unfortunately it is not working. I tried to set forceShow to be true in android, but it won't play the correct sound when the app is open. So I have to improvise. I don't want to waste time fiddling with this when the answer is probably obvious to somebody with more experience. Anybody see what I'm doing wrong?
I had same problem months ago..
My issue was in the path of media file, I had to change it several times until it works.. The trick was the "assets" folder.
var sound = new Media("assets/www/sounds/" + fileName);

Audio will not play on Android using phonegap, but works fine on iOS

It works fine on iOS.
I have looked many answers including these 2:
Play sound on Phonegap app for Android
HTML5 audio not playing in PhoneGap App (Possible to use Media?)
I have tried all of the solutions:
Changing the path to /android_asset/www/ for Android
Using .ogg files
Pre load the audio with a play() / pause().
Using the Media plugin provided by Cordova/Phonegap
Here is my current code:
if(device.platform === "Android") //DIFFERENT PATH FOR ANDROID
{
url = "/android_asset/www/sound.ogg";
}else{
url = "sound.mp3";
}
alert(url);
sound = new Media(url);
sound.play();
Anyone have an ideas?? It feels like I have been going round in circles
I had similar and this solution worked for me
Get complete path of your application using window.location.pathname, this way you dont need to worry about device type, it will give you the complete path including index.html and by using the below function simply strip off index.html from the string and append your media file.
function getPhoneGapPath() {
var path = window.location.pathname;
path = path.substr( path, path.length - 10 ); //strip off index.html
return 'file://' + path;
};
And then
url = getPhoneGapPath()+'sound.ogg'; //OR MP3
alert(url);
sound = new Media(url);
sound.play();
Hope this helps
I finally managed to get it working!!
I first initialising the audio file by finding the full Android path using #Sarim Sidd help above and then preloading the audio file:
//INITIALISE SOUND
if(device.platform === "Android") //DIFFERENT PATH FOR ANDROID
{
//navigator.notification.beep(1);
var path = window.location.pathname;
path = path.substr( path, path.length - 10 ); //strip off index.html
url = 'file://'+path+"sound.ogg";
}else{
url = "sound.mp3";
}
//sound = new Media(url);
sound = new Media(url);
sound.play();
sound.pause();
Then when to play the audio I forced the volume to full (1.0) before calling the play method. For some reason I noticed the audio kept reseting to mute each time I started the app:
//PLAY BELL SOUND THROUGH APP
sound.setVolume(1.0);
sound.play();

Using FileReader to read a video file after capture on android using Phonegap

I am facing a problem in what should have been a simple task of capturing video inside phonegap app. What I am trying to do is to start video recorder, capture a video file, get back the mediaFile and then extract the file path from it and read it using fileReader. I need the file to be in binary base64 so I am using readAsDataURL to read it.
What happens though is as follow. The video is captured correctly, the file path is read correctly. Filereader gets initiated. Then the app crash without the success alert poping up. This happened when I tried to read any file on the sdCard regardless of the video.
Here is the code snippet:
function captureSuccess(mediaFiles) //video captured successfully
{
var i=0;
alert(mediaFiles[i].fullPath);
alert(mediaFiles[i].size);
fileReader = new FileReader();
fileReader.onerror = function () {
alert("failed");
}
fileReader.onload = function (evt) {
alert(evt.target.result);
}
fileReader.readAsDataURL(mediaFiles[i].fullPath);
}
It seems like you shouldn't be using FileReader because that's not what it's for. You're better off trying the VideoPlayer PhoneGap plugin.
https://github.com/phonegap/phonegap-plugins/tree/master/Android/VideoPlayer

Categories

Resources