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;
}
}
Related
I am not getting all devices when I am trying to scan Bluetooth devices with my application. It does not show android and windows device list. I have attached screenshots to understand my problem.
Here is my code.
_centralManager = new CBCentralManager(DispatchQueue.CurrentQueue);
_centralManager.DiscoveredPeripheral += _centralManager_DiscoveredPeripheral;
_centralManager.UpdatedState += (object sender, EventArgs e) =>
{
var manager = sender as CBCentralManager;
if (manager.State == CBCentralManagerState.PoweredOn)
_centralManager.ScanForPeripherals(new CBUUID[0]);
};
Scan event:
public void _centralManager_DiscoveredPeripheral(object sender, CBDiscoveredPeripheralEventArgs e)
{
var device = e.Peripheral;
var rssi = e.RSSI;
var ads = e.AdvertisementData;
}
Note: In my app, I was show device which name is not equal to null or blank.
I had implemented GATTA server on an Android device and it worked.
First, run the app on an Android device, start the server and done.
https://github.com/androidthings/sample-bluetooth-le-gattserver/blob/master/java/app/src/main/java/com/example/androidthings/gattserver/GattServerActivity.java
In my case, I wanted to do this for android things. So I implemented the code in android things.
I have some video file embedded with the project, that to be opened in the native video player using Xamarin forms.
Note: Having in-app video player is limited here.
Any idea for this?
Sorry for late, you could refer to FormsNativeVideoPlayer.
When you load your video file from Raw folder, you just need to modify the VideoPlayer_CustomRenderer code like this :
protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged (e);
...
string uriPath = "android.resource://" + Forms.Context.PackageName + "/" + Resource.Raw.audio;
var uri = Android.Net.Uri.Parse((uriPath));
//Set the videoView with our uri, this could also be a local video on device
videoView.SetVideoURI (uri);
...
}
I have Windows 10 Pro, build 10586.494 on HP ZBook G2. When I go to Settings -> Devices -> Bluetooth and switch on Bluetooth, I can see nearby BLE devices (they are custom made BLE devices manufactured by my company).
I want to interact with my BLE devices in Universal Windows application (in Visual Studio 2015). I use this code (it is just a snippet of code)
BluetoothLEAdvertisementWatcher watcher;
//....
watcher = new BluetoothLEAdvertisementWatcher { ScanningMode = BluetoothLEScanningMode.Active };
watcher.Received += WatcherOnReceived;
watcher.Start();
//....
private void WatcherOnReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs btAdv)
{
}
When I run this application, WatcherOnReceived is never executed (although watcher.Start was executed). Why and how to fix it?
If found a better workaround for this. (I'm using Win 10 1803.)
First, to recap the problem since the original question is not entirely clear about it:
BluetoothLEAdvertisementWatcher works if BLE device is advertising before watcher.Start()
BluetoothLEAdvertisementWatcher does not work if BLE device starts advertising after watcher.Start()
If BluetoothLEAdvertisementWatcher is not working after watcher.Start(), it will begin working when Windows 10 Bluetooth Settings is opened or closed (i.e. the opening or closing of the settings window triggers BluetoothLEAdvertisementWatcher to start working)
So, the workaround I came up with is to keep starting and stopping a second BluetoothLEAdvertisementWatcher in the background to create the same sort of trigger that the Windows Bluetooth Settings window provides.
For example, using a WinForms timer:
...
var timer = new Timer { Interval = 1000 };
timer.Tick += Timer_Tick;
timer.Start();
...
private void Timer_Tick(object sender, EventArgs e)
{
var watcher = new BluetoothLEAdvertisementWatcher();
watcher.Received += (s, a) => { };
watcher.Start();
Task.Delay(500).ContinueWith(_ => watcher.Stop());
}
I have to keep Bluetooth switched on in Settings -> Devices -> Bluetooth in order to receive advertisements in my app.
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...
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();