【问题标题】:Android MediaPlayer stops palyng form assetst after changing image on button from assets从资产更改按钮上的图像后,Android MediaPlayer 停止为资产付费
【发布时间】:2017-01-20 12:32:41
【问题描述】:

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

W/MediaPlayer-JNI:MediaPlayer 在没有发布的情况下完成

@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);

【问题讨论】:

    标签: android audio mp3 assets


    【解决方案1】:

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

    【讨论】:

    • 在另一个线程中开始音乐有帮助:) 谢谢你的建议Thread thread = new Thread() { @Override public void run() { int p = 0; while (p != 1) { mpMusic.start(); } } }; thread.start();
    【解决方案2】:

    永远不要像这样创建MediaPlayer MediaPlayer mpMusic = new MediaPlayer();

    使用

            MediaPlayer.create(context,R.raw.music);
    

    【讨论】:

    • 我想使用 assets 文件夹。这是我可以找到使用资产文件夹的唯一方法
    • @RobertRupa 您是否尝试在另一个线程中加载Bitmap?也许AsyncTask
    【解决方案3】:

    尝试关闭描述符

    mpMusic.setDataSource(descriptor.getFileDescriptor(), start, end);
    descriptor.close();
    

    【讨论】:

    • 关闭描述符后问题依然存在。
    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    相关资源
    最近更新 更多