I have a simple Android webview app, built with Phonegap Build. When trying to play a local mp3 file that's included in the APK, nothing happens. However, if I pull an mp3 from the web it works.
e.g.
<audio src="www.example.com/01.mp3" > WORKS JUST FINE
<audio src="01.mp3" > DOES NOT WORK
I'm new to Android development, so I can only guess that the file is somehow inaccessible by Android's media player. Here's a link to the errors/warnings from my log file if that helps shed any light on the problem.
http://pastebin.com/isS542RE
I have it working using Phonegap's Media class. I must have been doing something wrong initially, but I'm not sure what. The proper way to access it through the media object is as Simon stated previously:
myMedia = new Media("/android_asset/www/test.mp3");
You have to put the full path as the media class defaults to the /SDCARD directory on relative paths.
Simon also has a great write up about using the media class:
http://simonmacdonald.blogspot.com/2011/05/using-media-class-in-phonegap.html?m=1
As for the HTML5 method and using the AUDIO tag, I'm still not certain why it fails on local files. Phonegap's media class works just fine though and is probably a more reliable solution anyhow. Phonegap FTW!
Have you tried pointing the file to the local file system path using "file:///android_assets/01.mp3" assuming your .mp3 is within the applications assets directory?
We had the same problem, the media files, in order to be played, needs to be in the external storage dir. Try to move your HTML and related files to /mnt/sdcard/.
I filed an official bug report on this:
http://code.google.com/p/android/issues/detail?id=41995&thanks=41995&ts=1356643666
Related
I am attempting to build a UnoPlatform app which has the capability to play some embedded mp3 sound effect files.
Using MediaPlayerElement, it is straight-forward to play media from HTTP sources, however it seems there must be some platform specific hoops to jump through to get embedded resource files to play.
The following event handler works for UWP, but not IOS, Android, or WebAssembly:
private void SpinButton_OnClick(object sender, RoutedEventArgs e)
{
player1.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Sound.mp3"));
}
I have attempted to follow the info here https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/mediaelement for how to embed the media on IOS, but it does not seem to work for me.
Is there a trick to this? is there a sample out there with this working?
The current implementation of the MediaPlayerElement is still a bit limited, but playing back mp3 on Android and iOS should be supported.
The trick is to put the .mp3 files in the right location as they are not yet (as compared to image files) automatically copied over to the correct platform-specific location during build. In case of Android, you need to put the audio files in the Assets folder, for example:
And the Build Action needs to be set to AndroidAsset.
In case of iOS, the files go to the Resources folder and have the Build Action set to Bundle Resource.
You can now reference the files as if they were in fact in the "Assets" folder, using the ms-appx: URI scheme:
<Grid>
<MediaPlayerElement AutoPlay="True" Source="ms-appx:///Assets/test.mp3" />
</Grid>
This is just a quick example, but hopefully you can use it as a starting point. Whenever in doubt, it is quite useful to browse the source code of the respective features on GitHub, for example here is the media file loading implementation for Android.
For WebAssembly, MediaPlayerElement is not supported yet, but if you want to see it supported soon, please vote up on this issue.
I have a hybrid app that plays mp3, but I don't want to set all the mp3s inside the assets folder, I want the user to select mp3 that he/she likes to download.
So the question is... Can I use a previously downloaded mp3 inside webview, natively in the app data folder?
Something like this in file:///android_assets/index.html:
<html><body>
<audio src="file:///Android/data/com.packagename/previouslydownloaded.mp3">
</body></html>
Thanks a lot.
Since Android Lollipop you can use files from anywhere because WebView has gained full support for content:// scheme. Older Android versions may also work (for example, MediaPlayer gained Uri support for playback in API 14). Just create a ContentProvider and refer to associated content uris directly in web page.
<html><body>
<audio src="content://com.example.authority/file/path.mp3">
</body></html>
Both files from secondary external storage (accessible only via Storage Access Framework) and your app private files, exposed via FileProvider or something like that, should be accessible via content://.
Note, that writing ContentProviders is slightly tricky business, you may want to look at following question and all it's answers: Is it possible to use content:// as a source for an <audio> element in a WebView
I am new to phoneGap. I am creating a dummy app to upload an audio file from iOS/android device on local server. So I was wondering if there is a way to open the audio gallery and select the required audio file to upload. Similar to an image gallery.
Is it possible to open audio list and select necessary audio file? If not, what is the alternative?
As far as I remember, you cannot reach the pictures and audio files stored in the media gallery. Therefore, you may need to implement your own native plugin and call the appropriate methods from your JavaScript code.
You can capture audio/image by using Capture plugin of Phonegap. Check this out Capture Plugin
You can perform basic File operations as well. Check this out File Plugin
For your situation, you have to implement native interface which can be accessed by your JavaScript code. This would be an alternative way for it. If you desire to create your own plugin, please have a look at Plugin Development Guide. This might be helpful.
Well I have implemented code using file plugin which will iterate through all the folders recursively to find all the image (created this as I needed multi selection approach which is not directly provided by cordova) . You can modify the extension of image (search) i.e png,jpeg etc with audio extension.
With this code you will get the path of audio files and you can make use of filetransfer to upload it on server
Hope it helps you
I've been following this blog to help me make a simple music player function with an Android app (http://simonmacdonald.blogspot.com/2011/05/using-media-class-in-phonegap.html).
All works well - but I would like some way to extract a list of all music media stored on the phone so the user can click a file and play it. Does anyone know if this is possible via phone gap? I'd need to access track name/artist etc and a link to the track.
Cheers
Paul
Prognosis is not good.... PhoneGap does not have an API for accessing the media library. So, you would have to fall back to the File API to locate music files, and then... you would have to read the binary data in the actual files to extract out the track/artist metadata. Ouch. Seems pretty impractical.
[EDIT]
By the way, PhoneGap is not the only game in town. Appcelerator seems to solve the same problem, and apparently has a richer API, which includes
access to media metadata
When I download MP3 files with my app they go to the download folder and when I navigate there and try to play them I get content not supported. I have looked around a little bit, but can't figure out why. Anyone know how I can fix this, is it something with the MIME types? What does this do:
connection.setRequestProperty("Content-Type", "audio/*");
MP3 • Mono/Stereo 8-320Kbps constant (CBR) or variable bit-rate
(VBR) MP3 (.mp3)
Android Supports MP3 in the above format. Incorrect encoding makes this type of error. Also check to see if the extension and file names are proper. Because some downloaders may name the file like mywebsite.com-mysong.mp3. The android OS may wrongly interpret the file as a .com file rather than a .mp3 file.
Reference:
http://developer.android.com/guide/appendix/media-formats.html#core
Please ensure you save with the .mp3 extension. sometimes it can get lost along the way.