如何使用一个媒体播放器播放res文件夹中的歌曲

时间:2013-09-17 06:18:24

标签: android

我在res文件夹中有2首歌曲如何使用mediaplayer

播放它们

我已将这段代码用于2首不同的歌曲,我如何使用一台媒体播放器播放歌曲

公共类Nationalanthem扩展了活动{

int songs[]={R.drawable.inno,R.drawable.vandemataram};
MediaPlayer mp;
ImageButton play,stop,b1,b2,back,exit,b3;
//Button back,exit;
MediaPlayer mediaPlayer,mediaplayer1;
RadioGroup rg;
RadioButton van,jana;
@Override

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.natinal_play);



         mp=new MediaPlayer();
         back=(ImageButton)findViewById(R.id.imageButton6);
         exit=(ImageButton)findViewById(R.id.imageButton5);

         b1=(ImageButton)findViewById(R.id.imageButton3);
         b2=(ImageButton)findViewById(R.id.imageButton2);
         b3=(ImageButton)findViewById(R.id.exitbton);
         rg=(RadioGroup)findViewById(R.id.radioGroup1);
         van=(RadioButton)findViewById(R.id.radiov);
         jana=(RadioButton)findViewById(R.id.Radioja);

         //mediaPlayer = MediaPlayer.create(getApplicationContext(),R.drawable.inno);
         //mediaplayer1 = MediaPlayer.create(getApplicationContext(),R.drawable.vandemataram);



     b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub                                  
                rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        // TODO Auto-generated method stub
                        if (mediaPlayer != null)
                           // mediaPlayer.reset();
                        switch (checkedId) {
                        case R.id.radiov:
                            mediaPlayer = MediaPlayer.create(getApplicationContext(),songs[0]);
                            mediaPlayer.start();
                            break;
                        case R.id.Radioja:
                            mediaPlayer = MediaPlayer.create(getApplicationContext(),songs[1]);
                            mediaPlayer.start();
                            break;
                        default:
                            break;
                        }



                    }
                });

            }
        });

         b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //mediaPlayer.release();
                mediaPlayer.stop();
                mediaPlayer = MediaPlayer.create(getApplicationContext(),R.drawable.inno);
            }
        });

         b3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                mediaPlayer.pause();
                //mediaplayer1.pause();
            }
        });

back.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        //mediaplayer1.stop();

        mediaPlayer.stop();
        mediaPlayer = MediaPlayer.create(getApplicationContext(),R.drawable.inno);
    Intent i2= new Intent(getApplicationContext(),Second.class);
    startActivity(i2);
    //finish();
    }
}); 
exit.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        //mediaplayer1.stop();

        AlertDialog.Builder altert =new AlertDialog.Builder(Nationalanthem.this);
        altert.setTitle("Exit");
        altert.setIcon(R.drawable.ic_launcher);
        altert.setMessage("Are you sure want to exit");
        altert.setPositiveButton("Yes",new DialogInterface.OnClickListener()
        { 
            public void onClick(DialogInterface dialog, int which)
        {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                intent.addCategory(Intent.CATEGORY_HOME);   
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                finish();
                startActivity(intent);
                mediaPlayer.stop();
                mediaPlayer = MediaPlayer.create(getApplicationContext(),R.drawable.inno);
                mediaplayer1.stop();
                mediaplayer1 = MediaPlayer.create(getApplicationContext(),R.drawable.vandemataram);     

        }

        });

        altert.setNegativeButton("No",new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                //Toast.makeText(getApplicationContext(), "this is delete",Toast.LENGTH_LONG).show();

            }
        });

        altert.show();
            //System.exit(0);
    }


});



}


}

我现在无法听到没有声音的歌曲

3 个答案:

答案 0 :(得分:1)

正如您所见in the documentation,您可以使用reset()将媒体播放器恢复到初始状态。

答案 1 :(得分:1)

您可以将文件放在asset目录中,并使用File.listFiles()方法获取文件列表。然后使用:

for (File file : files) {
    AssetFileDescriptor afd = getAssets().openFd(file.getName());
    player = new MediaPlayer();
    player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
    player.prepare();
    player.start();
    // Do other tasks
}

有关详细信息,请查看this

答案 2 :(得分:0)

试试这段代码

Integer songs[]={R.drawable.inno,R.drawable.rock,R.drawable.hello};

next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mediaPlayer.reset();
            mediaPlayer = MediaPlayer.create(getApplicationContext(),song[index]);
            mediaPlayer.start();

        }
    });

编辑 - 使用RadioGroup

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            if (mediaPlayer != null)
                mediaPlayer.reset();
            switch (checkedId) {
            case R.id.radiov:
                mediaPlayer = MediaPlayer.create(getApplicationContext(),song[0]);
                mediaPlayer.start();
                break;
            case R.id.Radioja:
                mediaPlayer = MediaPlayer.create(getApplicationContext(),song[1]);
                mediaPlayer.start();
                break;
            default:
                break;
            }



        }
    });