WP7 how to get current song - c#

I have a track which is playing on my phone and then I pause it using the controls that pop down after pressing the volume keys.
In my application, how can I resume that song and find out what the song is?
The following does not work.
MediaPlayer.Queue.ActiveSong // This reports no songs at all. Only works if I give it a song to play
MediaHistory.Instance.NowPlaying.Title // MediaHistory.Instance = null
Also just trying this does nothing.
MediaPlayer.Resume
Edit: If it makes any difference, I am debugging a Windows Phone 7 app on and Windows Phone 8 device.
If I give a song to MediaPlayer to play then all the above works.

to play a song
MediaPlayer.Play(SongCollection songs);
MediaPlayer.Play(SongCollection songs,int index);
and to get songs
MediaLibrary mLibrary = new MediaLibrary();
songs = mLibrary.Songs;
to check current state
if(MediaPlayer.State == MediaState.Playing || MediaPlayer.State == MediaState.Paused)
and to check active song
Microsoft.Xna.Framework.Media.MediaPlayer.Queue.ActiveSong.Name;

Related

Using the VLC ActiveX plugin to watch youtube videos C#

Recently I have started messing around with the VLC ActiveX plugin trying to play youtube videos in a WindowsFormApplication and I have run into some issues that I couldn't find any mention of or solution to. For simplicity I made a new project to demonstrate my problems:
private void button1_Click(object sender, EventArgs e)
{
Player.playlist.play();
}
private void button2_Click(object sender, EventArgs e)
{
url = textBox1.Text;
Player.playlist.add(url);
listBox1.Items.Add(url);
}
2 buttons, play and add to playlist.
Problem 1:
Audio cuts out a few seconds before the end of the video.
Problem 2:
I have been unable to get the control to play the next video in the playlist. It just reaches the end of the first video. If I click the play button again, it plays the first video. One thing I thought might be causing it was the AutoPlay property but it's set to true.
Problem 3:
The Player.playlist.next function, like most thing doesn't have a description and does not let me go to the next video in the playlist.
Problem 4:
The AutoLoop property does not work, assuming that it's supposed to get the control to loop a video.
Problems number 2 and 3 make me think that the songs aren't being added to the playlist correctly, but again I was unable to find any way to confirm or solve that issue.
Using VisualStudio 2015, VLC plugin version, as stated in the control properties, 3.0.1 Vetinari.
(edit) after testing I know that the videos are added to the playlist, but still it won't autoplay the next in playlist at the end.

Simultaneous microphone recording and audio playback (WP 8.1 XAML)

Using the MediaElement control and MediaCapture class, I am trying to record microphone input and play audio at the same time.
As soon as I start recording, whatever track is playing mutes. I don't think it stops, because it continues playing after the recording has stopped. I have added hooks into several events on the MediaElement and none are being fired. For example, CurrentStateChanged, MediaEnded, MediaFailed etc.
Recording code:
public async void InitializeMediaCapture()
{
_mediaCaptureManager = new MediaCapture();
var settings = new MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
settings.MediaCategory = MediaCategory.Other;
await _mediaCaptureManager.InitializeAsync(settings);
}
private async void CaptureAudio()
{
_recordStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);
var recordProfile = MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Auto);
await _mediaCaptureManager.StartRecordToStorageFileAsync(recordProfile, this._recordStorageFile);
//audio playback stops on preceding line of code
}
I use .Play() on the MediaElement to play the audio, with the control in my XAML and the audio source set there.
<MediaElement x:Name="playbackElement"
Source="ms-appx:///Audio/Song.mp3"
AutoPlay="False" />
I have also tried playing the audio as BackgroundAudio, but that didn't work either. Any ideas?
1.Never set a MediaElement's Source in XAML, unless that XAML is on a page that you navigate to after asking the user for consent.
2.Check to see if background music is playing and then set the source (in code).
Note: If you set the source and then immediately call Play(), the Play() will have no affect since the MediaElement will still be in the "Opening" state, instead set "AutoPlay = true" (works from code)
Here's a Reference

How to play a particular item from media play list?

I am using windows media player in windows forms application. I have 10 media items in my playlist. foo, foo1,foo2,foo3 ....
Now my playlist is playing lets say foo1. Now on button click I want to play item foo6. How do I play this ? i.e. how do I change my current playing item too foo6 ?
If this is not clear please comment, I will add more information.
Edit: Following is the code for creating a new playlist.
WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();
playlist = WMPLeft.playlistCollection.newPlaylist("myplaylist1");
for (int i = 0; i < FOO.Length; i++)
{
media = WMPLeft.newMedia(FOO[i]);
playlist.appendItem(media);
}
What I want is something like this
WMPLeft.playlist.Item(3).play();
This is wrong. But this is the kind of code I want.
After much research I have found this msdn link which shows how to do what I wanted.
// Declare a variable to hold the position of the media item
// in the current playlist. An arbitrary value is supplied here.
int index = 3;
// Get the media item at the fourth position in the current playlist.
WMPLib.IWMPMedia media = player.currentPlaylist.get_Item(index);
// Play the media item.
player.Ctlcontrols.playItem(media);
LINK to MSDN

How to play background music online in Windows 8

In my application, I use UI MediaElement. But when i click the Windows key, the music stops.
I tried using:
MediaControl.PlayPressed += MediaControl_PlayPressed;
MediaControl.PausePressed += MediaControl_PausePressed;
MediaControl.PlayPauseTogglePressed += MediaControl_PlayPauseTogglePressed;
MediaControl.StopPressed += MediaControl_StopPressed;
I set source MediaElement:
media.Source = new Uri("http://stream-hq.mp3.zdn.vn/fsgggsfdlwjglwjAAAAA/2a3f830202ea6d29bc7c5a5146401566/4ff5620a/2011/12/27/a/4/a4fcc199a184a93cfeb0fe35642c53bf.mp3", UriKind.RelativeOrAbsolute);
Please help me!
For a Metro/WinRT app to play audio in background, the app needs the following:
A MediaElement control that:
Is in a XAML page.
The AudioCategory property set to BackgroundCapableMedia (as in Armando's answer). There are other values for games or communication systems as needed. See the Audio Playback in a Metro Application for information on what the different options mean.
Use the MediaControl object to capture at least the following. Other events and properties can be handled if desired but the following are required for background playback to function.
PlayPressed
StopPressed
PlayPauseTogglePressed
PausePressed
Add audio to the list of support background tasks in the applications manifest. The manifest is usually called Package.appxmanifest. Select it in the Solution Explorer, go to the Declarations tab and check "Audio" as shown:
See the Transport Controls Guide for more info about capturing hardware buttons (e.g. play/pause on the keyboard) and the quickstart guide for creating a media player for more info.
This would be my first answer. Make sure you set AudioCategory="BackgroundCapableMedia" in your XAML like this:
<MediaElement x:Name="backgroundMusic"
AutoPlay="True"
AudioCategory="BackgroundCapableMedia"
Source="mms://betafm.santafe-conicet.gov.ar:1175">
</MediaElement>
Hope it helps!

How to play a sound in C#, .NET

I have a Windows application written in C#/.NET.
How can I play a specific sound when a button is clicked?
You could use:
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"c:\mywavfile.wav");
player.Play();
You can use SystemSound, for example, System.Media.SystemSounds.Asterisk.Play();.
For Windows Forms one way is to use the SoundPlayer
private void Button_Click(object sender, EventArgs e)
{
using (var soundPlayer = new SoundPlayer(#"c:\Windows\Media\chimes.wav")) {
soundPlayer.Play(); // can also use soundPlayer.PlaySync()
}
}
MSDN page
This will also work with WPF, but you have other options like using MediaPlayer MSDN page
Additional Information.
This is a bit high-level answer for applications which want to seamlessly fit into the Windows environment. Technical details of playing particular sound were provided in other answers. Besides that, always note these two points:
Use five standard system sounds in typical scenarios, i.e.
Asterisk - play when you want to highlight current event
Question - play with questions (system message box window plays this one)
Exclamation - play with excalamation icon (system message box window plays this one)
Beep (default system sound)
Critical stop ("Hand") - play with error (system message box window plays this one)
 
Methods of class System.Media.SystemSounds will play them for you.
 
Implement any other sounds as customizable by your users in Sound control panel
This way users can easily change or remove sounds from your application and you do not need to write any user interface for this – it is already there
Each user profile can override these sounds in own way
How-to:
Create sound profile of your application in the Windows Registry (Hint: no need of programming, just add the keys into installer of your application.)
In your application, read sound file path or DLL resource from your registry keys and play it. (How to play sounds you can see in other answers.)
Code bellow allows to play mp3-files and in-memory wave-files too
player.FileName = "123.mp3";
player.Play();
from http://alvas.net/alvas.audio,samples.aspx#sample6 or
Player pl = new Player();
byte[] arr = File.ReadAllBytes(#"in.wav");
pl.Play(arr);
from http://alvas.net/alvas.audio,samples.aspx#sample7
To play an Audio file in the Windows form using C# let's check simple example as follows :
1.Go Visual Studio(VS-2008/2010/2012) --> File Menu --> click New Project.
2.In the New Project --> click Windows Forms Application --> Give Name and then click OK.
A new "Windows Forms" project will opens.
3.Drag-and-Drop a Button control from the Toolbox to the Windows Form.
4.Double-click the button to create automatically the default Click event handler, and add the following code.
This code displays the File Open dialog box and passes the results to a method named "playSound" that you will create in the next step.
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Audio Files (.wav)|*.wav";
if(dialog.ShowDialog() == DialogResult.OK)
{
string path = dialog.FileName;
playSound(path);
}
5.Add the following method code under the button1_Click event hander.
private void playSound(string path)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = path;
player.Load();
player.Play();
}
6.Now let's run the application just by Pressing the F5 to run the code.
7.Click the button and select an audio file. After the file loads, the sound will play.
I hope this is useful example to beginners...
I think you must firstly add a .wav file to Resources. For example you have sound file named Sound.wav. After you added the Sound.wav file to Resources, you can use this code:
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.Sound);
player.Play();
This is another way to play sound.

Categories

Resources