Windows Phone 8 WPF MediaElement not playing video - c#

I have created a MediaElement in my Windows Phone 8.1 app, and I am trying to play an mp4 video. When I press the button to play the video, it shows the video's first frame (splash screen), but it never goes beyond that, and it looks like a still picture. What could I be doing wrong? I don't get any error from my MediaFailed method, either.
private void openButton_Click(object sender, RoutedEventArgs e)
{
shakeImage.Visibility = Visibility.Collapsed;
timer.Stop();
timerReset.Stop();
rotateImage.Stop();
mediaElement.Stop();
Uri explosion = new Uri(BaseUri, "Explode.mp4");
mediaElement.Source = explosion;
mediaElement.Play();
mediaElement.MediaFailed += mediaElement_MediaFailed;
}
void mediaElement_MediaFailed)object sender, ExceptionRoutedEventArgs e)
{
throw new FileNotFoundException();
}

if you are playing audio file from your phone you should change "UriKind " to "Relative" like this
Uri explosion = new Uri( "Explode.mp4",UriKind.RelativeOrAbsolute);
// or you could use this way
Stream stream = isoStore1.OpenFile("Explode.mp4", System.IO.FileMode.Open, System.IO.FileAccess.Read );
this.mediaElement.Stop();
this.mediaElement.SetSource(stream);
mediaElement.Play();
stream.Close();

Turns out it was due to Windows Phone 8 being picky about file formats and not throwing errors even though they were incorrect. I converted it to a certain wmv type and it seems to work now.

Related

How to play the song playing online using c# for Windows phone application?

I'm having the streaming URL(http://stereo.wavestreamer.com:8894). How to play the song in the MediaElement which is playing online in this URL and how to get the artist name?
I tried with the below code on button_click. Its not working.
private void PlayBtn_Click(object sender, RoutedEventArgs e)
{
Player.Source = new Uri("http://stereo.wavestreamer.com:8894", UriKind.RelativeOrAbsolute);
Player.Volume = 1.0;
if (Player.CurrentState != MediaElementState.Playing)
{
Player.Play();
}
}
You can find an example about how to stream music from your app on this link.
You need VS 2012 and the Windows Phone 8 SDK to open it.
You should be able to do it like this:
//You have to out your source link here to let the app work , enjoy :)
Player.Source = new Uri("Your Link goes here", UriKind.RelativeOrAbsolute);
if (Player.CurrentState != MediaElementState.Playing)
{
Player.Play();
}

How to implement Media player Listener In C# Xamarin Android?

I am developing one of my android app in xamarin platform in which i want to play audio using url and now i am able to play that file using url. So now i want perform following tasks where got stuck.
1) play file with seek bar according to file buffer size .
2) Unable to implement media player listener like OnComplitionListener specially in Xamarin.
Here i want share code that only playing audio without seekbar and media player listeners.
public void OnlinePlayer(String filePath)
{
try {
player.Reset ();
player.SetDataSource(filePath);
player.SetAudioStreamType(Stream.Music);
// player.SetOnCompletionListener(this);
player.Prepare();
player.Start();
player.PrepareAsync();
player.Completion += delegate {
};
} catch (Exception e) {
Console.WriteLine("OnlinePlayer()", "Exception in streaming mediaplayer e = " , e.Message.ToString());
}
}
In my case, I resolve the 2nd part of your question in this way for the listener:
player.Completion += OnPlayer_Completion;
then
private void OnPlayer_Completion(object sender, EventArgs e)
{
...your code...
}

How can I play a sound with defined start- and end-time in .Net

In my C# project I want to play a sound with a specified start- and end time.
I used the System.Media.SoundPlayer with the .Play() function. But with this function I can only play the whole sound file or can abort it after I have counted the runtime.
In fact I want to say, that the given sound file should start for example at time 1m:25s:30ms and should end at time 1m:50s:00ms or after a duration of 10s or so.
Does anybody know a simple solution for his problem?
Thanks 4 help.
Not really an answer, but this question got me wondering if you can do something like this:
long startPositionInBytes = 512;
long endPositionInBytes = 2048;
using ( var audioStream = File.OpenRead(#"audio.wav") )
{
audioStream.Position = startPositionInBytes;
using ( var player = new SoundPlayer(audioStream) )
{
player.Play();
do
{
Thread.Sleep(1);
} while ( audioStream.Position <= endPositionInBytes );
player.Stop();
}
}
In case you want to play a Background Media Player (for e.g., an audio file) which would start from a specific time and would work for a certain duration, then this code snippet could also be useful:
StorageFile mediaFile = await KnownFolders.VideosLibrary.GetFileAsync(fileName);//the path
BackgroundMediaPlayer.Current.SetFileSource(mediaFile);
BackgroundMediaPlayer.Current.Position = TimeSpan.FromMilliseconds(3000/*enter the start time here*/);
BackgroundMediaPlayer.Current.Play();
await Task.Delay(TimeSpan.FromMilliseconds(10000/*for how long you want to play*/));
BackgroundMediaPlayer.Shutdown();//stops the player
**But this would only work for Windows 8.1 and above.
For more information, click here...
In the End I use the NAudio library. That can handle this - not in a perfect way but ok.
see https://stackoverflow.com/a/13372540/2936206
I got it to work by using Windows Media Player in a C sharp program with a timer control and an open dialog.
I followed a sample that used buttons on the form and made the media player invisible.
After opening the file with an Open button that used the open dialog to open the mp3 file, on the Play button I put this code [the end effect is that it started the file in position 28.00 and ended it in position 32.50]:
private void button2_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
timer1.Start();
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = 28.00;
axWindowsMediaPlayer1.Ctlcontrols.play();
}
Then in the timer tick event:
private void timer1_Tick(object sender, EventArgs e)
{
if (axWindowsMediaPlayer1.Ctlcontrols.currentPosition >= 32.50)
{ axWindowsMediaPlayer1.Ctlcontrols.pause(); }
label1.Text = String.Format("{0:0.00}",
axWindowsMediaPlayer1.Ctlcontrols.currentPosition);
}

Navigation of a recorded file to media element or media player launcher

I've created an audio recorder for wp7. In that i actually used a listbox(named as filesListBox) to show the recorded audio files and if the user click on any file, then it simply plays the audio(Not by MediaPlayerLauncher). It worked perfectly.
private void filesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string filename = (string)e.AddedItems[0];
PlayFromIS(filename);
}
After this, i have used the MediaPlayerLauncher to play the recorded audio files from the listbox. It actually opens the file but while playing the audio, its tempo is extremely becomes low and the voice changes to something.
private void filesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
string filename = (string)e.AddedItems[0];
MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher();
mediaPlayerLauncher.Media = new Uri(filename, UriKind.Relative);
mediaPlayerLauncher.Controls = MediaPlaybackControls.All;
mediaPlayerLauncher.Location = MediaLocationType.Data;
mediaPlayerLauncher.Orientation = MediaPlayerOrientation.Landscape;
mediaPlayerLauncher.Show();
}
catch (IndexOutOfRangeException x)
{
}
}
Due to this i have then created a MediaElement.xaml page and added a MediaElement in it. But i dont know how to access the filename(from MainPage.xaml) in the MediaElement.xaml Page like the media player launcher does.
myMediaElement.Source = new Uri("??Don't know what to write here to access the filename??", UriKind.Relative);
I think MediaPlayerLauncher is much better than MediaElement but either one is accepted. Can anybody help me with this? Thanks in advance for your hard work!
Something like this should do the job, given that you can access the file from the Isolated Storage:
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = file.OpenFile("file/path/here", FileMode.Open, FileAccess.Read))
{
this.mediaElement.SetSource(stream);
}
}

Windows Phone - Access to camera requires the ISV Camera Capability

I am building a Windows Phone app which uses the camera. I have the code:
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true) ||
(PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) == true)) {
// Initialize the default camera.
_photoCamera = new Microsoft.Devices.PhotoCamera();
//Event is fired when the PhotoCamera object has been initialized
_photoCamera.Initialized += new EventHandler<Microsoft.Devices.CameraOperationCompletedEventArgs>(OnPhotoCameraInitialized);
//Set the VideoBrush source to the camera
viewfinderBrush.SetSource(_photoCamera);
}
else {
// The camera is not supported on the device.
this.Dispatcher.BeginInvoke(delegate() {
// Write message.
txtDebug.Text = "A Camera is not available on this device.";
});
}
}
private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e) {
int width = Convert.ToInt32(_photoCamera.PreviewResolution.Width);
int height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);
}
And I keep getting the exception:
"Access to camera requires the ISV Camera Capability."
I have a Lumia 900, and I know it runs camera APIs (the sample form MS works fine). But when I go to put this code in my app, I get this exception. Does anyone have any idea what might be going on? I am a good C#'er, but the Windows Phone is quite new to me.
Many thanks!
Brett
Solved it:
I had to add the line:
<Capability Name="ID_CAP_ISV_CAMERA"/>
to my WMAppManifent.xml file.

Categories