BackgroundMediaPlayer not working in Windows Phone 8.1 - windows-phone-8.1

I have set up a Windows Phone project along with its associated BackgroundTask project, and have also done the needed declarations as explained in this article.
I need to select an audio file from the app UI (from the Music Library), and assign it to the BackgroundMediaPlayer to play it, so that the song can be played when the UI is in foreground, and also when it is suspended or the screen is locked.
I have tried the following but it does not work:
Set file path from UI:
StorageFile file = (await KnownFolders.MusicLibrary.GetFileAsync(
"Song from Music Library.mp3"));
BackgroundMediaPlayer.Current.SetUriSource(new System.Uri(file.Path,
UriKind.RelativeOrAbsolute));
BackgroundMediaPlayer.Current.Play();
Result: No audio is played. No exceptions are thrown.
Pass the file path as string to the backgroundtask, search for the file from Music Library and play it:
IReadOnlyList<StorageFile> songs = await
KnownFolders.MusicLibrary.GetFilesAsync(CommonFileQuery.OrderByName);
StorageFile fileToPlay = songs.AsQueryable().Single(
s => s.Path == toPlay);
BackgroundMediaPlayer.Current.SetFileSource(fileToPlay);
Result: The debugger does not move forward after "StorageFile fileToPlay = songs.AsQueryable().Single(s => s.Path == toPlay);". It just stops there.
Again, no audio is played. No exceptions are thrown.
Appreciate your help.

BackgroundMediaPlayer set Uri source of Media library item
StorageFile file = (await KnownFolders.MusicLibrary.GetFilesAsync()).FirstOrDefault();
BackgroundMediaPlayer.Current.SetUriSource(new Uri(file.Path, UriKind.RelativeOrAbsolute));
See the answer.

Related

Error when Play file .mp3 (StorageFile ) with BackgroundMediaPlayer in isolated storage windows phone 8.1 rt

I use Windows Phone 8.1 Runtime
I get my song in isolated storage
My code :
storageFile is song in isolated storage
IRandomAccessStream stream = await storagefile.OpenAsync(FileAccessMode.Read);
BackgroundMediaPlayer.Current.SetStreamSource(stream);
BackgroundMediaPlayer.Current.Play();
When I play it,but it not work , like photo (try emulator and device lumina 520)
http://photoshare7.com/image/f5a
_message:
Unable to cast object of type 'Windows.Media.Playback.MediaPlayer ' to type 'Windows.Media.Playback.IMediaPlayerSource'
Please Help me .Tks all.Sr my English.
I just ran into the same issue :) This happens when you call SetStreamSource from foreground application. See [https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn642090.aspx]. To play audio from a file, you need to send background audio player a message (see [How to pass an object to background project in Windows Phone?).

Playing a Media File from Isolated Storage through MediaPlayer launcher in Windows Phone

I am able to store media files into the Isolated Storage and able to retrieve the same.
Now what I want is to play the same media file through Media Player Launcher, I have tried using Media Element and I am able to play the stored media file but when I tried playing through Media Player launcher it gave me Argument Exception.
The Problem what I understood was that while retrieving the media file from Isolated Storage, it is returning in the form of "IsolatedFileStream".
You can set the media element source by the following method :
using (IsolatedStorageFile storageFile= IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream= storageFile.OpenFile(VideoFile, FileMode.Open, FileAccess.Read))
{
this.mediaElement.SetSource(fileStream); // Accepts IFileStream
}
}
But there is no method to set the Media Property of the Media Player Launcher from Isolated Storage.
MediaPlayerLauncher mediaPlayerLauncher1 = new MediaPlayerLauncher();
mediaPlayerLauncher1.Media = this.mediaElement.Source;// Argument Exception
mediaPlayerLauncher1.Controls = MediaPlaybackControls.All;
mediaPlayerLauncher1.Location = MediaLocationType.Data;
mediaPlayerLauncher1.Show();
Please provide me the way to sort this problem.
use this code .
it's very simple and work fine for IsoStorage files
MediaPlayerLauncher med = new MediaPlayerLauncher();
med.Media = new Uri("FileLocationInIsolatedStorage", UriKind.Relative);
med.Show();

Unauthorized Access Exception when Creating an instance of SpeechSynthesizer in WP8.1 Emulator

I was trying to recreate the simle Text to Speech example used on the MSDN website. However whenever the code came to create the instance of the SpeechSynthesizer class it failed with a Unauthorised Acception error when running on the WP8.1 emulator. I currently do not have an actual device to test on to see if this makes a difference.
My code was simply:
private async void TTS()
{
// The media object for controlling and playing audio.
MediaElement mediaElement = new MediaElement();
// The object for controlling the speech synthesis engine (voice).
var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
// Generate the audio stream from plain text.
SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Hello World");
// Send the stream to the media object.
mediaElement.SetSource(stream, stream.ContentType);
mediaElement.Play();
}
I know there was an issue with the SpeechSynthesizer in Windows 8.1, and I found solutions to this when looking to fix the problem, but found little about the problem with WP8.1 SpeechSynthesizer. Has anybody else came across this problem and found a fix?
You should add one DeviceCapability in Package.appxmanifest file:
In DeviceCapability Tab, check the microphone, because it will provides access to the microphone’s audio feed, which allows the app to record audio from connected microphones.
Look at this library: App capability declarations (Windows Runtime apps)

Update Album Art Windows 8.1 Store App

I'm trying to update the album art for a background playing track in Windows 8.1 Store app (C#/Xaml) but although I get no exceptions, the image doesn't update in the little transport popup...
this is the code I'm executing to update it:
var track = App.MediaPlayer.Tag as Track;
await App.Api.Cache.DownloadFile("currentalbumart.png", new Uri(track.medium_image_url));
// Get the updater.
SystemMediaTransportControlsDisplayUpdater updater = App.SystemControls.DisplayUpdater;
updater.Type = MediaPlaybackType.Music;
updater.MusicProperties.AlbumArtist = track.artist;
updater.MusicProperties.Title = track.name;
updater.Thumbnail = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appdata:///local/currentalbumart.png"));
updater.Update();
The DownloadFile method executes without any issue, and if I navigate to the local folder, indeed the image does get downloaded. I thought perhaps I'm using the wrong URI to it, but I don't get any exceptions when I assign it to the thumbnail...
what might be wrong here? many thanks
I actually had this problem for ages but finally fixed it today, after about 20+hours on previous apps to get it working. If you use the following code to update your thumbnail, you can use a html link:
updater.thumbnail = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(new Windows.Foundation.Uri("http://www.example.com/logo.jpg"));
This leaves the full code for the function to be:
//Used to update the media controls etc
function UpdateSongInfoManually(artist,songName,AlbumArtist, SongArt) {
// Get the updater.
var updater = systemMediaControls.displayUpdater;
updater.type = 1; //1=Music
// Music metadata.
updater.musicProperties.AlbumArtist = artist;
updater.musicProperties.AlbumArtist = mixName;
updater.musicProperties.Title = songTitle;
updater.thumbnail = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(new Windows.Foundation.Uri(MixArt));
// Set the album art thumbnail.
// RandomAccessStreamReference is defined in Windows.Storage.Streams
//updater.Thumbnail =
// RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Music/music1_AlbumArt.jpg"));
// Update the system media transport controls.
updater.update();
}
I have a similar problem in a Windows 8.1 Phone (Store) app. First I tried to copy MP3 files (with ID3 tags) and a folder.jpg file as album art using Windows Explorer to the Phones Music library (Music//). Using standard Music app everything works fine.
Than I tried to do the same inside the app I am developing. I download mp3 and jpg file from a server and stored them into the music library. I verified the result with the phones standard Music App. The ID3 tags are detected correctly but the album art jpg file is not shown. And now the thing becomes completely magic. Using Explorer I copied the jpg file to the PC and checked it. Jpg looks fine. Than I copied it back to the phone. And now standard music app shows correct album art !!
Maybe it has something to do with file security.

org.osmf.media.MediaPlayer Throw Error: The specified capability is not currently supported

I have a back end sound.php which can return .m4a sound file from web server and I can make a web request with id to the sound.php to return specify .m4a file. i.e. sound.php?id=1234
I am now trying to use org.osmf.media.MediaPlayer and AudioElement and URLResource
var mediaPlayer:MediaPlayer = new MediaPlayer();
var ae:AudioElement = new AudioElement(new URLResource("http://xxx.com/sound.php?id=12"));
mediaPlayer.media = ae;
mediaPlayer.play();
and it throw error of The specified capability is not currently supported .I have tested the link via browser which is return a .m4a file sucessfully .
I dont understand if it is claiming the requesting method or the returned file , would somebody has any idea? Thanks
Try setting the MediaPlayer.autoPlay to true or you should wait until the media is loaded, which is signaled through the mediaPlayerStateChange event, with state READY.
[UPDATE]
As stated in the NetStream - Adobe ActionScript® 3 (AS3 ) API Reference page and also in the Supported codecs | Flash Player page:
Flash Player 9 Update 3 plays files derived from the standard MPEG-4
container format that contain H.264 video and/or HE-AAC audio, such as
F4V, MP4, M4A, MOV, MP4V, 3GP, and 3G2. One thing to note is that
protected MP4 files, such as those downloaded from iTunes or digitally
encrypted by FairPlay, are not supported.
It seems you will have to try the NetStream approach.