Android 媒体播放器错误
当我加载应用程序时,我让媒体播放器播放 mp3。但我必须移动这个应用程序,现在每次加载该应用程序时都会出现强制关闭错误。
媒体播放器像这样打开:
final MediaPlayer mp = MediaPlayer.create(Splash.this, R.raw.indra);
mp.start();
我知道它的媒体播放器会导致错误,因为当我注释应用程序上面的行时。
还有其他方法可以尝试加载 mp3吗?
谢谢
编辑:
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();
编辑:
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将您的文件放入资产文件夹中并应用此代码即可。
Just put your file in asset folder n apply this code..