Android MediaPlayer准备失败:状态= 0x1,带有流媒体音乐

时间:2015-03-19 13:07:50

标签: android

我正在尝试播放流式音乐背景,为此我正在使用以下音乐服务课程:

public class MusicService extends Service implements
        OnErrorListener, MediaPlayer.OnCompletionListener {

    private static final int NOTIFICATION_ID = 1;
    private final IBinder mBinder = new ServiceBinder();
    MediaPlayer mPlayer;
    private int length = 0;
    private Notification mNotification;

    public MusicService() {
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mPlayer.stop();
    }

    public class ServiceBinder extends Binder {
        public MusicService getService() {
            return MusicService.this;
        }
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mPlayer = new MediaPlayer();
        mPlayer.setOnCompletionListener(this);
        mPlayer.setOnErrorListener(this);

        if (mPlayer != null) {
            mPlayer.setLooping(true);
            mPlayer.setVolume(100, 100);
        }

        mPlayer.setOnErrorListener(new OnErrorListener() {

            public boolean onError(MediaPlayer mp, int what, int extra) {

                onError(mPlayer, what, extra);
                return true;
            }
        });
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // mPlayer.start();
        return START_STICKY;

    }

    public void startMusic(String path) {
        Toast.makeText(this, "pause music", Toast.LENGTH_SHORT).show();
        if (mPlayer.isPlaying()) {
            mPlayer.release();
        }
        try {
            SongController s = (SongController) DBControllerFactory.getController("queue", this);
            Log.d("MusicService", Config.SERVICE_ROOT_URL + "uploads/" + ((Song) s.select()[s.getTotalRows() - 1]).getFile() + ".mp3");
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mPlayer.setDataSource(Config.SERVICE_ROOT_URL + "uploads/" + ((Song) s.select()[s.getTotalRows() - 1]).getFile() + ".mp3");
            mPlayer.prepare();  /////error at here
            mPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void pauseMusic() {
        Toast.makeText(this, "pause music", Toast.LENGTH_SHORT).show();
        if (mPlayer.isPlaying()) {
            mPlayer.pause();
            length = mPlayer.getCurrentPosition();
        }
    }

    public void resumeMusic() {
        Toast.makeText(this, "resume music", Toast.LENGTH_SHORT).show();
        if (mPlayer.isPlaying() == false) {
            mPlayer.seekTo(length);
            mPlayer.start();
        }
    }

    public void stopMusic() {
        //setUpAsForeground("test");
        Toast.makeText(this, "stop music", Toast.LENGTH_SHORT).show();
        mPlayer.stop();
        mPlayer.release();
        mPlayer = null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPlayer != null) {
            try {
                mPlayer.stop();
                mPlayer.release();
            } finally {
                mPlayer = null;
            }
        }

    }

    public boolean onError(MediaPlayer mp, int what, int extra) {

        Toast.makeText(this, "music player failed", Toast.LENGTH_SHORT).show();
        if (mPlayer != null) {
            try {
                mPlayer.stop();
                mPlayer.release();
            } finally {
                mPlayer = null;
            }
        }
        return false;
    }

    void setUpAsForeground(String text) {
        Intent openIntent = new Intent(getApplicationContext(), MainScreenActivity.class);
        openIntent.putExtra("action", "opendrawer");
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
                0,
                openIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mNotification = new Notification();
        mNotification.tickerText = text;
        mNotification.icon = R.drawable.ic_launcher;
        mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
        mNotification.setLatestEventInfo(getApplicationContext(),
                getResources().getString(R.string.app_name), text, pi);
        startForeground(NOTIFICATION_ID, mNotification);
    }
}

播放第一次播放音乐时,它可以正常播放,但第二次出现以下错误:

java.io.IOException: Prepare failed.: status=0x1
 W/System.err﹕ at android.media.MediaPlayer._prepare(Native Method)
 W/System.err﹕ at android.media.MediaPlayer.prepare(MediaPlayer.java:1125)
......

但是,如果我从设备中删除我的应用程序的缓存它再次正常工作。所以我认为这个问题与缓存有关,但我无法确定问题可以帮助我吗????

3 个答案:

答案 0 :(得分:0)

尝试在开始之前重置媒体播放器

if (mpObject.isPlaying())
            {
                mpObject.stop();
                mpObject.reset();
                mpObject.release();
            }

答案 1 :(得分:0)

我认为在try catch块中加载文件时出现问题。 BTW你在哪里使用这些方法: 1.startMusic 2.pauseMusic 3.resumeMusic 4.stopMusic

我认为第二次媒体播放器没有获取源文件,并且在try catch块中的mediaplayer.prepare没有被调用。 \

尝试将要设置源文件的行记录到要播放的媒体播放器中。让我们知道您在log cat中的内容。

我希望它会对你有所帮助。如果您需要帮助,可以自由回复。

答案 2 :(得分:0)

我认为this是您的解决方案。如果对您有所帮助,请告诉我。如果它也不适合你,那么你需要共享SongController类的源代码