Play remote mp3 using Song.FromUri - windows-phone-7

I am trying to play audio files hosted on Azure blob. I serve them up over HTTP (not HTTPS) and I use SoundEffect.Play from a stream I open using web client. This works great with WAV files.
WebClient wc = new WebClient();
wc.OpenReadAsync(uri);
wc.OpenReadCompleted += (s, e) =>
{
try
{
var sfx = SoundEffect.FromStream(e.Result);
FrameworkDispatcher.Update();
sfx.Play();
}
catch (Exception ex)
{
Debug.WriteLine("Well, crap!");
}
};
For MP3s, I use this method:
var song = Song.FromUri(item.Title, uri);
FrameworkDispatcher.Update();
MediaPlayer.Play(song);
It works in the emulator, sometimes, but NOT on a device! I even tried not using Zune to deploy and use WPConnect to deploy the XAP directly to the device (some folks said this made it work)
This is driving me nuts! These mp3 files should just play, right?

Whether you can play an MP3 or not very much depends upon the specifics of that file.
See
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff462087(v=vs.105).aspx#BKMK_AudioSupport for details of the MP3s that are supported.
Please also note that the codecs supported by devices and the emulator have some differences so there are cases where you can play something in the emulator but not on a device.

Related

Why is $DATA empty when downloading a file via UWP

I'm experiencing an issue while downloading an exe file from within an UWP App. The EXE cannot be run after downloading. When I download it via any browser, it works as expected. My research so far brought me to Alternate Data Streams.
When I download the EXE via any browser, the file gets an ADS like My.EXE:Zone.Identifier:$DATA with ZoneId=3 and some additional stuff inside.
When I download the same file from my UWP, it's also like My.EXE:Zone.Identifier:$DATA, but $DATA is empty with a size of 0 bytes like in the screenshot.
Did anyone experience the same issue and found a solution? Any hint would be great.
EDIT[Code added]:
private async void Download_Click(object sender, RoutedEventArgs e)
{
var uri = new Uri(resourceLoader.GetString("Link"));
var success = await Windows.System.Launcher.LaunchUriAsync(uri);
}

Programmatically download APK from Google Drive doesn't work

When finding random apk's online through urls, I can successfully download and install them, the user is prompted to ask if they want to install comes up. But when I upload the same apk's to Google drive, and then run the download url from Google drive, the apk's doesn't work. I get a "There was a problem while parsing the package" on the device screen. I put a log to see how much data is being downloaded. And it appears that the apk's being downloaded from google drive are barely the size of what the apk's should be. Around 50k instead of 4MB. I see a lot of questions online about this, but none have talked about Google play not sending the full file. Is there something I'm missing in order to get the full apk downloaded from Google drive? here is the code,
private void downloadApk(){
// checkVersion();
String extStorageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
File folder = new File(extStorageDirectory);
folder.mkdirs();
File file = new File(folder, "app-debug.apk");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
DownloadApkTask downloadApkTask = new DownloadApkTask(APKURL,file);
downloadApkTask.execute();
}
public class DownloadApkTask extends AsyncTask<String, Void, Void> {
String fileURL;
File directory;
public DownloadApkTask(String fileURL,File directory) {
this.fileURL = fileURL;
this.directory = directory;
}
#Override
protected Void doInBackground(String... params) {
Log.v("DO in Back started","Started");
try {
FileOutputStream f = new FileOutputStream(directory);
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
Log.v("PROGREsS", String.valueOf(len1));
f.write(buffer, 0, len1);
}
f.close();
directory.setReadable(true,false);
} catch (Exception e) {
System.out.println("exception in DownloadFile: --------"+e.toString());
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void stringReturn) {
super.onPostExecute(stringReturn);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/app-debug.apk");
Log.v("STARTING INSTALLATION","-----");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
}
}
Based from this page, parsing error occurs on app installment.
When you try to install an application suddenly a window pop-ups saying "there is a problem parsing the package" which means the application cannot be installed due to apk parser i.e. parsing issue.
There are several reasons why this parsing error occurs & definitely one of them is responsible for your parsing error:
File may be downloaded incompletely.
Application might be not suitable for your hardware or OS version.
Due to security issue settings
Corrupted APK file.
Follow the steps shown below for fixing the android parse error on your mobile devices:
Check Manifested app apk file.
Change the Andriomanifest.xml file to its default setting & also check the name of that file. If the original name of the file is “aap.apk” & if you renamed it as "app1.apk" then also it might cause an error. If you have some knowledge of coding, look into the app code if there is some problem with coding.
Security settings.
For the security purpose, the phone has an inbuilt setting that doesn't allow installing applications from a 3rd party provider other than mobile apps provided by play store. Don’t install an app from the non-trusted website. That might really risk your mobile.
Enable USB debugging.
Go to the settings >> Scroll down then, at last, you will see option “About device” select it.
Look for option “build number.”
Tap on “Build number” for 7 times.
You will see a message “you are now a developer.”
Once you enable to go back to settings
Choose “Developer options.”
Tick mark "USB debugging."
Corrupted App file.
The parse error may cause due to corrupted file too. In this case, download a new but complete APK file, & try again to install it again. This might help you.
Disable Antivirus.
If you have installed applications like antivirus & cleaner apps, then this can also prevent some apps installation. This prevention is due to the safety purpose of the handset. They block suspicious downloads from non-trusted sites. If you really want to install that app then disable the antivirus temporarily.
Clear cache cookies of play store.
Open google play store
Select sidebar & choose option “settings.”
In general settings, you will find out to “clear local search history.”

Windows Phone leave background music playing

My Windows Phone app was rejected because when I play a sound in the browser in my app it stops existing background music from playing (rejection issue 6.5.1).
How do I update my application to not stop background music?
My JavaScript is something like this:
var mySound = new Audio(soundpath);
Sound.play(mySound);
I could not find anything in Microsoft.Xna.Framework.Media that handles how the app handles browser sound, but maybe I missed something?
What you are looking for is from the xna soundeffect
Here is what I use and it works for me great for all my sound effects.
(You cannot use music with a 'soundeffect' object, as it will not pass the windows store qualifications. You have to use the MediaElement for music)
I can’t remember which you need but I have all these includes
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
and this function
private static void LoadSoundEffect(string fileName, SoundEffectInstance mySound)
{
var stream = TitleContainer.OpenStream("Sounds/" + fileName);
var effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
mySound = effect.CreateInstance();
}
If you use a SoundEffectInstance, you can start,pause,stop the soundeffect etc. without all the problems.
SoundEffect works as well, but I prefer the SoundEffectInstance.
Edit: If you use soundeffect, it will not effect the music being played.
I'm assuming that you are building a native Windows Phone app (.XAP) and then using a browser control to display your app content. If that is the case, you have checks that you will need to perform when the app is first opened. Below is a description of how to resolve that issue.
You will need to add this piece of code to your app (I suggest you check this every time your app loads/opens). Add a reference with the using statement as well for the Media.
if (Microsoft.Xna.Framework.Media.MediaPlayer.State == MediaState.Playing)
{
MessageBoxResult Choice;
Choice = MessageBox.Show("Media is currently playing, do you want to stop it?", "Stop Player", MessageBoxButton.OKCancel);
if (Choice == MessageBoxResult.OK)
mediaElement1.Play();
}
If the user allows or disallows the media to be played you must put checks in your JavaScript for this. You will fail certification again if the user says "Cancel" and you play music anyway.
There is more information on the topic here.

Extrading Metadata of an audio streaming file (MediaElement)

I just started to develop for the Windows Phone (7.1/8) platform and am still not really familiar with it.
My plan is an internet radio app which streams the audio file from a server. I used MediaElement to set the source property to the streaming URL.
It works and the app starts playing the music but I can't read any metadata about the song which is coming from the server such as artist name/title or any string which I can use to know about the song itself.
I've been searching around and tried the MediaReached event as well, but it never gets fired as well?
So any idea what should I do?
and My Code Behind Sample:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
MyMedia.Source = new Uri("MyURL");
}
I hate to tell you this but it is because the MediaElement.Attributes are not supported for Windows Phone 7 - all the limitations can be found here: http://msdn.microsoft.com/en-us/library/ff426928(VS.95).aspx

Is a .mp4 video file recorded in the WP Emulator ready for outside storage?

MS have released some code examples where a video is recorded in the WP Emulator and then saved in isolated storage. Is this .mp4 file ready to export away from the WP Emulator and play in other applications? Or is it needed to format it in some way or the other? If so, how to do it?
http://channel9.msdn.com/Shows/Inside+Windows+Phone/Inside-Windows-Phone-16-Mango-Camera-APIs
Source code for video recording is located in the WP project is called CameraUpload:
https://skydrive.live.com/?cid=bc58fec5c97e307a&sc=documents&id=BC58FEC5C97E307A%21295
http://msdn.microsoft.com/en-us/library/hh394041(v=vs.92).aspx
Edit:
I am trying to upload a video recorded in the WP Emulator to Azure blob.
A file does get uploaded, but I am not able to play that file in Zune.
I would like to be able to play the video file recorded in the WP Emulator on Zune, what to do to enable this?
The method in the Azure WCF service role, which saves the video looks like this:
(Please forgive if the method parameters have slightly misdirecting and confusing names.)
bool SaveImage(int salesItemId, string contentType, byte[] photo);
The video is saved to a blob container named "firstmay".
The code in the phone client saving the video looks like this:
client.SaveImageAsync(77, "mp4", GetPhotoBytes(m_capturedFileName));
public byte[] GetPhotoBytes(string fileName)
{
using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream isoStream = appStorage.OpenFile(m_capturedFileName, FileMode.Open);
byte[] buffer = new byte[isoStream.Length];
isoStream.Read(buffer, 0, (int)isoStream.Length); isoStream.Close();
return buffer;
}
}
When uploading a video from the WP client application, one first records a video and then uploads it by clicing "Save", it all goes on in the MainPage.
The client and server application can be downloaded from skydrive:
https://skydrive.live.com/redir?resid=159250F5CE7FE134!118
That'll all depend on what you want to be able to play back the content on. PCs with Zune would be fine, and I'd expect other video capable mobiles to be fine, but older mobiles, or standard Vista installs would require additional software, or transcoding of the video to be playable.

Resources