问题描述
我想在带有libgdx库的android应用中加载mp3歌曲,但我找不到工作的方法.我不知道如何编程这个问题".
I want load mp3 song in an android app with libgdx library but i don't find the way to works. I don't know how programm this "issue".
资产管理器可以成为可播放mp3歌曲的课程吗?
Assets Manager could be the class for works with mp3 song?
我也是这样发现的:
Gdx.files.newMusic(file);
但是在Android系统中无法正常工作,而在台式机上,相同的代码可以工作.
but in Android don´t work and desktop the same code work.
更新:解析方法
public void parse() {
JsonReader reader = new JsonReader();
JsonValue rootElem = reader.parse(file);
JsonValue songDataElem = rootElem.get("songData");
JsonValue notesDataElem = songDataElem.get("notes");
JsonValue barsDataElem = songDataElem.get("bars");
JsonValue keysDataElem = songDataElem.get("keys");
JsonValue audioDataElem = rootElem.get("audioData");
NoteData[] notes = new NoteData[notesDataElem.size];
for (int i = 0; i < notesDataElem.size; i++) {
notes[i] = new NoteData(notesDataElem.get(i).getInt("pitch"), notesDataElem.get(i).getFloat("time"));
}
BarData[] bars = new BarData[barsDataElem.size];
for (int i = 0; i < barsDataElem.size; i++) {
BarData bar = new BarData(barsDataElem.get(i).getFloat("time"));
bars[i] = bar;
}
char[] keys = new char[keysDataElem.size];
for (int i = 0; i < keysDataElem.size; i++) {
keys[i] = keysDataElem.getChar(i);
}
float tempo = songDataElem.getFloat("tempo");
String file = audioDataElem.getString("file");
songData = new SongData(notes, bars, keys, tempo);
parsed = true;
}
和构造函数:
public SongFile(FileHandle file) {
manager = new AssetManager(new ExternalFileHandleResolver());
file = Gdx.files.external(file.path());//30
if (file.exists()) {
manager.load(file.path(), Music.class);
manager.finishLoading();
music = manager.get(file.path(), Music.class);
music.setLooping(true);
music.play();
}
}
在主班上:
String file = "/storage/emulated/0/download/prueba.mp3";
SongFile songFile = new SongFile(new FileHandle(file));
songFile.parse();
song = songFile.makeSong();
推荐答案
要加载音乐实例,我们可以执行以下操作:
To load a Music instance we can do the following:
Music music = Gdx.audio.newMusic(Gdx.files.internal("data/mymusic.mp3"));
您还可以使用 AssetManager 加载您的Music
这样您就可以以正确的方式管理您的资产.
You can also use AssetManager to load your Music
so that you can manage your assets in proper way.
AssetManager manager = new AssetManager();
manager.load("data/mymusic.mp3", Music.class);
manager.finishLoading();
成功加载资产后,您可以获取音乐.
You can get your Music after assets have been successfully loaded.
Music music = manager.get("data/mymusic.mp3", Music.class);
各种播放可用于播放音乐的属性
music.play();
检查此线程如果您在Android上遇到一些特殊问题.有时候Sound
可能无法在Android设备上播放,但可以在台式机上播放.
Check this thread if you've some particular problem on Android. Some times Sound
may not play on Android devices but on desktop, it does play.
编辑
将此权限添加到AndroidMainfest.xml文件.
Add this permission to AndroidMainfest.xml file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
对于Android来说,如果在进行与文件IO相关的任何工作之前未获得用户的允许,则targetSdkVersion小于23.对于当前的targetSdkVersion,请检查您的android build.gradle
文件的defaultConfig,如果没有显示其检查AndroidManifest.xml
文件.
Make it clear for Android your targetSdkVersion is less than 23 if not take permission at run time by user before proceeding any work related to your File IO. For current targetSdkVersion check your defaultConfig of android build.gradle
file, if not present their check AndroidManifest.xml
file.
外部目的地是我们保留自己的数据(如视频,音乐和所有数据)的地址.
External Destination is address where we keep our own data like video, music and all.
Gdx.files.getExternalStoragePath()在Android中提供路径/storage/emulated/0/
,在桌面上提供用户目录,例如C:\Users\Abhishek Aryan\
Gdx.files.getExternalStoragePath() give path /storage/emulated/0/
in Android and User Directory on Desktop like this C:\Users\Abhishek Aryan\
/storage/emulated/0/
表示内置存储,下载位于内置存储中.
/storage/emulated/0/
represent inbuilt Storage and Download is inside inbuilt storage.
if(Gdx.app.type==Application.ApplicationType.Android) {
var assetManager = AssetManager(ExternalFileHandleResolver())
var fileHandle = Gdx.files.external("/Download/WorldmapTheme.mp3")
if (fileHandle.exists()) {
assetManager.load(fileHandle.path(), Music::class.java)
assetManager.finishLoading();
var music = assetManager.get<Music>(fileHandle.path())
music.setLooping(true)
music.play()
}
}
编辑2
此代码对我来说很好,希望对您有用
This code working fine for me, Hopefully this will work for you
//ApplicationListener
if(Gdx.app.getType()== Application.ApplicationType.Android) {
String file = "/download/prueba.mp3";
FileHandle fileHandle = Gdx.files.external(file);
SongFile songFile = new SongFile(fileHandle);
songFile.parse();
song = songFile.makeSong();
}
SongFile
类的构造函数
public class SongFile {
AssetManager manager;
Music music;
public SongFile(FileHandle file){
manager = new AssetManager(new ExternalFileHandleResolver());
if (file.exists()) {
manager.load(file.path(), Music.class);
manager.finishLoading();
music = manager.get(file.path(), Music.class);
music.setLooping(true);
music.play();
}
}
...
}
这篇关于如何使用libGDX在Android中加载音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!