更改数据源以使用现有MediaPlayer进行音频播放? [英] Changing data source for audio playback using existing MediaPlayer?

查看:383
本文介绍了更改数据源以使用现有MediaPlayer进行音频播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用相同的媒体播放器,但更改数据源.这是我想做的事情:

I'm trying to use the same media player but change the data source. Here is what I'm trying to do: 

    private MediaPlayer mMediaPlayer;

    public void pickFile1() {
       initMediaPlayer("myfile1.mp3");
    }

    public void pickFile2() {
       initMediaPlayer("myfile2.mp3");
    }

    private void initMediaPlayer(String mediafile) {
    // Setup media player, but don't start until user clicks button!
    try {
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
        } else {
            mMediaPlayer.reset();   // so can change data source etc.
        }
        mMediaPlayer.setOnErrorListener(this);
        AssetFileDescriptor afd = getAssets().openFd(mediafile); 
        mMediaPlayer.setDataSource(afd.getFileDescriptor());
    }
    catch (IllegalStateException e) {
        Log.d(TAG, "IllegalStateException: " + e.getMessage());
    }
    catch (IOException e) {
        Log.d(TAG, "IOException: " + e.getMessage());
    }
    catch (IllegalArgumentException e) {
        Log.d(TAG, "IllegalArgumentException: " + e.getMessage());
    }
    catch (SecurityException e) {
        Log.d(TAG, "SecurityException: " + e.getMessage());
    }

    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);  // Keep playing when screen goes off!
}

我只想在要更改为新的媒体文件时调用它.不过,它似乎并未成功更改数据源.第一个问题:是否可以通过这种方式进行操作,还是必须释放媒体播放器并为每个新文件创建一个新文件?如果可能的话,为什么我的代码无法正常工作?

I just call this when I want to change to a new mediafile. It doesn't appear to be changing the data source successfully though. First question: is it possible to do it this way, or do I have to release the media player and create a new one for each new file? If it is possible, then why isn't my code working right?

好吧,发布和重新创建媒体播放器也没有做!它只是不断播放同一首歌!!!?这怎么可能呢?新思路-为每个曲目创建一个不同的媒体播放器,那真的是我在这里要做的吗?这可能是Android中的错误吗?

well, releasing and recreating the media player isn't doing it either! It just keeps playing the same song!?!? How is that even possible? New idea -- create a different media player for each track, is that really what I have to do here? Is this a bug in Android perhaps?

推荐答案

好吧,我从来没有得到一个很好的答案.我认为仿真器中可能发生了一些有趣的事情.我所做的对我来说很有用,就是将文件下载到外部SD卡并从那里播放.这会将代码稍作更改:

Well, I never did get a really good answer for this. I think it might be something funny that happens in the emulator. What I have done that is working great for me, is to download the files to the external SD card and play them from there. That changes the code slightly to this:

String path = getExternalFilesDir(null).toString() + "/";
mMediaPlayer.setDataSource(path + mediafile);

其余的保持不变.

这篇关于更改数据源以使用现有MediaPlayer进行音频播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆