媒体播放器准备显示非法状态例外

时间:2016-01-29 07:47:52

标签: android media-player android-mediaplayer illegalstateexception

我正在尝试从内部存储中播放音频文件。 我使用的代码是..

package com.abhi.firstapp.firstapp;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.net.URI;

public class MainActivity extends AppCompatActivity {

     MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);



    File f= new File("/sdcard/a.mp3");
    if(f.exists())
    {

        Toast toast= Toast.makeText(this, "file exists", Toast.LENGTH_LONG);
        toast.show();
        Log.d("uri","1");
        Uri uri= Uri.fromFile(f);
        Log.d("uri", "2");

        mp= new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        Log.d("uri", "3");

        try {
            mp.setDataSource("/sdcard/a.mp3");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //mp.setDataSource(getBaseContext(), uri);
            Log.d("uri", "4");


        try {
            mp.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("uri", "IOException");
        }

        mp.start();


    }
    else {
        Toast toast1 = Toast.makeText(this, "file does not exist", Toast.LENGTH_LONG);
        toast1.show();
    }

    //MediaPlayer mp= MediaPlayer.create(getBaseContext(), uri);
    //mp.start();

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

通过使用日志,我可以确定此代码一直运行到mp.prepare(mediaplayer prepare)。在此步骤中,它会给出错误Illegal State Exception

 Caused by: java.lang.IllegalStateException
   at android.media.MediaPlayer.prepare(Native Method)

请帮助!

2 个答案:

答案 0 :(得分:8)

您可能想要改变一些事情。

首先:mp.prepare()将阻止您的主要线程,这是禁止的,并且会导致Android将关闭您的应用的异常。为防止这种情况,设计了mp.prepareAsync。请改用该方法并实现onPreparedListener和onErrorListener。

第二:在调用prepare()之前,你应该提供一个数据源。

你可以这样做:

public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...

        MediaPlayer mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

        try { 
            mediaPlayer.setDataSource(streamURL);
        } catch (IOException e) {
            // Error, do something
        }

        mp.prepareAsync();
        ...
    }
        @Override
    public void onPrepared(MediaPlayer player) {
        mediaPlayer.start();
    }

    ...

}

答案 1 :(得分:1)

我有同样的问题。因为MediaPlayer已经准备好,所以可以提出它。当MediaPlayer mp = MediaPlayer.create(getContext(),someUri);创建新的MediaPlayer时,mp会自动准备,所以您不要自己准备。