MediaElement Windows Phone 7 - windows-phone-7

I'm creating a little app to help me better understand how to play sounds on WP7 devices but I'm having a problem actually getting the sound to come out of the device.
I have the following code:
<MediaElement x:Name="note1" Source="test.mp3" AutoPlay="False" />
private void btn1_Click(object sender, RoutedEventArgs e)
{
note1.Source = new Uri("test.mp3", UriKind.Relative);
note1.Play();
}
Where test.mp3's Build Action is a Resource.
The thing I don't understand is when I add a breakpoint on the method btn1_Click and I stop at note1.Play() it actually plays test.mp3 but when debug without breakpoints and click on the button I hear nothing.
Is there a way to fix this issue?

Have you tried playing with test.mp3's Build Action set as content.
Also did you close zune software after it recognizes the phone and completes sync, and connect using wp7connect tool. for more info about wp7connect tool try here.
zune locks all media on wp7 device and you cant play any media, but the status of the media will be "ended".
try setting up media's following events MediaFailed MediaOpened,MediaEnded, DownloadProgressChanged, CurrentStateChanged and BufferingProgressChanged

Also, make sure you have add the capability ID_CAP_MEDIALIB to your manifest (WMAppManifest.xml), this seems to be required for MediaElement (otherwise you'll get AG_E_NETWORK_ERROR in your MediaFailed handler).

i dont recommend mediaElement for more than one audio item ..it has weird effects ...use something like:
Stream stream = TitleContainer.OpenStream(#"Audio/buzzer.wav");
SoundEffect effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
using the xna framework ....and make sure there WAV files.

Uri kind must be RelativeOrAbsolute.
private void btn1_Click(object sender, RoutedEventArgs e)
{
note1.Source = new Uri("test.mp3", UriKind.RelativeOrAbsolute);
note1.Play();
}

You need to make sure the MediaElement has been opened before you can call .Play() on it - you can do so by adding an event receiver to the MediaOpened event. It would also be good to call .Stop() any time prior to reassigning the Source property - take a look at this thread for more details.

This can't be solved without an Eventhandler. Do as mentioned below.
<MediaElement x:Name="note1" Source="test.mp3" AutoPlay="False" />
private void btn1_Click(object sender, RoutedEventArgs e)
{
note1.Source = new Uri("test.mp3", UriKind.Relative);
note1.MediaOpened += new RoutedEventHandler(note1_MediaOpened);
}
void note1_MediaOpened(object sender, RoutedEventArgs e)
{
note1.Play();
}
this is perfectly works. enjoy...

Related

Windows phone 8 Fast Resume when Tombstoned

http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj735579%28v=vs.105%29.aspx
According to the document when I use fast resume in windows phone 8 I can resume my App from main tile.
But when my app is tombstoned ,
for example, mainPage can navigate to ->pageA can naviget to->PageB, I deactived the app from PageA, then the
app is tombstoned, when I click the Tile that navigate to PageB, it's strange that the app back to Page A .
How to fix this problem?
It sounds like you are not saving the application state before it is tombstoned. There are 4 events that are fired for preserving application state:
These are related to completely closing and reopening the application (eg: Phone restart)
Application_Launching
Application_Closing
These are related to tombstoning (task switching)
Application_Activated
Application_Deactivated
It sounds like what you need is the second one relating to activating / deactivating. These methods are placed in the Applications *.cs file and allow you to preserve and reinstate the ViewModel when tombstoning.
This is an example:
private readonly string ModelKey = "Key";
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
PhoneApplicationService.Current.State[ModelKey] = ViewModel;
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
if (PhoneApplicationService.Current.State.ContainsKey(ModelKey))
{
ViewModel = PhoneApplicationService.Current.State[ModelKey] as FeedViewModel;
RootFrame.DataContext = ViewModel;
}
}

Get response from web browser for windows phone 'mango'

I m my application I want to use Webbrowser which will be using Wifi,after the Webbrower opens the the link (task.URL) ,I want to check what is the response like whether the linked got opened or it failed.How do I do that means get the response ?
WebBrowserTask task = new WebBrowserTask();
task.URL = "https://www.goggle.com/";
task.Show();
kindly help
Thanks.
Usually, to catch this use NavigationFailed event as follow:
void WebPage_Loaded(object sender, RoutedEventArgs e)
{
this.webHome.Navigate(new Uri(www,UriKind.Absolute));
this.webHome.NavigationFailed += webHome_NavigationFailed;
}
void webHome_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e)
{
this.webHome.NavigateToString("No web page available.");
}

Windows phone 7 - unable to play audio under lock screen

I am developing an app for windows phone 7. There is a media element which plays video from a url. When i lock the phone, the audio and video stops playing. I have tried disabling ApplicationIdleDetetction and i have handled Rootframe Obscured and Unobscured. I just couldn't figure out how to continue playing the audio when the phone is locked.
Any help on this is greatly appreciated !!
thanks
graham
Use the AudioPlayerAgent to keep the music playing even when the phone gets locked!
Check the "Background Audio Player Sample" on the Windows Phone Code Samples.
Video will automatically stop playing when the screen is locked - that is a built-in system feature. Think of it as a fail-safe for applications that will drain the device battery by playing video in the background, which is an unnecessary task anyway - who watches the content? ApplicationIdleDetection won't help with this task at all.
If you have a separate audio stream, you could use AudioPlayerAgent, that can be used to play both local and remote audio streams.
Read this:
Background Audio Overview for Windows Phone
How to: Play Background Audio for Windows Phone
Streaming Audio in Windows Phone
You can do this with a dispatcher timer. Here is an example of how I do it in my app Searchler (This feature not yet in marketplace, update coming very soon!) using the MMP Player Framework available # http://smf.codeplex.com/
namespace Searchler.Views
{
public partial class PlayerView : PhoneApplicationPage
{
bool appUnderLock = false;
DispatcherTimer dispatcherTimer = new DispatcherTimer();
}
public PlayerView()
{
InitializeComponent();
//Hack to enable play under lock screen
UIThread.Invoke(() => VideoPlayer.PlayStateChanged += VideoPlayer_PlayStateChanged);
UIThread.Invoke(() => (Application.Current as App).RootFrame.Obscured += RootFrame_Obscured);
UIThread.Invoke(() => (Application.Current as App).RootFrame.Unobscured += RootFrame_Unobscured);
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 3);
}
void dispatcherTimer_Tick(object sender, EventArgs e)
{
if( VideoPlayer.PlaybackPosition == VideoPlayer.EndPosition)
((PlayerViewModel)DataContext).Next(); //Custom GetNext Video Method
}
void RootFrame_Unobscured(object sender, EventArgs e)
{
dispatcherTimer.Stop();
appUnderLock = false;
}
void RootFrame_Obscured(object sender, ObscuredEventArgs e)
{
dispatcherTimer.Start();
appUnderLock = true;
}
}

Add contact in WP7 without using show() method

I wish to add new contacts in WP7. I have used the following
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
SavePhoneNumberTask savePhoneNumber = new SavePhoneNumberTask();
code.savePhoneNumber.Completed += new EventHandler<TaskEventArgs>(savePhoneNumber_Completed);
savePhoneNumber.PhoneNumber = "1987654320";
savePhoneNumber.Show();
}
This shows add more details of Contact page, if we press save then only it is saved.
But i want to save the contacts without calling Show() method.
Please anyone help me.....
You can't and you shouldn't be able to.
Imagine that we could add contacts silently. Apps would start spamming my contacts.
This is the way it works.

Windows Phone 7 with Silverlight - MediaElement doesn't play with this code

What am I doing wrong please?
it doesn't show any error, and doesn't play.
MediaElement song = new MediaElement();
song.Source = new Uri(#"\WP7_aaa\WP7_aaa\GameSounds\MenuScreen.mp3", UriKind.Relative);
LayoutRoot.Children.Add(song);
song.AutoPlay = false;
song.Play();
In your project, for the MP3 file, have you -
set the Build Action property to Content?
set the Copy To Output Directory to Copy Always?
In case you haven't done the above in the project, try them out.
HTH, indyfromoz
You need to wait for the song to be loaded before you can call the Play method on it.
What you want is:
MediaElement song = new MediaElement();
song.Source = new Uri("Audio/background.mp3", UriKind.Relative);
song.MediaOpened += MediaElement_MediaOpened;
And then in the event handler:
private void MediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
(sender as MediaElement).Play();
}
See this thread for more details. (edit: no idea where to find this thread now that they split it into WP and Xbox Forums...)
You must specify kind of Uri as RelativeOrAbsolute.
MediaElement song = new MediaElement();
song.Source = new Uri(#"\WP7_aaa\WP7_aaa\GameSounds\MenuScreen.mp3", UriKind.RelativeOrAbsolute);
LayoutRoot.Children.Add(song);
song.AutoPlay = false;
song.Play();

Resources