在我的Andr​​oid应用程序的RES /原始文件夹我如何播放MP3? [英] How do I play an mp3 in the res/raw folder of my android app?

查看:136
本文介绍了在我的Andr​​oid应用程序的RES /原始文件夹我如何播放MP3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小(200KB)MP3在我的Andr​​oid应用程序的RES /原始文件夹中。我试图从Eclipse中的模拟器中运行它。它是公认的在R文件的资源,但是当我尝试prepare /开始,我的活动崩溃!有没有别的东西我需要改变,或许在清单?

MediaPlayer的MPLAYER = MediaPlayer.create(FakeCallScreen.this,R.raw.mysoundfile);

尝试 {
MPLAYER prepare()。
mPlayer.start();
}赶上(IOException异常E){
后来//处理这个问题
}

解决方案

在启动IE浏览器的onCreate活动把下面的code。

 公共无效的onCreate(包savedInstanceState){


        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        MediaPlayer的MPLAYER = MediaPlayer.create(FakeCallScreen.this,R.raw.mysoundfile);
        mPlayer.start();

    }
 

当停止IE浏览器的onDestroy活动把下面的code。

 公共无效的onDestroy(){

    mPlayer.stop();
    super.onDestroy();

}
 

希望它可以帮助:)

I have a small (200kb) mp3 in the res/raw folder of my android app. I am trying to run it in an emulator from Eclipse. It is recognized as a resource in the R file but when I try to prepare/start, my activity crashes! Was there something else I needed to change, perhaps in the manifest?

MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);

try {
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
// handle this later
}

解决方案

When starting the activity i.e on onCreate put the following code.

  public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);         
        setContentView(R.layout.main);

        MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
        mPlayer.start();

    }

When stopping the activity i.e on onDestroy put the following code.

   public void onDestroy() {

    mPlayer.stop();
    super.onDestroy();

}

Hope it helps :)

这篇关于在我的Andr​​oid应用程序的RES /原始文件夹我如何播放MP3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆