问题
I should start by giving some background. We're about to release a non profit html5 children's game for android platforms. I have used eclipse to create an apk file but there is no audio (the rest of the game works fine). I have tried phone gap but there is no audio for the game. After doing days of research and possible solutions, I decided to ask here on stackoverflow.
The game uses html/css/js.
Project folder structure in Eclipse is: assets(android_assets)->www->index.html, game.xml, js Folder (the game engine), styles Folder, and assets Folder. The sub asset folder contains -> music folder, images folder, and backgrounds folder
Error Message:
The index.html file loads the game.xml. The game.xml file loads all the files in the assets folder. However, all music files are not found in logcat:
Failed to open file '/android_asset/www/assets/music/song.mp3.' (No such file or directory). My xml file sound call is: <source href="assets/music/song.mp3" type="mp3" />
.
I use this same reference structure with images and backgrounds that are currently loading fine. When loading the music from an external url like www.oursever.com/song.mp3. The game is able to play the song just fine when running as an apk file on an Android device. So the code is right but the local file path to the song in the apk file is wrong.
After much reading, I learned that the assets folder is compressed and it is not recommended to read audio files from that folder without uncompressing them. The alternative is to place the mp3 files in the res/raw folder. I change my xml to read: ''. However, I receive the same error about the file not being found. I know items place in the res folder uses identifiers. But I'm not sure what I should put in the xml source file path to reference the song in res/raw. I printed out the file path to the song by using this code: System.out.println("Raw Path: " + getString(R.raw.menu));
Oh another method I tried, was pushing the entire app to the SDCard. I still received the "cannot find file" error.
Application Permissions:
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Summary
The game works fine on our local webserver with all assets. When trying to create an apk file using Eclipse and Phonegap options produces the game with no audio. I read that the assets folder has a 1 mb limit per file so that is another reason why I am attempting to use res/raw folder. The only issue that is stopping us from publishing this non profit children's game is that I cannot find where the music file is located at within the apk file. I have explored the apk file contents and it shows that song.mp3 is inside res/raw/ folder. However the Android SDK states it not. Does anyone have any ideas?
Thank you four time,
Its my first time posting here so let me know if anything is confusing.
UPDATE 1 I tried an alternate idea by trying to read the song from the SD Card, when I tried this I received a permission denied error (so it did find the file). Even though I have READ_EXTERNAL_STORAGE permissions set, my app is being denied from using the mp3 file. So I tried copying the song from the raw directory, making a new folder on the sd card, and placing the file in the new folder by using this link: How to Copy Raw files into SD Card
When using this solution the folder is never created and the file is never copied either. I also cannot find my app after installing it to my phone. I checked the internal storage->sdcard folder and examine the com folders. I also checked my external storage->sdcard for the com.mycompany folder and its not their either. Our test phone is running Android 4.1.2.
Update 2 I tried the suggestion for the sample project ''. I receive the error Failed to open file 'android.resource://com.mycompany/raw/song.mp3'. (No such file or directory). Song.mp3 is located in the res/raw folder. I tried an alternate idea by trying to read the song from the SD Card, when I tried this I received a permission denied error (so it did find the file). Even though I have READ_EXTERNAL_STORAGE permissions set.
Update 3 I'm working on a blank project again from scratch. The tutorial I used for the previous project is: https://www.youtube.com/watch?v=QRa4yMjoI7c I'll update this post next Monday, thank you Andrew and anyone else that at least viewed this post.
Update 4 I have made a sample project that I have uploaded here: http://tinyurl.com/p7s5w2v Go to file then download. The project is extremely small but you can see that I put the menu.mp3 file in the raw/res directory and that 'android.resource://com.mycompany/raw/menu.mp3' is unable to find the song when called in 'fisher.xml'. The song plays correctly on our webserver and our local server. But menu.mp3 cannot be found when running on an android device or emulator. I can always tell the xml file to load all the audio files for the game from our webserver but this will be slow.
Update 5 Current Tries:
source href="android.resource://com.brg.example/raw/menu" file not found
source href="sdcard/menu.mp3" permission denied on sdcard (app has read access)
source href="storage/sdcard0/menu.mp3" permission denied
source href="file:///assets/music/menu.mp3" file not found
Manifest File:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.brg.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.brg.example.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Update 6 I have decided to settle on loading the media files from a remote server using a url. I initially thought converting a html project to an android webview with audio would be less time consuming than other platforms. I was mistaken with this. We may revisit Android again in the future. PhoneGap support team has been basically silent after we paid for their service. I thanked everyone that decided to glance at this posting.
回答1:
Try this: "android.resource://com.your.package/raw/filename"
回答2:
I've just had a similar problem -- for me the key part was omitting the extension of the media file: android.resource://com.mycompany/raw/song
来源:https://stackoverflow.com/questions/23399855/xml-file-in-assets-folder-cannot-access-res-raw-folder