WP8 error media player can not start - c#

I'm trying to play a small video file in my windows phone application. Is pretty basic
void StartMediaPlayer()
{
MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher();
mediaPlayerLauncher.Media = new Uri("/Assets/video/video1.wmv", UriKind.Relative);
mediaPlayerLauncher.Location = MediaLocationType.Install;
mediaPlayerLauncher.Controls = MediaPlaybackControls.All;
mediaPlayerLauncher.Orientation = MediaPlayerOrientation.Landscape;
mediaPlayerLauncher.Show();
}
i call this void on image tap event, and this is what happend
the debugger show that the error is here: mediaPlayerLauncher.Show();

That error will be thrown the application can't find your file /Assets/video/video1.wmv in the install directory on the phone. Make sure that your video is in your project, and is set to build type "Content". You could open the XAP file to double-check it's in the correct relative location too (it's just a ZIP file archive renamed).

if some has the same problem, just a heads up the files should be in main directory
mediaPlayerLauncher.Media = new Uri("video1.wmv", UriKind.Relative);

Related

Playing WPF music as a resource [duplicate]

I am trying to play a sound file in my WPF application. Currently I have the following call:
private void PlaySound(string uriPath)
{
Uri uri = new Uri(#"pack://application:,,,/Media/movepoint.wav");
var player = new MediaPlayer();
player.Open(uri);
player.Play();
}
Now if I specify Media/movepoint.wav as build action Content and load it as a relative or absolute file path it works fine, so I suspect this has something to do with the Pack URI, but I cannot for the life of me figure out what.
The objective is to store the file as a resource so that its not available in the output directory. I can provide either the WAV copy or the MP3 copy.
I tried this with an image file, which works the same as a sound file as far as the uri is concerned because it's just another resource. I used the code below which essentially matches what you have.
new Uri(#"pack://application:,,,/Resources/logo.png")
Make sure that your 'Media' folder is not nested in any other folder. If it is, you need to include that folder as well.
Using .NET Framework 4.0, VS2012.
This link gives a pretty good description of the whole "pack" scheme of things.
EDIT
More research on this topic seems to indicate that what you want to do might not be possible with audio or video files. The excerpt below is taken from the remarks section of this MSDN page.
Although you can declare an instance of this class in Extensible
Application Markup Language (XAML), you cannot load and play its media
without using code. To play media in XAML only, use a MediaElement.
Also, if you declare an instance in XAML, the only practical use is to
fill property element syntax for the Player property.
When distributing media with your application, you cannot use a media
file as a project resource. In your project file, you must instead set
the media type to Content and set CopyToOutputDirectory to
PreserveNewest or Always.
MediaPlayer can be used in two different modes, depending on what is
driving the player: independent mode or clock mode. In independent
mode, the MediaPlayer is analogous to an image and the media opened
through the Open method drives playback. In Clock mode, the
MediaPlayer can be thought of as a target for an animation, and thus
it will have corresponding Timeline and Clock entries in the Timing
tree which controls playback. For more information on media modes, see
the Multimedia Overview.
MediaPlayer is different from a MediaElement in that it is not a
control that can be added directly to the user interface (UI) of an
application. To display media loaded using MediaPlayer, a VideoDrawing
or DrawingContext must be used.
The following seems to work in .NET Framework 4.5:
var sri = Application.GetResourceStream(new Uri("pack://application:,,,/MyAssemblyName;component/Resources/CameraShutter.wav"));
if ((sri != null))
{
using (s == sri.Stream)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(s);
player.Load();
player.Play();
}
}
CameraShutter.wav is embedded as Resource in my project (and resides inside Resources subfolder, as indicated in the pack URI).
You can also load a Stream into the SoundPlayer if the .wav file is an Embedded Resource. Note that in this example the resources are in a folder called Resources that is in the root of the project, that is why it is written {0}.Resources.{1}.
//the wav filename
string file = "emergency_alarm_002.wav";
//get the current assembly
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
//load the embedded resource as a stream
var stream = assembly.GetManifestResourceStream(string.Format("{0}.Resources.{1}", assembly.GetName().Name, file));
//load the stream into the player
var player = new System.Media.SoundPlayer(stream);
//play the sound
player.Play();
This also seems to work and is maybe simpler to remember than that long line of pack or assembly stuff..
I opened the Resources.resx and dragged a sound file in there called aa_pickup.wav Then set the properties of it to Copy to Output Dir = Copy Always.
System.IO.Stream str = Properties.Resources.aa_pickup;
System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
snd.Play();
Now.. if I could only work out how to change the volume.

Xamarin. Got an issue with loading mp3 to play via MediaPlayer: FileNotFoundException

I create an application for android via Monogame (C#). In order to reduce my .apk size I decied to don't use Content Pipeline for audio. I found that I can use raw mp3 with Song.FromUri() method. Using next code
Song mySong;
//...
mySong = Song.FromUri("track01", new System.Uri("audio.mp3", System.UriKind.Relative));
//...
MediaPlayer.Play(mySong);
I got and exception "Unhandled Exception: Java.IO.FileNotFoundException: track01" on
MediaPlayer.Play(mySong);
, but I have file "audio.mp3" in main folder.
Solution Screenshot
Changing property "Build Action" to "None"/"Content"/"AndroidAsset" and "Copy to output directory" to "Copy always" didn't bring anything new.
I searched the solution, but it looks like nobody has the same problem or they solve it easily. The only similar problem I meet here, but their solution didn't help.
Do you have any ideas that can help me? Thanks.
I'm not sure about Song.FromUri Method, but this is how I'm using sound files.
var player = new MediaPlayer();
var fd = global::Android.App.Application.Context.Assets.OpenFd("sounds/Wrong.mp3");
player.Prepared += (s, e) =>
{
player.Start();
};
player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
player.Prepare();
I have Kept my Wrong.mp3 in Assets/sounds Folder
Is it not a possibility to put the audio file into the resources?
If you open your project properties, there should be a resources tab. Within this tab, you should be able to add different values (strings, images, etc) including audio files. If you've added this here, it should be reachable through the resource manager.
Let me know if this works out!

Adding .wav file from resources (WPF C#)

I am trying to add a .wav/.mp3 file to a project. I've added some pictures and they are working well, but I can't add any music. When I type the Uri like new Uri("file.mp3") and I put the file in the project directory it works, but this code below isn't (but it reads pictures O.o).
Uri jezus_uri = new Uri(#"pack://application:,,,/Resources/jesusrender.png");
Uri devil_uri = new Uri(#"pack://application:,,,/Resources/devilrender.jpg");
BitmapImage jesus_obraz = new BitmapImage(jezus_uri);
BitmapImage devil_obraz = new BitmapImage(devil_uri);
Uri muzyka = new Uri(#"pack://application:,,,/Resources/plig.wav");
MediaPlayer punch = new MediaPlayer();
punch.Open(muzyka);
punch.Play();
Build action is set to Resource of course. There is no error. Music is just not playing.
You cannot play sounds from a resource in a Media Player. You can only play it from a file. You may want to go with the approach of having your sound file deployed in the project directory and playing the file from the Media Player.
Alternatively, you can use the SoundPlayer to play sounds from a resource file.
SoundPlayer sndPing = new SoundPlayer(SoundRes.GetType(), "Ping.wav");
sndPing.Play();
Ref: https://msdn.microsoft.com/en-us/library/3w5b27z4%28v=vs.80%29.aspx

Accessing files from the project folder of a WP app

I'm new to Windows Phone development and I'm trying to do a simple training app.
I want my app to load some audio files that I've put into a folder inside the project.
Here's a short snippet:
private void LoadSound(String SoundFilePath, out SoundEffect Sound)
{
// For error checking, assume we'll fail to load the file.
Sound = null;
try
{
// Holds informations about a file stream.
StreamResourceInfo SoundFileInfo = Application.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));
My folder structure is something like
- Project
- Kits
- TestKit_128
- Bass_126.wav
and calling
Button.setSound("Kits\\TestKit_128\\bass_126.wav");
throws a System.NullReferenceException because the path is not found when the URI is created. (At least I think so!)
What should I do?
Is there any way to load files from a folder in the project or to copy them into the IsolatedStorage when I run the app for the first time?
Thanks
EDIT:
I've just opened the XAP file with WinRar and there's no "Kits" folder so I guess that my problem is how to make it add the folder to the XAP file.
I think you have to add the folder and the file to your project (i assume you are using Visual Studio). Try rightclick on your solution and add an existing object (in this case your wav file).
Did you set the build action of the wav file to "Content"? Right click on the WAV file --> properties --> build action == content.
Try this:
Uri uri = new Uri("Kits/TestKit_128/bass_126.wav", UriKind.Relative);//Above the Kits folder only the project itself.
StreamResourceInfo sr = Application.GetResourceStream(uri);
try
{
using (Stream stream = sr.Stream)
{
//working here with your data image or wav or whatever you want from URI
}
}
catch (Exception)
{
}
This is for resource inside project and working good for me. Also prevoius note about "Content" is right, it wouldn't work without it. I think the problem is double-slashes in address.

How do I stream videos from web server in Silverlight using ExpressionMediaPlayer control?

I would like to stream videos that reside at the webserver from within a ExpressionMediaPlayer control. The following results in a network error. I believe that the problem is with my Uri. I have the videos inside the 'ClentBin' folder. Can anyone tell me how this is done?
private void videoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedVideo = (Video)videoList.SelectedItem;
PlaylistItem item = new PlaylistItem();
item.MediaSource = new Uri(#"/ClientBin/" + selectedVideo.FilePath, UriKind.RelativeOrAbsolute);
item.IsAdaptiveStreaming = false;
ep.Playlist.Items.Add(item);
}
Thanks!
There can be a number of factors that contribute to a network error in the Expression Media Player. Here are some basic checks...
1. Check the video file itself
Launch Windows Media Player, go to File > Open URL... and make sure you can play the video with the absolute URL, just to rule out any basic problems with the web server. (Note that this does not apply if you are working with Adaptive Streaming, which it doesn't appear you are.)
2. What does selectedVideo.FilePath contain?
Is this a simple file name (i.e. MyVideo.wmv) or is it a relative file path? Forward or backward slashes?
3. Try it with an absolute static URI
Just to rule out relative path issues with your app / web server / any virtual directory configuration, try:
item.MediaSource = new Uri(#"http://mysite.com/ClientBin/MyVideo.wmv", UriKind.Absolute);
4. Remove the leading slash from /ClientBin/
Try just new Uri(#"ClientBin/" + selectedVideo.FilePath, UriKind.Relative); and see if the relative path is then correct.

Categories

Resources