Video is not played in iOS project - xamarin

I have used Plugin.MediaManager in my Xamarin.Forms application. It works perfectly in Android project, but in iOS project it does not.
I have added
VideoViewRenderer.Init();
in AppDelegate, and this is the code in the view:
async void PlayStop_Clicked(object sender, System.EventArgs e)
{
if (this.BtnPlayStop.Text == "Start Video")
{
string video = Path.Combine(_videoPath, this.viewModel.Item.Video);
if (File.Exists(video))
{
await CrossMediaManager.Current.Play(video, MediaFileType.Video);
this.BtnPlayStop.Text = "Stop Video";
}
}
else
{
await CrossMediaManager.Current.Stop();
this.BtnPlayStop.Text = "Start Video";
}
}
Code enters the first if, since button changes its text to 'Stop Video' but no video appears. The video is a local mp4 file.
As I told, this works perfect in Android.
What's wrong?
Thanks
Jaime

I have replaced the method that plays or stops the video by this one:
async void PlayStop_Clicked(object sender, System.EventArgs e)
{
if (this.BtnPlayStop.Text == "Iniciar Video")
{
string video = Path.Combine(_videoPath, this.viewModel.Item.Video);
if (File.Exists(video))
{
Device.BeginInvokeOnMainThread(() =>
{
_ = CrossMediaManager.Current.Play("file://" + video, MediaFileType.Video);
});
this.BtnPlayStop.Text = "Detener Video";
}
}
else
{
await CrossMediaManager.Current.Stop();
this.BtnPlayStop.Text = "Iniciar Video";
}
}
The "file://" part is important when loading local media files.
With that method, it works in both Android and iOS.
Regards
Jaime
Regards
Jaime

Related

Xamarin Forms Media Plugin - Take photo return to screen pops to root

I'm experiencing the issue here on an iOS phone.
https://github.com/jamesmontemagno/MediaPlugin/issues/433?_pjax=%23js-repo-pjax-container
here's the code that navigates to the photo page. You can see all the different things I tried
private async void addPhotosClickedAsync(object sender, EventArgs args)
{
// await ((RootPage)Application.Current.MainPage).Detail.Navigation.PushAsync(new TakePhoto(_workOrderId));
// await ((RootPage)App.Current.MainPage).Detail.Navigation.PushModalAsync(new TakePhoto(_workOrderId));
//Device.BeginInvokeOnMainThread(async () =>
//{
// await Navigation.PushAsync(new TakePhoto(_workOrderId));
//});
await ((RootPage)App.Current.MainPage).Detail.Navigation.PushAsync(new TakePhoto(_workOrderId));
}
On the camera page, there is a camera button icon that invokes the media plugin to take a picture. When the picture UI returns, it sets an image source to the photo just taken. The UI returns for a moment, then all of a sudden the app pops back to root
private async Task OnCameraTapped(object sender, EventArgs args)
{
CameraHelper cameraHelper = new CameraHelper();
try
{
ImgBytes = await cameraHelper.TakePicture();
PhotoImage.Source = ImageSource.FromStream(() =>
{
return new MemoryStream(ImgBytes);
});
}
catch (Exception ex)
{
if (ex.Message == "No camera available")
{
await DisplayAlert("Error", "No camera available", "Ok");
}
else
{
await DisplayAlert("Error", "Unable to take picture.", "Ok");
}
}
}
Is there some sort of work around for this?

Windows UWP app mobile back button not working

I'm using this well documented solution to add a back button to our app. I'm setting things up like this when the App is initialized:
Windows.UI.Core.SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += CreateNewKeyView_BackRequested;
private void CreateNewKeyView_BackRequested(object sender, BackRequestedEventArgs e)
{
NavigationService.Instance.GoBack();
}
The back button is shown on the desktop app and works as expected, navigating our Frame back to previous pages.
However, on Windows Phone, the hardware button just exits the app. The various places that I found code like this all state that this should work for the mobile hardware button, but it simply isn't working for us.
You should set e.Handled = true in your CreateNewKeyView_BackRequested method.
Don't know how you code for your NavigationService, I just tested the following code, it works by my side:
private void CreateNewKeyView_BackRequested(object sender, BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame != null)
{
if (rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
}
}
Or, for a phone, we use also special API for Hardware Buttons.
You can judge if the current using a phone Api is or not in the OnLaunched method:
if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
{
Windows.Phone.UI.Input.HardwareButtons.BackPressed += OnBackPressed;
}
then complete the OnBackPressed method:
public void OnBackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame != null)
{
if (rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
}
}
To do this, you need at first add the Windows Mobile Extensions for the UWP references in your project.
Here is
private void CreateNewKeyView_BackRequested(object sender, BackRequestedEventArgs e) //event handle nya untuk backbutton
{
var frame = ((Frame)Window.Current.Content);
if (frame.CanGoBack)
{
frame.GoBack();
e.Handled = true;
}
}

How to play MediaElement in background.?

I am developing an application to play online radio via streaming. I have used MediaElement. But the problem is the player doesn't play in background. I mean as soon as I click on "start" or "back" button on the phone, the streaming as well as the audio stops. I have not tested it on any device, so please inform me if it does happen in simulator but not device. Here is my code..
private void Play()
{
if (mediaElement == null || mediaElement.CurrentState != MediaElementState.Playing)
{
if (SystemTray.ProgressIndicator == null)
SystemTray.ProgressIndicator = new ProgressIndicator();
SystemTray.ProgressIndicator.IsIndeterminate = true;
SystemTray.ProgressIndicator.IsVisible = true;
SystemTray.ProgressIndicator.Text = "Connecting to *********...";
mediaStream = new ********.RadioStream(uri);
mediaStream.StreamSetupComplete += (o, e) =>
{
Dispatcher.BeginInvoke(() =>
{
if (mediaElement != null)
{
LayoutRoot.Children.Remove(mediaElement);
}
mediaElement = new MediaElement();
mediaElement.Volume = 1.0;
LayoutRoot.Children.Add(mediaElement);
mediaElement.SetSource(mediaStream);
SystemTray.ProgressIndicator.IsVisible = false;
});
};
}
}
I want to know the steps to enable this to play in background. Atleast when the user presses "start" button, the audio streaming should not stop.
Also one more problem I have is I have added an ApplicationBarMenu in which I have an "Exit" button. As soon as the user clicks this button the streaming should stop and application should close itself. I am unable to close the application programmatically. Code is give below..
void exit_Click(object sender, EventArgs e)
{
if (playing)
{
MessageBoxResult Choice;
Choice = MessageBox.Show("Media is currently playing, do you want to stop it?", "Stop Player", MessageBoxButton.OKCancel);
if (Choice == MessageBoxResult.OK)
{
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(#"Images/play.png", UriKind.Relative));
play.Background = brush;
Stop();
playing = false;
try
{
// if (NavigationService.CanGoBack)
// {
// while (NavigationService.RemoveBackEntry() != null)
// {
// NavigationService.RemoveBackEntry();
// }
// }
}
catch
{
}
}
else
{
}
}
}
Please help me with the proper code. Even if there is any other way to stream media in background other than MediaElement, please suggest that too..
Hoping a reply soon. Thanks to all in advance.
You must use BackgroundAudioPlayer for this.
You should take a look at Microsoft.Phone.BackgroundAudio Namespace too.

WP7 background music play

In my WP7 app, I want 2 music files to run in background.I am using MediaElement to do this. I am facing two issues.
how to play in background?
How to loop background music?
This is how I do it. Add the following:
<MediaElement x:Name="meSong" />
This is added in the constructor:
meSong.MediaEnded += new RoutedEventHandler(meSong_MediaEnded);
This is how I loop the song once it has ended:
private void meSong_MediaEnded(object sender, RoutedEventArgs e)
{
meSong.Position = TimeSpan.Zero;
meSong.Play();
}
This is how I set my song:
private void SetSong(string selectedSong)
{
if (ViewModel.IsMusicOn)
{
try
{
meSong.Stop();
meSong.Source = new Uri(string.Format("Media/Sounds/{0}.wav", selectedSong), UriKind.Relative);
meSong.Position = new TimeSpan(0);
meSong.Volume = 0.5;
}
catch (Exception)
{
// nothing for now
}
}
}
And this is how you obviously start your music:
meSong.Play();

Need help using the CameraCaptureTask

i am trying to create a simple demo application that does the following: i have a button at MainPage.xaml (with Name="btnCamera") and an image control (with Name="photo") and when i press the button i want to start the camera task, capture a photo and display it on the image control. The problem is that my code works on the emulator but not on a real device. The device i have is updated to the latest update(7740). Do you have an explanation for that or any change to my code to make it work? That is my code:
public partial class MainPage : PhoneApplicationPage
{
CameraCaptureTask _cameraCapture;
public MainPage()
{
InitializeComponent()
_cameraCapture = new CameraCaptureTask();
_cameraCapture.Completed += new EventHandler(_cameraCapture_Completed);
}
private void btnCamera_Click(object sender, RoutedEventArgs e)
{
try
{
_cameraCapture.Show();
}
catch (Exception)
{
MessageBox.Show("Error occured");
}
}
void _cameraCapture_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage image = new BitmapImage();
image.SetSource(e.ChosenPhoto);
photo.Source = image;
}
}
}
You need to make sure Zune is not running. The code looks fine and should work if you unplug the phone from the PC. If you want to debug whilst plugged into the PC, use WPConnect instead of Zune.

Resources