I am using silverlight 4 with smf and I want to create a play , pause toggle button. So if the media is playing and if the button is clicked it will pause the video. But if the media is paused, it will restart playing video.
How can I do that
private void btnPlay_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
I don't know anything about silverlight, but you could use a boolean.
private void btnPlay_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if(isPlaying)
{
music.pause();
isPlaying = FALSE;
return;
}
music.play();
isPlaying = TRUE;
return;
}
Related
I realize there are similar questions here and I have looked at them, I just can't apply the solutions to my own problem.
I am writing an MP3 player for a Pi using visual studio (C#) on a PC, then using Mono on the Pi to run it.
As Mono does not seem to have an MP3 library (happy to be corrected) my workaround is to use a gui interface to control a command line mp3 player (omxplayer).
I have a working test interface which i ssh across to the Pi from my PC to test each time.
The problem is when I hit Button1 to run it, it works fine in that the mp3 plays.
But as soon as I hit button 2 to send the app the "p" key which should pause the program, it just quit out. Ultimately I will have a bunch of buttons sending key presses to control it like next skip stop etc.
I have tested the functionality from a terminal and it works fine.
Any suggestions would be appreciated.
Here is my code.
"Oh", Please don't flame me for making procStartInfo and proc global, I was grasping at straws :)
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
ProcessStartInfo procStartInfo;
System.Diagnostics.Process proc;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Button Clicked";
procStartInfo = new ProcessStartInfo("omxplayer", "/home/pi/Music/Debug/Mutter.mp3 &");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
private void button3_Click(object sender, EventArgs e)
{
SendKeys.Send("{p}");
}
}
}
I want to play a sound while user save note in database here i'm using Media player to play sound and i have save the sounds file in raw folder.. Here is my Code
mediaPlayer = MediaPlayer.create(this,R.raw.save_effects);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.note_save:
mediaPlayer.start();
if (isEdit) {
updateNote(id,editText1.getText().toString(),editText2.getText().toString(),getDateTime(),Colors);
}else {
saveNote(editText1.getText().toString(),editText2.getText().toString(),getDateTime(),Colors);
break;
}
case R.id.color:
mediaPlayer.start();
openColorPicker();
break;
case android.R.id.home:
finish();
break;
}
return true;
}
Here i'm getting the problem while clicking on save button not playing the sound but selecting color it play sounds please some one help me out of this problem..
I've attached the KeyDown event to a ListView in my Win 10 UWP app. I want to make VirtualKey.Enter have a special effect, but the event is not firing for this particular key. Neither does it for Space, Arrow up or down. This I guess because the listview already has defined a special behaviour for those keys.
I'd like to override some of those keys though, or at least trigger additional actions. Even attaching events to those key with modifiers (e.g. Shift+ArrowDown) would not work because the events still are not firing.
I read that for WPF that there is a PreviewKeyDown-event which one can attach to. I can't find that event for UWP though. Are there any other options?
Stephanie's answer is a good one and it works in the general case. However, as Nilzor observed it will not work in the case of a ListView for the Enter key. For some reason the ListView handles the KeyDown event in case Enter is pressed.
A better way to handle key events when dealing with a ListView, as the question asks, is this.
private void ListView_Loaded(object sender, RoutedEventArgs e)
{
(sender as ListView).AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(ListView_KeyDown), true);
}
private void ListView_KeyDown(object sender, KeyRoutedEventArgs args)
{
if (args.Key == Windows.System.VirtualKey.Enter)
{
}
}
Notice the last argument in the AddHandler function. This specifies whether we want to handle events already handled by a previous element in the visual tree.
Of course don't forget to unsubscribe from the event when appropriate
Here is one way to do it : subscribe to the global Window.Current.CoreWindow.KeyDown event.
Then save the focus state of your listview and react accordingly.
Here is the code :
public sealed partial class MainPage : Page
{
bool hasFocus = false;
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
if(hasFocus)
{
Debug.Write("Key down on list");
}
}
private void myList_GotFocus(object sender, RoutedEventArgs e)
{
hasFocus = true;
}
private void myList_LostFocus(object sender, RoutedEventArgs e)
{
hasFocus = false;
}
You will also need to subscribe to the focus events in xaml, for your ListView :
<ListView .... GotFocus="myList_GotFocus" LostFocus="myList_LostFocus"/>
Corcus's solution doesn't work for me. What is working is handling PreviewKeyDown directly from XAML. Works well for SPACE or ENTER key:
XAML:
<ListView PreviewKeyDown="BookmarksListView_PreviewKeyDown">
Code behind:
private void BookmarksListView_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
// DO YOUR STUFF...
e.Handled = true;
}
}
You can use AddHandler method.
private void KeyEnterEventHandler(object sender, KeyRoutedEventArgs e)
{
if (e.OriginalKey == Windows.System.VirtualKey.Enter)
{
PlayFromListView();
}
}
private void LoadListView()
{
foreach (var music in playListStorageFile.PlayList)
{
ListViewItem item = new ListViewItem();
item.AddHandler(FrameworkElement.KeyDownEvent, new KeyEventHandler(KeyEnterEventHandler), true);
TextBlock mytext = new TextBlock();
mytext.Text = music.Nro.ToString() + " - " + music.Name;
mytext.Tag = music.Nro;
item.Content = mytext;
lvMusics.Items.Add(item);
}
}
https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.addhandler?view=winrt-18362
I have a seamle code, that include mediaPlayer of JavaFX!
I set mediaPlayer.setCycleCounts (MediaPlayer.INDEFINITE) and song repeat always!
But between repeat cycles I have some little stop, in 0.5 sec! How to do this cycle without stops?
private File mp3File;
private Media media;
private MediaPlayer mediaPlayer;
private MediaView mediaView;
public SoundEngine(String mediaFile) {
mp3File = new File(mediaFile);
try {
media = new Media(mp3File.toURI().toURL().toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
mediaPlayer = new MediaPlayer(media);
mediaView = new MediaView(mediaPlayer);
mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
}
i think your sound file has silent part at the end or begining.
You can use mediaPlayer.setStartTime(Duration.millies(500)) to skip the silent part if it is at the begining of the file.
I just did a dictionary application in blackberry along with a speech to text conversion support .Everything is working fine. Now i wanted to disable the sound when the user needs So how can i do it programmatically .Please help me
Try this
use the flag value as reference
if flag value is true then user click on item then it will play the sound
else sound wont play and display one dialog that Do you want enable sound with two options yes or no
if user click on yes then make flag value as true and item.setText("Voice Disable"); otherwise no action means no changes in flag
in your list item click listener write condition as following
if(flag==true)
{
write your logic to play
}
sample code is
public class app extends UiApplication{
public static void main(String[] args) {
new app().enterEventDispatcher();
}
public app() {
pushScreen(new SampleScreen());
}
}
class SampleScreen extends MainScreen
{
static boolean flag=true;
MenuItem item=null;
public SampleScreen() {
// use the flag value as reference
// if flag value is true then user click on item then it will play the sound
// else sound wont play and display one dialog that Do you want enable sound with two options yes or no
// if user click on yes then make flag value as true and item.setText("Voice Disable"); otherwise no action means no changes in flag
// in your list item click listner write condition as following
// if(flag==true)
// {
// write your logic to play
// }
// you already implement
item=new MenuItem("Voice Disable",0,100) {
public void run() {
if(flag)
{
flag=false;
item.setText("Voice Enable");
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Voice Disable succesfully");
}
});
}else{
flag=true;
item.setText("Voice Disable");
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Voice Enable succesfully");
}
});
}
}
};
addMenuItem(item);
}
}