MediaPlayer的。打M4A文件时prepare被抛出IllegalStateException [英] MediaPlayer.prepare is throwing an IllegalStateException when playing m4a file

查看:1361
本文介绍了MediaPlayer的。打M4A文件时prepare被抛出IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用在MediaPlayer流的歌曲列表。某些歌曲的工作始终和其他人始终不工作。我不能看到这些文件之间的差异,他们似乎发挥在iTunes和罚款等。

I have a list of songs that I'm streaming using the MediaPlayer. Some of the songs consistently work and others consistently do not work. I can't see a difference between these files, and they seem to play fine in itunes and such.

当歌失败是在媒体播放器。prepare()行抛出IllegalStateException。引发的IllegalStateException异常中没有任何有用的信息,(detailMessage为null,stackState为null)

When the songs fail it is throwing an IllegalStateException on the mediaPlayer.prepare() line. The IllegalStateException that is thrown has no useful info in it, (detailMessage is null, stackState is null)

下面是我的code

try {
    mediaPlayer.setDataSource(media.url);
    setPlayerState(PlayerState.PREPARING);
    mediaPlayer.prepare();
} catch (Exception e) {
    e.printStackTrace();
    Log.e(TAG, "bad stream");
}

下面是一个URL来不工作的文件:
skdy.bryceb.dev.mediarain.com/song.m4a

Here is a url to the file that does NOT work: skdy.bryceb.dev.mediarain.com/song.m4a

下面是一个不工作:
skdy.bryceb.dev.mediarain.com/song2.m4a

Here is one that DOES work: skdy.bryceb.dev.mediarain.com/song2.m4a

任何想法,为什么这个工程上的一些歌曲和失败别人?

Any ideas why this works on some songs and fails on others?

推荐答案

感谢MisterSquonk我相信这种方式是可行的。

Thanks MisterSquonk I'm sure that way would work.

在打我的头撞在墙上了一会儿,我意识到,在一些歌曲,我得到的缓冲额前的球员状态已越来越设置为prepared后,我的具体情况。所以我增加了一个检查,以确保MediaPlayer的是在prePARED状态,然后它的伟大工作:

In my particular case after beating my head against the wall for a while I realized that on some songs, I was getting to the buffered amount before the player state was getting set to prepared. So I added a check to make sure that the MediaPlayer was in the "PREPARED" state and then it worked great:

// Media prepared listener
    mediaPlayer.setOnPreparedListener(
            new MediaPlayer.OnPreparedListener() {
                public void onPrepared(MediaPlayer mp) {
                    setPlayerState(PlayerState.PREPARED);
                }
            });

    // Media buffer listener
    mediaPlayer.setOnBufferingUpdateListener(
            new MediaPlayer.OnBufferingUpdateListener() {
                public void onBufferingUpdate(MediaPlayer mp, int percent) {

                    // Sometimes the song will finish playing before the 100% loaded in has been
                    // dispatched, which result in the song playing again, so check to see if the 
                    // song has completed first
                    if(getPlayerState() == PlayerState.COMPLETED)
                        return;

                    if(getPlayerState() == PlayerState.PAUSED)
                        return;

                    // If the music isn't already playing, and the buffer has been reached
                    if(!mediaPlayer.isPlaying() && percent > PERCENT_BUFFER) {
                        if(getPlayerState() == PlayerState.PREPARED)
                        {
                            mediaPlayer.start();
                            setPlayerState(PlayerState.PLAYING);
                        }
                        //if it isn't prepared, then we'll wait till the next buffering
                        //update
                        return;
                    }
                }
            });

这篇关于MediaPlayer的。打M4A文件时prepare被抛出IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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