更改数据源时Android Media Player崩溃

时间:2018-07-27 00:33:56

标签: android android-mediaplayer

我正在使用一项服务在后台播放音乐。该用户具有可以在线收听或下载并离线收听的歌曲列表。 当用户处于脱机模式时,将出现问题。如果他下载一首歌曲,就可以毫无问题地播放它,但是如果他跳到下一首(未下载)并且没有互联网,则它不能按预期工作,这不是问题。当用户跳到上一个已下载的文件时,出现此问题,这一次它没有播放

repo-root
——— src
——— ——— someFunction
——— ——— ——— some-sub-directory
——— ——— ——— ——— targetfile.json

private void initMediaPlayer() {
        int sura_number = activeSuraModel.getSura_number();
        String formatted_sura_number = String.format("%03d", sura_number);  //001

        String path = activeSuraModel.getReciter_server() + "/" + formatted_sura_number + ".mp3";


        mediaPlayer = new MediaPlayer();
        //Set up MediaPlayer event listeners
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnErrorListener(this);
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnSeekCompleteListener(this);
        mediaPlayer.setOnInfoListener(this);
        //Reset so that the MediaPlayer is not pointing to another data source
        mediaPlayer.reset();

        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {

            String downloadedFilePath = downloadedFiles.getDownloadedFilePath(downloaded_files_map, activeSuraModel, reciterName);    

            if (downloadedFilePath == null) {
                mediaPlayer.setDataSource(path); // get online
            } else {
                mediaPlayer.setDataSource(downloadedFilePath);// get from storage
            }

        } catch (Exception e) {
            Log.i("pat", "error  " + e.getMessage());
            stopSelf();
        }
        mediaPlayer.prepareAsync();
    } 

 private void playMedia() {
        if (!mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }
    }

 private void skipToNext() {

        if (suraIndex == suraModels.size() - 1) {
            //if last in playlist
            suraIndex = 0;
            activeSuraModel = suraModels.get(suraIndex);
        } else {
            //get next in playlist
            activeSuraModel = suraModels.get(++suraIndex);
        }

        //Update stored index
        new StorageUtil(getApplicationContext()).storeAudioIndex(suraIndex);

        stopMedia();
        //reset mediaPlayer
        mediaPlayer.reset();
        initMediaPlayer();
    }

    private void skipToPrevious() {

        if (suraIndex == 0) {
            //if first in playlist
            //set index to the last of audioList
            suraIndex = suraModels.size() - 1;
            activeSuraModel = suraModels.get(suraIndex);
        } else {
            //get previous in playlist
            activeSuraModel = suraModels.get(--suraIndex);
        }

        //Update stored index
        new StorageUtil(getApplicationContext()).storeAudioIndex(suraIndex);

        stopMedia();
        //reset mediaPlayer
        mediaPlayer.reset();
        initMediaPlayer();
    }

 @Override
    public void onCompletion(MediaPlayer mp) {

        if (isRepeat) {
            mp.start();
        } else if (isShuffle) {
            Random random = new Random();
            int random_number = random.nextInt(suraModels.size());
            activeSuraModel = suraModels.get(random_number);
            initMediaPlayer();
        } else {// play next song
            skipToNext();
        }


    }

0 个答案:

没有答案