Пример #1
0
        public void Play(string mediaName)
        {
            //_mp = MediaPlayer.Create(this, R.raw.blah);

            try
            {
                //if (m.IsPlaying)
                {
                    m.Stop();
                    m.Release();
                    m = new MediaPlayer();
                }

                AssetFileDescriptor descriptor = Xamarin.Forms.Forms.Context.Assets.OpenFd(mediaName);
                m.SetDataSource(descriptor.FileDescriptor, descriptor.StartOffset, descriptor.Length);
                descriptor.Close();

                m.Prepare();
                m.SetVolume(1f, 1f);
                //m.Looping = true;
                m.Start();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.StackTrace);
            }
        }
Пример #2
0
 public virtual MediaPlayer createMediaPlayer(Context context, int resid)
 {
     try
     {
         AssetFileDescriptor afd = context.Resources.OpenRawResourceFd(resid);
         MediaPlayer         mp  = new MediaPlayer(context);
         mp.SetDataSource(afd.FileDescriptor);
         afd.Close();
         mp.Prepare();
         return(mp);
     }
     catch (IOException ex)
     {
         Log.Debug(TAG, "create failed:", ex);
         // fall through
     }
     catch (IllegalArgumentException ex)
     {
         Log.Debug(TAG, "create failed:", ex);
         // fall through
     }
     catch (SecurityException ex)
     {
         Log.Debug(TAG, "create failed:", ex);
         // fall through
     }
     return(null);
 }
Пример #3
0
        /**
         * \brief Create the media player and load video
         */
        private void createMediaPlayer()
        {
            mMediaPlayerLock.Lock();
            mMediaControllerLock.Lock();

            mMediaPlayer     = new MediaPlayer();
            mMediaController = new MediaController(this);

            AssetFileDescriptor afd = null;
            bool fileExist          = true;

            try
            {
                afd = Assets.OpenFd(mMovieUrl);
            }
            catch (IOException e)
            {
                fileExist = false;
            }
            if (afd == null)
            {
                fileExist = false;
            }
            try
            {
                if (fileExist)
                {
                    mMediaPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
                    afd.Close();
                }
                else
                {
                    string URL_REGEX          = "^((https?|ftp)://|(www|ftp)\\.)[a-z0-9-]+(\\.[a-z0-9-]+)+((:[0-9]+)|)+([/?].*)?$"; //should be ok
                    Java.Util.Regex.Pattern p = Java.Util.Regex.Pattern.Compile(URL_REGEX);
                    Java.Util.Regex.Matcher m = p.Matcher(mMovieUrl);                                                               //replace with string to compare
                    if (m.Find())
                    {
                        mMediaPlayer.SetDataSource(mMovieUrl);
                    }
                }

                mMediaPlayer.SetDisplay(mHolder);
                mMediaPlayer.PrepareAsync();
                mMediaPlayer.SetOnPreparedListener(this);
                mMediaPlayer.SetOnErrorListener(this);
                mMediaPlayer.SetOnCompletionListener(this);
                mMediaPlayer.SetAudioStreamType(Stream.Music);
            }
            catch (Exception e)
            {
                Log.Error("PikkartFullscreenVideo", "error while creating the MediaPlayer: " + e.ToString());
                prepareForTermination();
                destroyMediaPlayer();
                Finish();
            }

            mMediaControllerLock.Unlock();
            mMediaPlayerLock.Unlock();
        }
        public void CloseFile()
        {
            if (_fd != null)
            {
                _fd.Close();
                //_fd.Dispose();
            }

            if (_player != null)
            {
                //_player.Release();
                _player.Reset();
                //_player.Dispose();
            }
        }
Пример #5
0
        public static void PlayMusic(Context context, string dir, string file, SeekBar seekbar)
        {
            MediaPlayer mp = new MediaPlayer();

            switch (dir)
            {
            case "assets":
                AssetFileDescriptor descriptor = context.Assets.OpenFd(file);
                mp.SetDataSource(descriptor.FileDescriptor, descriptor.StartOffset, descriptor.Length);
                descriptor.Close();
                break;

            case "external":
                Java.IO.File filefd = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, file);
                mp.SetDataSource(context, Android.Net.Uri.FromFile(filefd));
                break;
            }
            mp.Prepare();
            mp.SetVolume(1f, 1f);
            mp.Looping = (true);
            mp.Start();
            seekbar.IncrementSecondaryProgressBy(1);
            seekbar.SecondaryProgressTintMode = PorterDuff.Mode.Lighten;
            seekbar.Max = mp.Duration;
            System.Timers.Timer timer = new System.Timers.Timer(1000);
            timer.Start();

            timer.Elapsed += delegate
            {
                // seekbar.SecondaryProgress = mp.CurrentPosition;
                seekbar.Progress = mp.CurrentPosition;
            };

            mp.Completion += delegate
            {
                timer.Stop();
                mp.Stop();
            };
            //seekbar.ProgressChanged += (s,e)=>
            //{

            //    mp.SeekTo(e.SeekBar.SecondaryProgress);
            //};
        }
        private void PlaySound(int soundRes)
        {
            try
            {
                if (IsSoundEnabled)
                {
                    if (soundRes == 0)
                    {
                        return;
                    }

                    Player = new MediaPlayer();
                    AssetFileDescriptor afd = Context.Resources?.OpenRawResourceFd(soundRes);
                    if (afd == null)
                    {
                        return;
                    }
                    Player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
                    afd.Close();
                    Player.Prepare();
                    Player.Start();

                    Player.Completion += (sender, args) =>
                    {
                        try
                        {
                            Player.Release();
                        }
                        catch (Exception e)
                        {
                            Methods.DisplayReportResultTrack(e);
                        }
                    };
                    Player.Looping = false;
                }
            }
            catch (Exception e)
            {
                Methods.DisplayReportResultTrack(e);
            }
        }
Пример #7
0
        public static void PlaySound(CustomMediaPlayer mp, String asset)
        {
            if (null == mp)
            {
                return;
            }

            AssetManager assetManager = Game1.Activity.ApplicationContext.Assets;

            AssetFileDescriptor afd = null;

            try
            {
                afd = assetManager.OpenFd(asset);
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.PrintStackTrace();
            }

            if (null == afd)
            {
                return;
            }

            mp.Reset();

            mp.SetDataSource(afd.FileDescriptor,
                             afd.StartOffset,
                             afd.Length);

            mp.Prepare();

            afd.Close();

            mp.Start();
        }
Пример #8
0
        Texture2D GetAssetTexture(String texture)
        {
            try {
                AssetManager        assetManager = Activity.ApplicationContext.Assets;
                AssetFileDescriptor afd          = assetManager.OpenFd(texture);
                int size = 0;
                if (null != afd)
                {
                    size = (int)afd.Length;
                    afd.Close();
                    Texture2D result = null;
                    using (Stream inputStream = assetManager.Open(texture, Access.Buffer))
                    {
                        result = Texture2D.FromStream(GraphicsDevice, inputStream);
                        inputStream.Close();
                    }
                    return(result);
                }
            } catch (Exception e) {
                Log.Error(TAG, string.Format("Failed to read application key exception={0}", e));
            }

            return(null);
        }
Пример #9
0
        /**
         * \brief load a media file, either from file or from web
         * @param url file path or url (String)
         * @param playFullscreen whatever should play in fullscreen or in AR
         * @param autoStart auto-start when ready
         * @param seekPosition start position (in milliseconds)
         * @return true on success
         */
        public bool load(string url, bool playFullscreen, bool autoStart, int seekPosition)
        {
            mMediaPlayerLock.Lock();
            mSurfaceTextureLock.Lock();

            //if it's in a different state than NOT_READY don't load it. unload() must be called first!
            if ((mVideoState != VIDEO_STATE.NOT_READY) || (mMediaPlayer != null))
            {
                Log.Warn("Pikkart AR Video", "Already loaded");
                mSurfaceTextureLock.Unlock();
                mMediaPlayerLock.Unlock();
                return(false);
            }
            //if AR video (not fullscreen) was requested create and set the media player
            //we can play video in AR only if a SurfaceTexture has been created
            if (!playFullscreen && (Build.VERSION.SdkInt >= BuildVersionCodes.IceCreamSandwich) && mSurfaceTexture != null)
            {
                mMediaPlayer = new MediaPlayer();
                //first search for the video locally, then check online
                AssetFileDescriptor afd = null;
                bool fileExist          = true;
                try
                {
                    afd = mParentActivity.Assets.OpenFd(url);
                }
                catch (IOException e)
                {
                    fileExist = false;
                }
                if (afd == null)
                {
                    fileExist = false;
                }

                try
                {
                    if (fileExist)
                    {
                        mMovieUrl = url;
                        mMediaPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
                        afd.Close();
                    }
                    else
                    {
                        string URL_REGEX          = "^((https?|ftp)://|(www|ftp)\\.)[a-z0-9-]+(\\.[a-z0-9-]+)+((:[0-9]+)|)+([/?].*)?$"; //should be ok
                        Java.Util.Regex.Pattern p = Java.Util.Regex.Pattern.Compile(URL_REGEX);
                        Matcher m = p.Matcher(url);                                                                                     //replace with string to compare
                        if (m.Find())
                        {
                            mMovieUrl = url;
                            mMediaPlayer.SetDataSource(mMovieUrl);
                        }
                    }
                    try
                    {
                        mMediaPlayer.SetOnPreparedListener(this);
                    } catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine(e.StackTrace);
                    }
                    mMediaPlayer.SetOnBufferingUpdateListener(this);
                    mMediaPlayer.SetOnCompletionListener(this);
                    mMediaPlayer.SetOnErrorListener(this);
                    mMediaPlayer.SetAudioStreamType(Stream.Music);
                    mMediaPlayer.SetSurface(new Surface(mSurfaceTexture));
                    mFullscreen = false;
                    mAutoStart  = autoStart;
                    mMediaPlayer.PrepareAsync();
                    mSeekPosition = seekPosition;
                }
                catch (IOException e)
                {
                    Log.Error("Pikkart AR Video", "Error while creating the MediaPlayer: " + e.ToString());
                    mMovieUrl   = "";
                    mVideoState = VIDEO_STATE.ERROR;
                    mMediaPlayerLock.Unlock();
                    mSurfaceTextureLock.Unlock();
                    return(false);
                }
            }
            else
            { //play full screen if requested or old android
                mPlayFullScreenIntent = new Intent(mParentActivity, typeof(FullscreenVideoPlayer));
                mPlayFullScreenIntent.SetAction(Android.Content.Intent.ActionView);
                mFullscreen   = true;
                mMovieUrl     = url;
                mSeekPosition = seekPosition;
                mVideoState   = VIDEO_STATE.READY;
            }

            mSurfaceTextureLock.Unlock();
            mMediaPlayerLock.Unlock();

            return(true);
        }
Пример #10
0
        protected override void OnCreate(Bundle bundle)
        {
            sInstance = this;

            base.OnCreate(bundle);

#if MONOGAME_3_4 || MONOGAME_3_5
            _game = new Game1();
            SetContentView((View)_game.Services.GetService(typeof(View)));
#else
            Game1.Activity = this;
            _game          = new Game1();
            SetContentView(_game.Window);
#endif

            _ouyaInputView = new TV.Ouya.Sdk.OuyaInputView(this);

            View content = FindViewById(Android.Resource.Id.Content);
            if (null != content)
            {
                content.KeepScreenOn = true;
            }
            _game.Run();

            Bundle developerInfo = new Bundle();

            developerInfo.PutString(OuyaFacade.OUYA_DEVELOPER_ID, DEVELOPER_ID);

            byte[] applicationKey = null;

            // load the application key from assets
            try {
                AssetManager        assetManager = ApplicationContext.Assets;
                AssetFileDescriptor afd          = assetManager.OpenFd(SIGNING_KEY);
                int size = 0;
                if (null != afd)
                {
                    size = (int)afd.Length;
                    afd.Close();
                    using (Stream inputStream = assetManager.Open(SIGNING_KEY, Access.Buffer))
                    {
                        applicationKey = new byte[size];
                        inputStream.Read(applicationKey, 0, size);
                        inputStream.Close();
                    }
                }
            } catch (Exception e) {
                Log.Error(TAG, string.Format("Failed to read application key exception={0}", e));
            }

            if (null != applicationKey)
            {
                Log.Debug(TAG, "Read signing key");
                developerInfo.PutByteArray(OuyaFacade.OUYA_DEVELOPER_PUBLIC_KEY, applicationKey);
            }
            else
            {
                Log.Error(TAG, "Failed to authorize with signing key");
                Finish();
                return;
            }

            //developerInfo.PutString(OuyaFacade.XIAOMI_APPLICATION_ID, "0000000000000");
            //developerInfo.PutString(OuyaFacade.XIAOMI_APPLICATION_KEY, "000000000000000000");

            //developerInfo.PutStringArray(OuyaFacade.OUYA_PRODUCT_ID_LIST, Game1.PURCHASABLES);

            _ouyaFacade = OuyaFacade.Instance;
            _ouyaFacade.Init(this, developerInfo);
        }