选择一个音乐文件的MediaPlayer播放 [英] Select a music file to play with MediaPlayer

查看:116
本文介绍了选择一个音乐文件的MediaPlayer播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的机器人编程,现在我与我的应用程序有问题。我打算与功能codeA MediaPlayer的应用程序:选择存储使用的意图,然后开始播放该文件的文件。我用MediaPlayer.create(背景下,URI)的方法,但现在我得到的错误

I'm new in android programming and now i get a problem with my app. I intend to code a MediaPlayer application with feature: select a file from storage using intent then start playing that file. I use "MediaPlayer.create(context, uri)" method but currently i got error

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //openFile();
    Button openFile = (Button) this.findViewById(R.id.ButtonOpen);
    openFile.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent chooseFile;
            Intent intent;
            chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
            chooseFile.setType("file/*");
            intent = Intent.createChooser(chooseFile, "Choose a file");
            startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);

            Uri uri = intent.getData();
            initViews(uri);
        }
    }); 

}

private void initViews(Uri uri) {
    mButtonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    mButtonPlayStop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            buttonClick();
        }
    });

    mMediaPlayer = MediaPlayer.create(this, uri);
}


private void starPlayProgressUpdater() {
    mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());

    if (mMediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {

            @Override
            public void run() {
                starPlayProgressUpdater();
            }
        };
        mHandler.postDelayed(notification, 1000);
    } else {
        mMediaPlayer.pause();
        mButtonPlayStop.setText(getString(R.string.play_str));
        mSeekBar.setProgress(0);
    }
}

private void buttonClick() {
    if (mButtonPlayStop.getText() == getString(R.string.play_str)) {
        mButtonPlayStop.setText(getString(R.string.pause_str));
        try {
            mMediaPlayer.start();
            starPlayProgressUpdater();              
        } catch (IllegalStateException e) {
            mMediaPlayer.pause();
        }
    } else {
        mButtonPlayStop.setText(getString(R.string.play_str));
        mMediaPlayer.pause();
    }
}

和它引发NullPointerException异常的mMediaPlayer = MediaPlayer.create(这一点,URI);。它有问题的URI。谁能公会我的方法是什么?

And it throws NullPointerException in "mMediaPlayer = MediaPlayer.create(this, uri);". It has problem with the uri. Could anyone guild me the way?

由于Ilango J,I修订我的code,那么现在我可以选择和播放音乐文件。但我的expecteation是选择文件,然后点击后,只玩上播放/暂停按钮(带进度条),但我得到了新的NullPointerException我buttonClick

Thanks Ilango j, i revised my code then now i could select and play a music file. But my expecteation is select file then only play after click on Play/Pause button (with progress bar) but i got new NullPointerException with my buttonClick

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = (Context) getApplicationContext();

    Button openFile = (Button) this.findViewById(R.id.ButtonOpen);
    openFile.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {       
            initView();
        }
    }); 

}

private void initView() {
    mButtonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    mButtonPlayStop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            buttonClick();
        }
    });

    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 10);

}
    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == RESULT_OK && requestCode == 10) {
        Uri uri = data.getData();

        mMediaPlayer = MediaPlayer.create(mContext, uri);

        mSeekBar = (SeekBar) findViewById(R.id.SeekBar01);
        mSeekBar.setMax(mMediaPlayer.getDuration());
        mSeekBar.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                seekChange(v);
                return false;
            }
        });         
    }
}   

private void starPlayProgressUpdater() {
    mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());

    if (mMediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {

            @Override
            public void run() {
                starPlayProgressUpdater();
            }
        };
        mHandler.postDelayed(notification, 1000);
    } else {
        mMediaPlayer.pause();
        mButtonPlayStop.setText(getString(R.string.play_str));
        mSeekBar.setProgress(0);
    }
}

private void seekChange(View v) {
    if (mMediaPlayer.isPlaying()) {
        SeekBar sb = (SeekBar) v;
        mMediaPlayer.seekTo(sb.getProgress());
    }
}

private void buttonClick() {
    if (mButtonPlayStop.getText().equals(getString(R.string.play_str))) {
        mButtonPlayStop.setText(getString(R.string.pause_str));
        try {
            mMediaPlayer.start();
            starPlayProgressUpdater();              
        } catch (IllegalStateException e) {
            mMediaPlayer.pause();
        }
    } else {
        mButtonPlayStop.setText(getString(R.string.play_str));
        mMediaPlayer.pause();
    }
}

您能给一个建议?

Could you please give an advice?

推荐答案

首先开始的活动,从SD卡的PIC的媒体文件。更换以下code

First start activity to pic media file from sd card. Replace the following code

Button openFile = (Button) this.findViewById(R.id.ButtonOpen);
    openFile.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
           Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 10);
        }
    }); 

那么在添加以下code。在 onActivityResult

Then in add the following code in onActivityResult

@Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {

          if(requestCode == RESULT_OK && requestCode == 10){
                Uri uriSound=data.getData();
                play(this, uriSound); 
          }
      }

和调用下面的方法来创建媒体播放器和播放选定的音频文件。

And call the below method to create media player and play selected audio file.

   private void play(Context context, Uri uri) {

        try {
            MediaPlayer mp = new MediaPlayer();
            mp.setDataSource(context, uri);         
            mp.start();
          } catch (IllegalArgumentException e) {
          // TODO Auto-generated catch block
             e.printStackTrace();
          } catch (SecurityException e) {
          // TODO Auto-generated catch block
             e.printStackTrace();
          } catch (IllegalStateException e) {
          // TODO Auto-generated catch block
             e.printStackTrace();
          } catch (IOException e) {
          // TODO Auto-generated catch block
             e.printStackTrace();
          }
    }

这篇关于选择一个音乐文件的MediaPlayer播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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