0

我有一个 Mp3Link 需要在我的设备上以单独的线程播放它

我试过这个,但是当我执行我的代码时,我无法播放音乐,任何人都可以看看我的代码,出了什么问题?

这是我的代码:

Thread trd = new Thread(new Runnable(){
    public void run(){
        //code to do the HTTP request
        MediaPlayer mediaPlayer = new MediaPlayer();  
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
        try {  
            mediaPlayer.setDataSource(mp3Link);  
        } catch (IllegalArgumentException e) {  
            e.printStackTrace();  
        } catch (SecurityException e) {  
            e.printStackTrace();  
        } catch (IllegalStateException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        mediaPlayer.prepareAsync();  
        // You can show progress dialog here untill it prepared to play  
        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {  
            public void onPrepared(MediaPlayer mp) {  
                // Now dismis progress dialog, Media palyer will start playing 
                Log.d("Mediaplyer>>>>>>>>", "Mediaplyer>>>>>>>>");
                mp.start();  
            }  
        });  
        mediaPlayer.setOnErrorListener(new OnErrorListener() {  
            public boolean onError(MediaPlayer mp, int what, int extra) {  
                // dissmiss progress bar here. It will come here when  
                // MediaPlayer  
                // is not able to play file. You can show error message to user  
                return false;  
            }  
        });  

    }
});
trd.start();
4

1 回答 1

1

怎么放

mediaPlayer.prepareAsync();

run()功能结束时?

Thread trd = new Thread(new Runnable(){
    public void run(){
        //code to do the HTTP request
        MediaPlayer mediaPlayer = new MediaPlayer();  
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
        try {  
            mediaPlayer.setDataSource(mp3Link);  
        } catch (IllegalArgumentException e) {  
            e.printStackTrace();  
        } catch (SecurityException e) {  
            e.printStackTrace();  
        } catch (IllegalStateException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        // mediaPlayer.prepareAsync();  // <== Marked
        // You can show progress dialog here untill it prepared to play  
        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {  
            public void onPrepared(MediaPlayer mp) {  
                // Now dismis progress dialog, Media palyer will start playing 
                Log.d("Mediaplyer>>>>>>>>", "Mediaplyer>>>>>>>>");
                mp.start();  
            }  
        });  
        mediaPlayer.setOnErrorListener(new OnErrorListener() {  
            public boolean onError(MediaPlayer mp, int what, int extra) {  
                // dissmiss progress bar here. It will come here when  
                // MediaPlayer  
                // is not able to play file. You can show error message to user  
                return false;  
            }  
        });  
        mediaPlayer.prepareAsync(); // <== Add

    }
});
trd.start();

希望这可以帮助。

于 2013-05-15T05:35:53.993 回答