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); } }
/// <summary> /// Starts the player asynchronous from assets folder. /// </summary> /// <param name="fp">The fp.</param> /// <returns>Task.</returns> /// <exception cref="FileNotFoundException">Make sure you set your file in the Assets folder</exception> private async Task StartPlayerAsyncFromAssetsFolder(AssetFileDescriptor fp) { try { if (_player == null) { _player = new MediaPlayer(); } else { _player.Reset(); } if (fp == null) { throw new FileNotFoundException("Make sure you set your file in the Assets folder"); } await _player.SetDataSourceAsync(fp.FileDescriptor); _player.Prepared += (s, e) => { _player.SetVolume(0, 0); _isPlayerPrepared = true; }; _player.Prepare(); } catch (Exception ex) { Console.Out.WriteLine(ex.StackTrace); } }
public void SetupAudioFile(string filename) { AssetFileDescriptor fd = Android.App.Application.Context.Assets.OpenFd(filename); Player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length); Player.Prepare(); }
/// <summary> /// Starts the player asynchronous from assets folder. /// </summary> /// <param name="fp">The fp.</param> /// <returns>Task.</returns> /// <exception cref="FileNotFoundException">Make sure you set your file in the Assets folder</exception> private async Task StartPlayerAsyncFromAssetsFolder(AssetFileDescriptor fp) { try { if (_player == null) { _player = new MediaPlayer(); } else { _player.Reset(); } if (fp == null) { throw new FileNotFoundException("Make sure you set your file in the Assets folder"); } await _player.SetDataSourceAsync(fp.FileDescriptor); _player.Prepared += (s, e) => { _player.SetVolume(0, 0); _isPlayerPrepared = true; }; _player.Prepare(); } catch (Exception ex) { Console.Out.WriteLine(ex.StackTrace); } }
public void StartPlayer(AssetFileDescriptor filePath) { if (player == null) { player = new MediaPlayer(); player.Reset(); /* player.SetDataSource(filePath.FileDescriptor, filePath.StartOffset, filePath.Length); player.Prepare(); player.Start(); * */ } if(player != null) { //player.Reset(); player.SetDataSource(filePath.FileDescriptor, filePath.StartOffset, filePath.Length); player.Prepare(); if (AudioPosition > 0) { player.SeekTo(AudioPosition); } player.Start(); } }
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); }
void CreateNotification(string title, string desc) { CrossVibrate.Current.Vibration(); AssetFileDescriptor asset = Application.Context.Assets.OpenFd("test.mp3"); PlayFd(asset.FileDescriptor, asset.StartOffset, asset.Length); var uiIntent = new Intent(this, typeof(MainActivity)); const int pendingIntentId = 0; PendingIntent pendingIntent = PendingIntent.GetActivity(this, pendingIntentId, uiIntent, PendingIntentFlags.OneShot); Notification.Builder builder = new Notification.Builder(this).SetContentIntent(pendingIntent).SetContentTitle(title).SetContentText(desc).SetSmallIcon(MVVM.Droid.Resource.Drawable.satX); Notification notification1 = builder.Build(); var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager; const int notificationId = 0; notificationManager.Notify(notificationId, notification1); //var notification = new Notification(Android.Resource.Drawable.ButtonMinus, title) //{ // Flags = NotificationFlags.AutoCancel //}; //notification.SetLatestEventInfo(this, title, desc, PendingIntent.GetActivity(this, 0, uiIntent, 0)); //notificationManager.Notify(1, notification); //DialogNotify(title, desc); }
/** * \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(); }
}// end of updateBreakTimePeriods /// <summary> /// Plays audio files thar are located in assets folder /// </summary> /// <param name="file">the filename e.g "name.mp3"</param> private void playAudioFile(string file) { AssetFileDescriptor descriptor = Assets.OpenFd(file); mediaPlayer.SetDataSource(descriptor.FileDescriptor); mediaPlayer.Prepare(); mediaPlayer.Start(); }
public Sound(string filename, float volume, bool looping) { using (AssetFileDescriptor fd = Game.Activity.Assets.OpenFd(filename)) _soundId = s_soundPool.Load(fd.FileDescriptor, fd.StartOffset, fd.Length, 1); this.Looping = looping; this.Volume = volume; }
///<Summary> /// Load wav or mp3 audio file from the Android Resources folder ///</Summary> public bool Load(string fileName) { AssetFileDescriptor afd = Android.App.Application.Context.Assets.OpenFd(fileName); soundIds.Add(pool.Load(afd, 1)); return(true); }
public void Play(int num) { AssetFileDescriptor afd = Application.Context.Assets.OpenFd($"Assets/{LanguageHelper.GetTag()}/Audio/__{num}.mp3"); player = new MediaPlayer(); player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length); player.Prepare(); player.Start(); }
/// <summary> /// Creates music from given file. /// </summary> /// <returns>The music.</returns> /// <param name="file">File.</param> public IMusic CreateMusic(string file) { try { AssetFileDescriptor afd = assets.OpenFd(file); return(new AndroidMusic(afd)); } catch { throw new Exception("Couldn't load music '" + file + "'"); } }
private async void Play() { if (string.IsNullOrEmpty(MediaUrl) || _mediaPlayer == null) { return; } try { // Video Support if (MediaUrl.EndsWith(".mp4", StringComparison.CurrentCultureIgnoreCase) || MediaUrl.EndsWith(".f4v", StringComparison.CurrentCultureIgnoreCase) || MediaUrl.EndsWith(".m4v", StringComparison.CurrentCultureIgnoreCase)) { _imageView.Visibility = ViewStates.Invisible; if (_popper.IsDebugMode) { AssetFileDescriptor afd = Assets.OpenFd(MediaUrl); await _mediaPlayer.SetDataSourceAsync(afd); } else { await _mediaPlayer.SetDataSourceAsync(MediaUrl); } _mediaPlayer.Looping = true; _mediaPlayer.Prepare(); } // PNG Support else if (MediaUrl.EndsWith(".png", StringComparison.CurrentCultureIgnoreCase)) { _textureView.Visibility = ViewStates.Invisible; _mediaPlayer.Release(); _mediaPlayer.Dispose(); _mediaPlayer = null; if (_popper.IsDebugMode) { System.IO.Stream ims = Assets.Open(MediaUrl); Drawable d = Drawable.CreateFromStream(ims, null); _imageView.SetImageDrawable(d); } else { _imageView.SetImageURI(Android.Net.Uri.FromFile(new Java.IO.File(MediaUrl))); } _loadingSpinner.Visibility = ViewStates.Invisible; } } catch (Exception ex) { Logger.Error($"Error trying to start media player - {ex.Message}", ex); } }
///<Summary> /// Load wav or mp3 audio file from the iOS Resources folder ///</Summary> public bool Load(string fileName) { player.Reset(); AssetFileDescriptor afd = Android.App.Application.Context.Assets.OpenFd(fileName); player?.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length); return(PreparePlayer()); }
/* * Memory-map the model file in Assets. */ public static MappedByteBuffer LoadModelFile(Context context, string modelFile) { AssetFileDescriptor fileDescriptor = context.Assets.OpenFd(modelFile); FileInputStream inputStream = new FileInputStream(fileDescriptor.FileDescriptor); FileChannel fileChannel = inputStream.Channel; long startOffset = fileDescriptor.StartOffset; long declaredLength = fileDescriptor.DeclaredLength; return(fileChannel.Map(FileChannel.MapMode.ReadOnly, startOffset, declaredLength)); }
/// <summary> /// Creates sound-effects from specified file. /// </summary> /// <returns>The sound.</returns> /// <param name="file">File.</param> public ISound CreateSound(string file) { try { AssetFileDescriptor afd = assets.OpenFd(file); int soundId = soundPool.Load(afd, 0); return(new AndroidSound(soundPool, soundId)); } catch { throw new Exception("Couldn't load sound '" + file + "'"); } }
public void PlayAlertSound() { MainActivity mainActivity = Xamarin.Forms.Forms.Context as MainActivity; AssetFileDescriptor afd = mainActivity.Assets.OpenFd("Alert.mp3"); var player = new MediaPlayer(); player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length); player.Prepare(); player.Start(); }
private MappedByteBuffer getModel(AssetManager assets, string modelFilename) { AssetFileDescriptor fileDescriptor = assets.OpenFd(modelFilename); FileInputStream inputStream = new FileInputStream(fileDescriptor.FileDescriptor); FileChannel fileChannel = inputStream.Channel; long startOffset = fileDescriptor.StartOffset; long declaredLength = fileDescriptor.DeclaredLength; return(fileChannel.Map(FileChannel.MapMode.ReadOnly, startOffset, declaredLength)); }
public void playSoundPoolSound(string name) { for (int i = 0; i < soundPoolSoundNames.size(); i++) { if (soundPoolSoundNames.get(i).Equals(name)) { soundPool.play( (int)soundPoolSoundIds.get(i), 1.0f, 1.0f, 1, 0, 1); return; } } Log.d(TAG, "playSoundPoolSound: loading " + name); // check first if this is a raw resource int soundId = 0; if (name.IndexOf("res/raw/") == 0) { string resourceName = name.Substring(4); int id = getResources().getIdentifier(resourceName, "raw", getPackageName()); if (id == 0) { Log.e(TAG, "No resource named " + resourceName); } else { AssetFileDescriptor afd = getResources().openRawResourceFd(id); soundId = soundPool.load(afd, 1); } } else { try { AssetFileDescriptor afd = getAssets().openFd(name); soundId = soundPool.load(afd, 1); } catch { Log.e(TAG, "Couldn't open " + name); } } if (soundId == 0) { // Try to load the sound directly - works for absolute path - for wav files for sdcard for ex. soundId = soundPool.load(name, 1); } soundPoolSoundNames.add(name); soundPoolSoundIds.add(soundId); soundPool.play((int)soundPoolSoundIds.get(soundPoolSoundNames.size() - 1), 1.0f, 1.0f, 1, 0, 1); }
public AndroidMusic(AssetFileDescriptor assetFileDescriptor, string name) : base(name, name) { _playbackState = PlaybackState.Stopped; _mediaPlayer = new MediaPlayer(); _mediaPlayer.Completion += (sender, args) => _playbackState = PlaybackState.Stopped; _mediaPlayer.Reset(); _mediaPlayer.SetDataSource(assetFileDescriptor.FileDescriptor, assetFileDescriptor.StartOffset, assetFileDescriptor.Length); _mediaPlayer.SetAudioStreamType(Stream.Music); _mediaPlayer.Prepare(); }
public void SetupAudioFile(string filename, bool loop, double volume) { AssetFileDescriptor fd = Android.App.Application.Context.Assets.OpenFd(filename); Player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length); float all_v = (float)volume; Player.SetVolume(all_v, all_v); Player.Looping = loop; Player.Prepare(); }
public void PlayAudio(string path, bool isLooping) { StopAudio(); _assetFileDescriptor = Android.App.Application.Context.Assets.OpenFd("alert.mp3"); _mediaPlayer.SetDataSource(_assetFileDescriptor.FileDescriptor, _assetFileDescriptor.StartOffset, _assetFileDescriptor.Length); _mediaPlayer.Looping = isLooping; _mediaPlayer.Prepare(); _mediaPlayer.Start(); }
/// <summary> /// The open file. /// </summary> /// <param name="uri"> /// The uri. /// </param> /// <param name="mode"> /// The mode. /// </param> /// <returns> /// The Android.OS.ParcelFileDescriptor. /// </returns> public override ParcelFileDescriptor OpenFile(Uri uri, string mode) { this.InitIfNecessary(); AssetFileDescriptor af = this.OpenAssetFile(uri, mode); if (null != af) { return(af.ParcelFileDescriptor); } return(null); }
public IMusic createMusic(string filename) { try { AssetFileDescriptor assetDescriptor = assets.OpenFd(filename); return(new AndroidMusic(assetDescriptor)); } catch (IOException e) { throw new SystemException("Couldn't load music '" + filename + "'"); } }
/// <summary> Creates the AudioData </summary> /// <param name="filename"></param> private AudioData(String filename) { Filename = filename; #if __ANDROID__ _AssetFileDescriptor = GameActivity.Instance.Assets.OpenFd(filename); Descriptor = _AssetFileDescriptor.FileDescriptor; StartOffset = _AssetFileDescriptor.StartOffset; Length = _AssetFileDescriptor.Length; #elif __IOS__ Data = NSData.FromFile(filename); #endif }
public ISound createSound(string filename) { try { AssetFileDescriptor assetDescriptor = assets.OpenFd(filename); int soundId = soundPool.Load(assetDescriptor, 0); return(new AndroidSound(soundPool, soundId)); } catch (IOException e) { throw new SystemException("Couldn't load sound '" + filename + "'"); } }
public async Task OpenFile(string fileName) { try { _player = new MediaPlayer(); _fd = global::Android.App.Application.Context.Assets.OpenFd(fileName); _player.SetDataSource(_fd.FileDescriptor, _fd.StartOffset, _fd.Length); _player.Prepare(); } catch (Exception e) { throw; } }
private void LoadTensorflowModel() { var assets = Android.App.Application.Context.Assets; AssetFileDescriptor fileDescriptor = assets.OpenFd(MODEL_FILE); FileInputStream inputStream = new FileInputStream(fileDescriptor.FileDescriptor); FileChannel fileChannel = inputStream.Channel; long startOffset = fileDescriptor.StartOffset; long declaredLength = fileDescriptor.DeclaredLength; _model = fileChannel.Map(FileChannel.MapMode.ReadOnly, startOffset, declaredLength); Interpreter.Options options = new Interpreter.Options(); options.SetNumThreads(THREAD_NUM); _interpreter = new Interpreter(_model, options); }
public void Prepare(string assetName, HandlePrepare prepareHandler) { if (mMediaPlayer == null) { mMediaPlayer = new Android.Media.MediaPlayer(); } AssetFileDescriptor assetFd = Android.App.Application.Context.Assets.OpenFd(assetName); mMediaPlayer.Reset(); mMediaPlayer.SetDataSource(assetFd); mMediaPlayer.SetAudioStreamType(ExchangeAudioStreamType(mAudioStreamType)); mMediaPlayer.Looping = mLoop; mMediaPlayer.SetOnPreparedListener(new LocalPrepareListener(prepareHandler)); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static android.media.AudioTrack createProgressTone(android.content.Context context) throws java.io.IOException private static AudioTrack createProgressTone(Context context) { AssetFileDescriptor fd = context.Resources.openRawResourceFd(R.raw.progress_tone); int length = (int)fd.Length; AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, length, AudioTrack.MODE_STATIC); sbyte[] data = new sbyte[length]; readFileToBytes(fd, data); audioTrack.write(data, 0, data.Length); audioTrack.setLoopPoints(0, data.Length / 2, 30); return(audioTrack); }
public AndroidMusic(AssetFileDescriptor afd) { this.mp = new MediaPlayer(); try { mp.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length); mp.Prepare(); isPrepared = true; mp.SetOnCompletionListener(this); mp.SetOnSeekCompleteListener(this); mp.SetOnPreparedListener(this); mp.SetOnVideoSizeChangedListener(this); } catch { throw new ApplicationException("Couldn't load music"); } }
public AndroidMusic(AssetFileDescriptor afd) { this.mp = new MediaPlayer (); try { mp.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length); mp.Prepare(); isPrepared = true; mp.SetOnCompletionListener(this); mp.SetOnSeekCompleteListener(this); mp.SetOnPreparedListener(this); mp.SetOnVideoSizeChangedListener(this); } catch { throw new ApplicationException("Couldn't load music"); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static void readFileToBytes(android.content.res.AssetFileDescriptor fd, byte[] data) throws java.io.IOException private static void readFileToBytes(AssetFileDescriptor fd, sbyte[] data) { System.IO.FileStream inputStream = fd.createInputStream(); int bytesRead = 0; while (bytesRead < data.Length) { int res = inputStream.Read(data, bytesRead, (data.Length - bytesRead)); if (res == -1) { break; } bytesRead += res; } }
public AndroidMusic(AssetFileDescriptor assetDescriptor) { mediaPlayer = new MediaPlayer(); try { mediaPlayer.SetDataSource(assetDescriptor.FileDescriptor, assetDescriptor.StartOffset, assetDescriptor.Length); mediaPlayer.Prepare(); isPrepared = true; mediaPlayer.SetOnCompletionListener(this); mediaPlayer.SetOnSeekCompleteListener(this); mediaPlayer.SetOnPreparedListener(this); mediaPlayer.SetOnVideoSizeChangedListener(this); } catch (Exception e) { throw new SystemException("Couldn't load music"); } }
internal SoundEffect(AssetFileDescriptor afd) { soundId = pool.Load(afd, 1); }
public AndroidMusicTrack (string path) { this.Asset = Assets.ResolveFd (path); }