无法使用MediaPlayer播放歌曲

时间:2013-08-24 22:25:18

标签: android media-player android-mediaplayer

使用MediaPlayer API播放简单的歌曲(存储在外部SD中)时遇到了一些问题。我真的不知道我做错了什么,因为我没有看到任何其他问题的代码错误和使用开发人员我也没有找到它。

我得到的错误是:

08-25 00:20:49.514: D/MediaPlayer(26764): mPlayerID = 94
08-25 00:20:49.519: E/MediaPlayer(26764): error (1, -2147483648)
08-25 00:20:49.597: E/MediaPlayer(26764): Error (1,-2147483648)

AudioActivity.java

public class AudioActivity extends Activity{

    Audio audioPlayer;

    public AudioActivity(){

        audioPlayer = new Audio();

    }

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        //Set of view where videos and the listView (is there is any) will appear
        setContentView(R.layout.audio_player);      

        Button play = (Button)findViewById(R.id.playButton);

        play.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                String path = Environment.getExternalStorageDirectory() + "/" + "song";
                audioPlayer.loadFile(path);
                audioPlayer.prepare();
                audioPlayer.play();
            }
        });

        Button pauseResume = (Button)findViewById(R.id.pauseButton);

        pauseResume.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                audioPlayer.pause();
            }
        });

        Button stop = (Button)findViewById(R.id.stopButton);

        stop.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                audioPlayer.stop();
            }
        });
    }
}

Audio.class

public class Audio {

    MediaPlayer mediaPlayer;
    String pathSong;

    public Audio(){

        mediaPlayer = new MediaPlayer();

        mediaPlayer.setOnErrorListener(new OnErrorListener() {

            public boolean onError(MediaPlayer arg0, int arg1, int arg2) {

                Log.d("Error", "Arg1: " + arg1 + ".Arg2: " + arg2);
                mediaPlayer.reset();
                return false;
            }

        });
    }

    public void loadFile(String path){

        try {
            mediaPlayer.setDataSource(path);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

    public void prepare(){
        try {
            mediaPlayer.prepareAsync();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void play(){

        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();    
            }
        });
    }

    public void stop(){

        mediaPlayer.stop();
    }

    public void pause(){

        mediaPlayer.pause();
    }

    public int getDuration(){

        return mediaPlayer.getDuration();
    }

    public MediaPlayer getMediaPlayer(){

        return mediaPlayer;
    }


    public TrackInfo[] getTrackInfo(){

        return mediaPlayer.getTrackInfo();
    }
}

有什么想法吗?为什么代码(1,-2147483648)没有出现在任何地方?谢谢!

0 个答案:

没有答案