简体   繁体   English

在Android中将AsyncTask与Mediaplayer结合使用

[英]using AsyncTask with mediaplayer in Android

i am developing an android application i have a function to play music from online streaming it take a time to make the application lunch up so i need to give the media player task to a thread to make the application lunch quickly and the thread take the media player so when i make this operation the application give me an exception that mediaplayer.prepare() need the main thread so i will post what i try in my code: 我正在开发一个android应用程序,我具有从在线流播放音乐的功能,需要花费一些时间使应用程序准备午餐,因此我需要将媒体播放器任务分配给线程,以使应用程序快速午餐并由该线程占用媒体播放器,所以当我执行此操作时,应用程序给了我一个例外,即mediaplayer.prepare()需要主线程,因此我将在代码中发布我尝试的内容:

public void playsong()
{
            mediaplayer mp =new mediaplayer();
            String currentUrl = sora.getUrl(this);  
            mp.reset();
            mp.setDataSource(currentUrl);
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setOnBufferingUpdateListener(this);
            mp.prepare();
            mp.start();

}




public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.player);
        initView();
        thread.start();

         }


Thread thread = new Thread()
{
    @Override
    public void run() {
        try {
          playSong();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
    }
};

For that you have to use AsyncTask... 为此,您必须使用AsyncTask ...

class PalyMusic extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... unused) {


      //your background process


        return (null);
    }

    protected void onPostExecute(Void unused) {

        //here you can call your mp.prepare();
    }

}

and for calling this 并称之为

   new PalyMusic().execute();

Don't forget to add this to AndroidManifest.xml file: 不要忘记将其添加到AndroidManifest.xml文件中:

<uses-permission android:name="android.permission.INTERNET"/>

Easy and proper way use prepareAsync() method rather prepare() 简单正确的方法使用prepareAsync()方法,而不是prepare()

public void playsong()
    {
        try {
            final MediaPlayer mp = new MediaPlayer();
            String currentUrl = sora.getUrl(this);
            mp.reset();
            mp.setDataSource(currentUrl);
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setOnBufferingUpdateListener(this);
            mp.prepareAsync();

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM