在一个测试项目中,我当前的目标是使用 int 来指定在应用程序初始化时要播放的 mp3 文件,而无需事先知道音轨的名称(可扩展性问题,因为我希望能够添加新的mp3 文件添加到随机化池中,以后无需触及代码)。但是,当我运行以下代码时,出现错误,描述如下:
错误:找不到适合的方法 create(MainActivity,String) 方法 MediaPlayer.create(Context,Uri) 不适用(参数不匹配;字符串无法转换为 Uri)方法 MediaPlayer.create(Context,int) 不适用适用(参数不匹配;字符串无法转换为 int)
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.widget.ArrayAdapter;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView bensonRecycler;
ArrayList<String> arrayList;
ArrayAdapter bensonAdapter;
MediaPlayer bensonPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AssetManager assetManager = getAssets();
String[] audios = assetManager.list("sound");
int i = 1;
final MediaPlayer mp = MediaPlayer.create(this, audios[i]);
mp.start();
}
}
我的 mp3 文件资源具有以下结构:
所以我的问题是我应该怎么做才能在 mp3 资源数组中使用 int i 指定要播放的 mp3 音轨(事先不知道 mp3 音轨的名称)? (我必须使用一个变量来指定在此测试项目中播放的配乐)
最佳答案
“assets”文件夹根本不是真正的文件夹,而是 bundle 的字节流。这就是为什么有 AssetManager .
AssetManager
Provides access to an application's raw asset files; see Resources for the way most applications will want to retrieve their resource data. This class presents a lower-level API that allows you to open and read raw files that have been bundled with the application as a simple stream of bytes.
AssetManager 使我们能够轻松构建文件的字符串数组,并且我们可以将这些文件视为属于小型文件系统。诀窍在于如何将这些文件(实际上是字节流)呈现给 MediaPlayer
,以便可以根据声音资源的索引来播放声音。
以下代码将播放索引声音。代码中的注释解释了它的工作原理。为了进行测试,我刚刚构建了一个简单的布局,其中包含三个按钮,分别在索引 0、1 和 2 处播放声音。
主要 Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void playSoundFromAssets(int index) {
try {
AssetManager assetManager = getAssets();
String[] audios = assetManager.list(SOUNDFILE_PATH);
if (audios == null || index >= audios.length) {
return;
}
String soundFilePath = new File(SOUNDFILE_PATH, audios[index]).getPath();
AssetFileDescriptor afd = getAssets().openFd(soundFilePath);
final MediaPlayer mp = new MediaPlayer();
/* For API 24+, we can just use the AssetFileDescriptor to play the sound. However,
for API 23-, we can't use the AssetFileDescriptor directly but can retrieve a
FileDescriptor from it that points to the beginning of our assets. The offset
and length from the AssetFileDescriptor serve for the FileDescriptor as well.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mp.setDataSource(afd);
} else {
FileDescriptor fd = afd.getFileDescriptor();
Log.d("MainActivity", String.format("<<<< %s %d %d", soundFilePath, afd.getStartOffset(), afd.getLength()));
mp.setDataSource(fd, afd.getStartOffset(), afd.getLength());
// One might think that mp.setDataSource(fd) would play the sound file we want, but
// it actually plays all sound files one after another. It seems that fd is a
// FileDescriptor that points to the beginning of the assets.
}
afd.close();
mp.prepare();
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void playSound(View view) {
switch (view.getId()) {
case R.id.play0:
playSoundFromAssets(0);
break;
case R.id.play1:
playSoundFromAssets(1);
break;
case R.id.play2:
playSoundFromAssets(2);
break;
}
}
private static final String SOUNDFILE_PATH = "sound";
}
关于android - 如何播放mp3数组中的音频[Android],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55551016/