gpt4 book ai didi

android - 如何从 mp3 数组 [Android] 播放音频

转载 作者:搜寻专家 更新时间:2023-11-01 09:20:56 26 4
gpt4 key购买 nike

在一个测试项目上工作,我目前的目标是使用一个 int 来指定在应用程序初始化时播放哪个 mp3 文件,而无需事先知道配乐的名称(可扩展性问题,因为我希望能够添加新的mp3 文件添加到随机化池中,而无需在将来触及代码)。但是,当我运行下面的代码时,出现了如下描述的错误:

error: no suitable method found for create(MainActivity,String) method MediaPlayer.create(Context,Uri) is not applicable (argument mismatch; String cannot be converted to Uri) 方法 MediaPlayer.create(Context,int) 不适用适用(参数不匹配;String 无法转换为 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 文件 Assets 具有以下结构:

enter image description here

所以我的问题是我应该怎么做才能在 mp3 Assets 数组中使用 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,以便可以根据声音 Assets 的索引播放声音。

以下代码将播放索引声音。代码中的注释解释了它是如何工作的。为了进行测试,我刚刚构建了一个简单的布局,其中包含三个在索引 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com