android - 从媒体资源更改按钮上的图片后,Android MediaPlayer停止播放表单素材资源

标签 android audio mp3 assets

我从 Assets 文件夹播放mp3时遇到问题。当我尝试更改按钮上的图像时,mp3停止播放(并非总是...)
我得到的错误

W/MediaPlayer-JNI: MediaPlayer finalized without being released


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(enableMusic){
        playMusic();
    }
...


private void playMusic() {
    boolean noAudioFile = false;
    Context context = this; // or ActivityNotification.this
    MediaPlayer mpMusic = new MediaPlayer();
    AssetManager mg2 = getResources().getAssets();
    String musicPath = langFolder+audioFolder+"system/Bubble_Bath.mp3";
    try {
        mg2.open(musicPath);

    } catch (IOException ex) {
        noAudioFile = true;
    }

    if(!noAudioFile) {
        AssetFileDescriptor descriptor = null;
        try {
            descriptor = context.getAssets().openFd(musicPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        long start = descriptor.getStartOffset();
        long end = descriptor.getLength();
        try {
            mpMusic.setDataSource(descriptor.getFileDescriptor(), start, end);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else{

    }
    mpMusic.setVolume(5,5);
    try {
        mpMusic.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mpMusic.start();
    mpMusic.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.start();
        }
    });


}


public void nextCard(View view) throws IOException {
    i++;
    if (i==files.length) i=0;
    if(i>-1){
        Button btnNext = (Button)findViewById(R.id.btnNext);
        btnNext.setText("Next");
    }
    Drawable d = Drawable.createFromStream(getAssets().open(categoryPath+"/"+files[i]+".png"), null);
    findViewById(R.id.btnImage).setBackgroundDrawable(d);
    if(files[i].substring(0,1).contains("a") || files[i].substring(0,1).contains("i")){
        startFileSound = langFolder + audioFolder + "system/GAME_thisisan.mp3";
    }
    else if (files[i].substring(files[i].length()-1,files[i].length()).contains("s")){
        startFileSound = langFolder + audioFolder + "system/GAME_theseare.mp3";
    }
    else{
        startFileSound = noAudioFileSound;
    }
}

当我尝试在下面注释行时,mp3播放没有问题。

Drawable d = Drawable.createFromStream(getAssets().open(categoryPath+"/"+files[i]+".png"), null);

最佳答案

您正在从 Assets 文件夹加载可绘制对象,这是正确的。但是不要在主线程中执行。尝试在其他线程中执行此操作。

关于android - 从媒体资源更改按钮上的图片后,Android MediaPlayer停止播放表单素材资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41763788/

相关文章:

Python 将 wav 转换为 mp3

android - 如何将 jpeg 图像转换为 nv21 帧?

android - 在 Busybox 中模拟不良网络连接

audio - 在Android上修改Sound Player

actionscript-3 - 在AS3中播放声音

audio - 相同比特率和相同长度的 MP3 文件大小不同

javascript - 除了 iscroll4 之外,适用于 android 2.3 的滚动条

java - "new"表达式中的花括号? (例如 "new MyClass() { ... }")

firefox - Firefox中的多个音频播放器

php - 合并两个 mp3 php