从目录连续播放音频文件

时间:2011-05-24 10:59:57

标签: android audio

我正在使用以下代码从sdcard中的目录连续播放音频文件。

我面临的问题是所有文件一次播放。我需要一个接一个地玩。

mPlay.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
            getFiles();
            }

            private void getFiles() {
                // TODO Auto-generated method stub
                File dir = new File("/sdcard/sdcard");
                 int index=0;
                 String[] children = dir.list();
                 for(int i=0;i<children.length;i++)
                 {
                     String filename=children[i];
                     playAudio(filename);
                 }
            }

            private void playAudio(String filename) {
                // TODO Auto-generated method stub
                 mPlayer = new MediaPlayer();
                    try {
                        mPlayer.setDataSource(filename);
                    } catch (IllegalArgumentException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (IllegalStateException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    try {
                        mPlayer.prepare();
                    } catch (IllegalStateException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    mPlayer.start();
                    mSeekBar.setMax(mPlayer.getDuration());
                    new Thread(new Runnable(){

@Override
public void run() 
{
// TODO Auto-generated method stub
while(mPlayer!=null && mPlayer.getCurrentPosition() <mPlayer.getDuration())
    {
Log.d("Indide Run Method",Integer.toString(mPlayer.getCurrentPosition()));
    try 
    {
          Thread.sleep(100);
     } 
catch (InterruptedException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
                                mSeekBar.setProgress(mPlayer.getCurrentPosition());
                            }   


                        }

                    }
                    ).start();
                    mSeekBar.setProgress(mPlayer.getCurrentPosition());

        pauseRecord.setVisibility(View.INVISIBLE);  


            }

        });

如何解决问题?

05-24 18:34:19.455: DEBUG/MediaPlayer(19961):     at dalvik.system.NativeStart.main(Native Method)
05-24 18:34:19.465: WARN/dalvikvm(19961): threadid=3: thread exiting with uncaught exception (group=0x2aadda10)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961): Uncaught handler: thread main exiting due to uncaught exception
**05-24 18:34:19.465: ERROR/AndroidRuntime(19961): java.lang.NullPointerException
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at com.fsp.audio.resume.AudioResume.playChild(AudioResume.java:402)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at com.fsp.audio.resume.AudioResume.playlist(AudioResume.java:393)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at com.fsp.audio.resume.AudioResume.access$0(AudioResume.java:391)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at com.fsp.audio.resume.AudioResume$1.onClick(AudioResume.java:132)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.view.View.performClick(View.java:2347)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.view.View.onTouchEvent(View.java:4185)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.widget.TextView.onTouchEvent(TextView.java:6521)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.view.View.dispatchTouchEvent(View.java:3717)**
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1659)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.os.Looper.loop(Looper.java:123)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at android.app.ActivityThread.main(ActivityThread.java:4203)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at java.lang.reflect.Method.invokeNative(Native Method)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at java.lang.reflect.Method.invoke(Method.java:521)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-24 18:34:19.465: ERROR/AndroidRuntime(19961):     at dalvik.system.NativeStart.main(Native Method)
05-24 18:34:21.435: ERROR/dun_service(19989): Unable to open external port /dev/ttyHSUSB0 : No such device
05-24 18:34:26.465: ERROR/dun_service(19991): Unable to open external port /dev/ttyHSUSB0 : No such device
05-24 18:34:31.495: ERROR/dun_service(19993): Unable to open external port /dev/ttyHSUSB0 : No such device
05-24 18:34:36.525: ERROR/dun_service(19995): Unable to open external port /dev/ttyHSUSB0 : No such device

1 个答案:

答案 0 :(得分:0)

为了播放另一个文件,您可以使用MediaPlayer.OnCompletionListener:

final MediaPlayer mp1 = new MediaPlayer();
mp1.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp1.setDataSource(file1);
mp1.prepare();
mp1.start();

mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        mp1.release();
        //file 1 has played, now play the second
    }
});