I've been working on a basic music player app.
I am trying to keep questions concise and so not put too much code or irrelevant info.
Basically, I want to add all mp3/m4a/wma to a long List of Strings. But no matter what methods I call from different libraries I cannot get it.
Here's what I've ended up with:
string musicPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic).AbsolutePath;
string musicPath_sd = GetExternalFilesDir(Android.OS.Environment.DirectoryMusic).ToString();
Log.Info("FILEPATHS TAG", "phone memory music directory path is: " + musicPath);
Log.Info("FILEPATHS TAG", "sdcard music directory path is: " + musicPath_sd);
So the string musicPath is fine and returns:
Time Device Name Type PID Tag Message
FILEPATHS TAG phone memory music directory path is: /storage/emulated/0/Music
FILEPATHS TAG sdcard music directory path is: /storage/emulated/0/Music
As you can see the 'sdcard music directory' is returnign the exact same location, but really it should be: storage/**extSdCard**/MUSIC (I know this because I browsed to the songs on my phone in EZ File Explorer and checked the info button which displays the path.
I am using Samsung Note 3 on Android 5.0 API 21 as physical debugging device
Related
I need to play video from sdcard on VideoView and always getting "Can't play this video message". I am testing on physical device. File is on external storage, and can be played by build in phone apps with no problem.
I have read some topics before and tried changing file format and resolution with no effect.
Second guess was path to the file so tried to change it multiple times in many variants using Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, creating File variable and getting path from File.Path.
Got the TextView where I put path every time and it is always correct.
I have no idea why it does not work.
Code:
Java.IO.File file = new Java.IO.File (Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/myimage/", "video.mp4");
tView.Text = file.Path;
MediaController mediaController = new MediaController(context: this);
vView.SetMediaController(mediaController);
mediaController.SetAnchorView(vView);
vView.SetVideoPath(file.Path);
vView.Start();
File.Path value:
"/storage/emulated/0/myimage/video.mp4"
EDIT: I have created "raw" folder in resource putted file there and then used following path:
vView.SetVideoPath("android.resource://" + PackageName + "/" + Resource.Raw.video);
It works, but if anyone know how to get the proper path to folder on sdCard?
Finally I figured it out. The problem was that my phone mounted card at path /storage/xxxx-xxxx/ my research revealed that the dir's name come from volume serial number(where android automatically mount card) and ExternalStorageDirectory refers to other locations in each android device. Sometimes it's internal sometimes usb mass storage.
Anyway didn't manage to find any method that returns the path so created following method:
string GetCardMountPoint()
{
string[] listOfDirs = Directory.GetDirectories("/storage/");
string path = null;
foreach ( string dir in listOfDirs)
{
if(dir.Contains('-'))
{
path = dir;
}
}
return path;
It's not much of sophisticated solution but it works for now.
I am using the media plugin for xamarin forms (by james montemagno) and the actual taking of the picture and storing it works fine, I have debugged the creation of the image on the emulator and it is stored in
/storage/emulated/0/Android/data/{APPNAME}.Android/files/Pictures/{DIRECTORYNAME}/{IMAGENAME}
however in my app it will get a list of file names from an API I want to check if the image exists in that folder.
The following works fine on IOS
var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string jpgFilename = System.IO.Path.Combine(documentsDirectory, App.IMAGE_FOLDER_NAME);
jpgFilename = System.IO.Path.Combine(jpgFilename, name);
I have tried the 2 following methods for getting it on android but both are incorrect
var documentsDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string jpgFilename = System.IO.Path.Combine(documentsDirectory, App.IMAGE_FOLDER_NAME);
jpgFilename = System.IO.Path.Combine(jpgFilename, name);
Java.IO.File dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DataDirectory + "/" + App.IMAGE_FOLDER_NAME + "/" + name);
dir ends up as
/storage/emulated/0/data/{DIRECTORY}/{IMAGENAME}
jpgFileName ends up as /data/data/{APPNAME}.Android/files/{DIRECTORYNAME}/{IMAGENAME}
I dont want to hardcode anything in the paths but neither of these are right. I could not find anything in the GIT documentation for getting the file path except by looking at the path of the file created when taking a picture
The problem
I had the same kind of issue with Xamarin Media Plugin. For me, the problem is:
we don't really know where the plugin save the picture on android.
After reading all documentation I found, I just noted this:
... When your user takes a photo it will still store temporary data, but also if needed make a copy to the public gallery (based on platform). In the MediaFile you will now see a AlbumPath that you can query as well.
(from: Github plugin page)
so you can save your photo to your phone gallery (it will be public) but the path is known.
and we don't know what means "store the temporary data".
Solution
After investigating on how/where an app can store data, I found where the plugin stores photos on Android >> so I can generate back the full file names
In your Android app, the base path you are looking for is:
var basePath = Android.App.Application.Context.GetExternalFilesDir(null).AbsolutePath
It references your app's external private folder. It looks like that:
/storage/emulated/0/Android/data/com.mycompany.myapp/files
So finally to get your full file's path:
var fullPath = basePath + {DIRECTORYNAME} + {FILENAME};
I suggest you to make a dependency service, for instance 'ILocalFileService', that will expose this 'base path' property.
Please let me know if it works for you !
I resolved a similar problem. I wanted to collect all files for my app in a folder visible to all users.
var documentsDirectory = Android.OS.Environment.GetExternalStoragePublicDirectory(
Android.OS.Environment.DirectoryDocuments);
If you want to create Directory, add to your class using System.IO; and you have the same functions in a normal .NET application.
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
If you want to create files or directory, you can use PCLStorage.
public async Task<bool> CreateFileAsync(string name, string context)
{
// get hold of the file system
IFolder folder = FileSystem.Current.LocalStorage;
// create a file, overwriting any existing file
IFile file = await folder.CreateFileAsync(name,
CreationCollisionOption.ReplaceExisting);
// populate the file with some text
await file.WriteAllTextAsync(context);
return true;
}
to get the private path
var privatePath = file.Path;
to get the public Album path
var publicAlbumPath = file.AlbumPath;
se the documentation here https://github.com/jamesmontemagno/MediaPlugin
I'm trying to save a mp3 file, into the device's music library folder.(I Want it to show up immediately as a playable song).
More clearly i'm looking for the path /storage/emulated/0/Music/, in each phone.Im not sure if this path changes, so i would rather not take the risk.
I have tried this paths:
System.Environment.SpecialFolder.CommonMusic -> Blank
System.Environment.SpecialFolder.MyMusic -> /data/user/0/App.App/files/Music
Android.OS.Environment.DirectoryMusic -> /Music
None of this offer, what i wanted.I tried writing to the /storage/emulated/0/Music/ which did the job, but i dont think this path is stable.
Anyone knows how can i get the music folder path programmatically ? Im coding in C# using xamarin.android.
I used this code to get access to my Music Folder
public void StartPlayer(String filePath)
{
string Mp3Path = Path.Combine("/sdcard/"+
Android.OS.Environment.DirectoryMusic,
filePath);
if (player == null)
{
player = new MediaPlayer();
}
player.Reset();
player.SetDataSource(Mp3Path);
player.Prepare();
player.Start();
}
The sdcard shortcut works great for me, hope it works for you too ^^
So I am trying to add a music player to my web browser so users can play music in it from a specific folder in it's app data folder ("\AppData\Roaming\MantouWeb360\MWP360")
I was looking around a came accross a solution here
The user can select files (multiple ones too) which is then added to a playlist and then played.
However I am not so experienced at C# and VS doesn't recognise the wmp.playlistCollection.newPlaylist("myplaylist"); part of the code (and the following code using the wmp. part). This must mean there was code outside the example defining wmp. but I do not know what it is. Any way to get this code to work so that users can play music from their selected files on my web browser or is there possibly a better way which copies the music to the folder I said above and plays them in order (loop at the end of the last file) so that the user does not need to keep selecting files?
WMPLib.IWMPPlaylist playlist = wmp.playlistCollection.newPlaylist("myplaylist");
WMPLib.IWMPMedia media;
if (ofdSong.ShowDialog() == DialogResult.OK)
{
foreach (string file in ofdSong.FileNames)
{
media = wmp.newMedia(file);
playlist.appendItem(media);
}
}
wmp.currentPlaylist = playlist;
wmp.Ctlcontrols.play();
I am using XNA and I want to save files to Vista's "Saved Games" folder.
I can get similar special folders like My Documents with Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) but I cannot find an equivalent for the Saved Games folder. How do I get to this folder?
http://msdn.microsoft.com/en-us/library/bb200105.aspx#ID2EWD
Looks like you'll need to use Microsoft.Xna.Framework.Storage and the StorageLocation class to do what you need to.
Currently, the title location on a PC
is the folder where the executable
resides when it is run. Use the
TitleLocation property to access the
path.
User storage is in the My Documents
folder of the user who is currently
logged in, in the SavedGames folder. A
subfolder is created for each game
according to the titleName passed to
the OpenContainer method. When no
PlayerIndex is specified, content is
saved in the AllPlayers folder. When a
PlayerIndex is specified, the content
is saved in the Player1, Player2,
Player3, or Player4 folder, depending
on which PlayerIndex was passed to
BeginShowStorageDeviceSelector.
There is no special folder const for it so just use System Variables. According to this Wikipedia article Special Folders, the saved games folder is just:
Saved Games %USERPROFILE%\saved games Vista
So the code would be:
string sgPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "saved games"));
...
EDIT: If, as per the comments, localization is an issue and as per your question you still want access to the Saved Games folder directly rather than using the API, then the following may be helpful.
Using RedGate reflector we can see that GetFolderPath is implemented as follows:
public static string GetFolderPath(SpecialFolder folder)
{
if (!Enum.IsDefined(typeof(SpecialFolder), folder))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GetResourceString("Arg_EnumIllegalVal"), new object[] { (int) folder }));
}
StringBuilder lpszPath = new StringBuilder(260);
Win32Native.SHGetFolderPath(IntPtr.Zero, (int) folder, IntPtr.Zero, 0, lpszPath);
string path = lpszPath.ToString();
new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
return path;
}
So maybe you think all i need is to create my own version of this method and pass it the folder id for Saved Games. That wont work. Those folder ids pre-Vista were actually CSIDLs. A list of them can be found here. Note the Note: however.
In releasing Vista, Microsoft replaced CLSIDLs with KNOWNFOLDERIDs. A list of KNOWNFOLDERIDs can be found here. And the Saved Games KNOWNFOLDERID is FOLDERID_SavedGames.
But you don't just pass the new const to the old, CLSIDL based, SHGetFolderPath Win32 function. As per this article, Known Folders, and as you might expect, there is a new function called SHGetKnownFolderPath to which you pass the new FOLDERID_SavedGames constant and that will return the path to the Saved Games folder in a localized form.
The easiest way I found to get the Saved Games path was to read the Registry value likes this:
var defaultPath = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Saved Games");
var regKey = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders";
var regKeyValue = "{4C5C32FF-BB9D-43b0-B5B4-2D72E54EAAA4}";
var regValue = (string) Registry.GetValue(regKey, regKeyValue, defaultPath);
I changed the location of my Saved Games via the Shell multiple times and the value of this key changed each time. I use the USERPROFILE/Saved Games as a default because I think that will work for the default circumstance where someone has never changed the location.