Android media player bug

时光怂恿深爱的人放手 提交于 2019-12-29 09:07:55

问题


I have the media player playing an mp3 when I load my application. But I had to move this application and now every time I load the application this gives a force close error.

The media player is opened like this:

 final MediaPlayer mp = MediaPlayer.create(Splash.this, R.raw.indra);
                   mp.start();

I know its the media player which causes the error as when I comment the lines above out the application works.

Is there any other ways I can try to load the mp3?

Thanks

Edit:

MediaPlayer mp = new MediaPlayer();    
         AssetFileDescriptor descriptor = contex.getAssets().openFd("indra.mp3");
                mp.setDataSource( descriptor.getFileDescriptor(), 
         descriptor.getStartOffset(), descriptor.getLength() );
                descriptor.close();
         mp.prepare();
                    mp.start();

Edit:

try {
        MediaPlayer mp = new MediaPlayer();    
         AssetFileDescriptor descriptor;

            descriptor = contex.getAssets().openFd("indra.mp3");
                mp.setDataSource( descriptor.getFileDescriptor(), 
         descriptor.getStartOffset(), descriptor.getLength() );
                descriptor.close();
         mp.prepare();
                    mp.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

回答1:


Just put your file in asset folder n apply this code..

Media Player mp = new MediaPlayer();    

 AssetFileDescriptor descriptor = contex.getAssets().openFd(fileName);
        mp.setDataSource( descriptor.getFileDescriptor(), 
 descriptor.getStartOffset(), descriptor.getLength() );
        descriptor.close();
 mp.prepare();
            mp.start();


来源:https://stackoverflow.com/questions/5219722/android-media-player-bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!