下载音频文件后,如何使用Android MediaPlayer播放?

时间:2012-09-06 11:59:43

标签: android android-mediaplayer

在播放歌曲时(下载后)我在mplayer.setDataSource(fis.getFD());收到例外。为什么我在这里得到例外?

while (isDownloading) 
    {
            try
            {

                String filePath = PATH + sponser;
                FileInputStream fis = new FileInputStream(filePath);
                mplayer.setDataSource(fis.getFD() );
                mplayer.setOnPreparedListener(new OnPreparedListener()
                {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                    }
                });
                mplayer.prepare();
                mplayer.start();

                mplayer.setOnCompletionListener(new OnCompletionListener() 
                {
                    @Override
                    public void onCompletion(MediaPlayer mp) 
                    {
                    }
                });
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                isDownloading = false;
            }
    }

logcat的:

03-07 17:20:14.400: W/System.err(3942): java.lang.NullPointerException
03-07 17:20:14.400: W/System.err(3942):     at com.nelitemusic.MyMixes$CustomAdapter$1.onClick(MyMixes.java:399)
03-07 17:20:14.410: W/System.err(3942):     at android.view.View.performClick(View.java:3127)
03-07 17:20:14.410: W/System.err(3942):     at android.view.View$PerformClick.run(View.java:12025)
03-07 17:20:14.410: W/System.err(3942):     at android.os.Handler.handleCallback(Handler.java:587)
03-07 17:20:14.410: W/System.err(3942):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 17:20:14.420: W/System.err(3942):     at android.os.Looper.loop(Looper.java:132)
03-07 17:20:14.420: W/System.err(3942):     at android.app.ActivityThread.main(ActivityThread.java:4126)
03-07 17:20:14.420: W/System.err(3942):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 17:20:14.420: W/System.err(3942):     at java.lang.reflect.Method.invoke(Method.java:491)
03-07 17:20:14.420: W/System.err(3942):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
03-07 17:20:14.420: W/System.err(3942):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-07 17:20:14.420: W/System.err(3942):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

使用像这样的媒体播放器的数据源方法

 mediaPlayer.setDataSource(fd.getFileDescriptor(),fd.getStartOffset(), fd.getLength()); 

避免出现问题,并确保正确下载视频。

答案 1 :(得分:0)

下载歌曲后,您必须运行Media Scanner。然后你可以播放那首歌。

 //Broadcast the Media Scanner Intent to trigger it
 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                                + Environment.getExternalStorageDirectory())));

MediaScanner扫描Android SD卡以获取新文件。