2

I want to play the video in AR camera. Totally I have 10 videos and one videoplayer. And I am downloading the video player from the server as an asset bundle with name videoplayer.unit3d and storing into sd card. when I am scanning the imageTarget I am reading the video asset bundle file using AssetBundle.LoadFromFile() function and for the first time its working fine.

If I scan the second imageTarget it is showing following error

"Can't be loaded because another AssetBundle with the same files is already loaded"

I have tried a bundle.Unload(true); and Caching.cleanchache() but its not working throwing same error. Also tried bundle.Unload(false);

 private void loadObject(string resourcePath, string objectName, TrackableBehaviour trackableBehaviuor, string videoUrl)
    {

        Debug.Log("Resource path " + resourcePath + " objectName " + objectName);

        Debug.Log("Video Url from sd card   " + videoUrl);

        FileInfo fileInfo = new FileInfo(resourcePath);

        if (!fileInfo.Exists)
            return;


        Debug.Log("File is present");
        AssetBundle bundle = AssetBundle.LoadFromFile(resourcePath, 0, 0);//www.assetBundle;


        Debug.Log("Bundle data is  " + bundle);
        if (bundle == null)
        {
            AndroidJavaObject jObject = new AndroidJavaObject("com.ezvidya.buzzle.activity.UnityActivity");
            jObject.Call("showErrorDialog");
            return;
        }
        else
        {
            AndroidJavaObject jo = new AndroidJavaObject("com.ezvidya.buzzle.activity.UnityActivity");
            jo.Call("closeScanDialog");
        }

        //Load an asset from the loaded bundle
        AssetBundleRequest bundleRequest = bundle.LoadAssetAsync(objectName, typeof(GameObject));

        //bundle.Unload(false);
        Caching.CleanCache();

        //get object
        GameObject cubeFromSDCard = bundleRequest.asset as GameObject;


        if (cubeFromSDCard != null)
        {
            // instantiate augmentation object and parent to trackable
            GameObject augmentation = (GameObject)GameObject.Instantiate(cubeFromSDCard);

            augmentation.transform.parent = trackableBehaviuor.gameObject.transform;

            augmentation.transform.localPosition = cubeFromSDCard.transform.localPosition;//new Vector3(0f, 0f, 0f);
            augmentation.transform.localRotation = cubeFromSDCard.transform.localRotation;//Quaternion.identity;
            augmentation.transform.localEulerAngles = cubeFromSDCard.transform.localEulerAngles;
            augmentation.transform.localScale = cubeFromSDCard.transform.localScale;// new Vector3(22f, 22f, 22f);

            Debug.Log("$$$$$$$$$$$$$$$$  Local Position from asset object " + cubeFromSDCard.transform.localPosition);
            Debug.Log("$$$$$$$$$$$$$$$$  Local Rotation from asset object " + cubeFromSDCard.transform.localEulerAngles);
            Debug.Log("$$$$$$$$$$$$$$$$  Local Scale from asset object " + cubeFromSDCard.transform.localScale);


            Debug.Log("$$$$$$$$$$$$$$$$  Position from asset object " + cubeFromSDCard.transform.position);
            Debug.Log("$$$$$$$$$$$$$$$$  Rotation from asset object " + cubeFromSDCard.transform.eulerAngles);
            Debug.Log("$$$$$$$$$$$$$$$$  Scale from asset object " + cubeFromSDCard.transform.lossyScale);


            // Caching.CleanCache();
            if (videoUrl != null && videoUrl.Length > 0)
            {
                VideoPlaybackBehaviour video = augmentation.GetComponent<VideoPlaybackBehaviour>();

                video.m_autoPlay = true;
                Debug.Log("Autoplay is " + video.AutoPlay);
                video.m_path = videoUrl;

                // Pause other videos before playing this one
                // Play this video on texture where it left off

                //OnTrackingFound(false);

                if (video != null && video.AutoPlay)
                {
                    VideoPlayerHelper.MediaState state = video.VideoPlayer.GetStatus();
                    if (state == VideoPlayerHelper.MediaState.PAUSED ||
                        state == VideoPlayerHelper.MediaState.READY ||
                        state == VideoPlayerHelper.MediaState.STOPPED)
                    {
                        // Pause other videos before playing this one
                        PauseOtherVideos(video);

                        // Play this video on texture where it left off
                        video.VideoPlayer.Play(false, 0);
                    }
                    else if (state == VideoPlayerHelper.MediaState.REACHED_END)
                    {
                        // Pause other videos before playing this one
                        PauseOtherVideos(video);

                        // Play this video from the beginning
                        video.VideoPlayer.Play(false, 0);
                    }

                }                

            }

            augmentation.gameObject.SetActive(true);
        }

        //bundle.Unload(false);
    }
Programmer
  • 121,791
  • 22
  • 236
  • 328
Sureshkumar S
  • 193
  • 1
  • 2
  • 13
  • it would be great if you can share the code for it so we can look into it. From what you have written it seems your problem is same as this https://stackoverflow.com/questions/12814214/unity-dont-load-the-same-assetbundle-twice but it seems like you have tried all solutions what are mentioned. So it would be great if you can share anything more about it. – killer_mech Oct 14 '17 at 07:51
  • Hi aaded my code. Please tell what is the issue here. – Sureshkumar S Oct 14 '17 at 09:10
  • Which line of code is causing this error? – Programmer Oct 14 '17 at 17:09
  • 1
    How about you try this bundle.Unload(false); and bundle = null; it might be because of some reference is being in memory. Let me know if anything comes up. Here is a reference site i used sometime back when i made assetbundle for my project. You can take reference from this one if you are not able to solve this problem http://www.theappguruz.com/blog/create-and-download-asset-bundle-in-unity. – killer_mech Oct 14 '17 at 21:35
  • If I unload its working with no errors. But the videoplayer creating every time when I am scanning the imageTarget so if i scan 5 time there will be 5 video player like layer how to solve this – Sureshkumar S Oct 16 '17 at 06:26
  • I see... so basically your main question problem seems to be resolved then... did the reference i gave helped you? if you think it is then you can write how it solved it otherwise you can write how you solved it and mark it as answer so it can be helpful to others. – killer_mech Oct 17 '17 at 02:25
  • Though your second question does not seem to related to this so usually you need to create another question but anyways i will answer it .This line "GameObject augmentation = (GameObject)GameObject.Instantiate(cubeFromSDCard);" it is creating multiple instances of video player. whenever you scan it generates another video VideoPlaybackBehaviour. What you do is destroy the old object and let new object be created(not recommend this. always reuse old objects whenever possible) or save it as a reference and next scanned object just change source video keeping the player as it is. Hope this helps. – killer_mech Oct 17 '17 at 02:27
  • Hi I can't ask as another question because its saying I have reached my question limit. I have another doubt the video is not auto playing with the same code. ehat is the problem here. – Sureshkumar S Oct 17 '17 at 09:24
  • That is vuforia SDK iam assuming you have used for it. I have not utilised it yet on an projects so i cannot help you there as for why it is doing. I can say one thing check if the video.autoplay behaviour at anypoint is getting turned to false. If it is then turn it on.And if you have got the answer please write down what it is. It will helpful to others. – killer_mech Oct 17 '17 at 11:13

2 Answers2

8

I have found the answer, After Instantiate the Gameobject just call bundle.Unload(false); it solved my problem.

Sureshkumar S
  • 193
  • 1
  • 2
  • 13
2

I have resolved this problem using add below two lines,

private AssetBundle myLoadedAssetBundle;
string bundleUrl="";
IEnumerator LoadAssetBundle ()
{
    if (myLoadedAssetBundle != null) {
        myLoadedAssetBundle.Unload(false); //scene is unload from here
    }

    while (!Caching.ready)
        yield return null;

    WWW www = WWW.LoadFromCacheOrDownload(bundleUrl, 1);
    yield return www;

    if (!string.IsNullOrEmpty(www.error))
    {
        Debug.Log(www.error);
        yield return null;
    }
    myLoadedAssetBundle = www.assetBundle;
}