1

在播放歌曲时(下载后),我在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;
            }
    }

日志猫:

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)
4

2 回答 2

2

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

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

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

于 2012-09-07T09:45:11.777 回答
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 卡中的新文件。

于 2012-09-06T12:02:16.737 回答