音频在android中并不清楚

时间:2014-02-25 11:11:57

标签: android audio media-player audio-streaming

我尝试根据用户选择从原始文件夹播放音频文件,但如果用户选择或更改两个或更多文件,则音频无法正常工作,但在更改选项后,它会流式传输所选文件以及之前选择的文件。我是Android的新手...我已经在下面发布了我的代码,任何人都帮我解决这个问题。

switch (Global.id)
{
case R.id.radioButton1:
    mMediaPlayer = MediaPlayer.create(context, R.raw.kalimba);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);
    mMediaPlayer.start();
    break;

case R.id.radioButton2:
    mMediaPlayer = MediaPlayer.create(context, R.raw.maid);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);
    mMediaPlayer.start();
    break;

case R.id.radioButton3:
    mMediaPlayer = MediaPlayer.create(context, R.raw.sleep_away);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);
    mMediaPlayer.start();
    break;
}

这是一个 onDestroy 方法。

private void destroy()
{
mMediaPlayer.release();
mMediaPlayer.reset();
mMediaPlayer.stop();            
}

3 个答案:

答案 0 :(得分:0)

试试这种方式,

if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
{
     mMediaPlayer.reset();
     mMediaPlayer.stop();

}

在Switch case中添加以上条件

switch (Global.id)
{
case R.id.radioButton1:
    mMediaPlayer = MediaPlayer.create(context, R.raw.kalimba);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);

    if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
    {
        mMediaPlayer.reset();
        mMediaPlayer.stop();
    }
    mMediaPlayer.start();
    break;
case R.id.radioButton2:
    mMediaPlayer = MediaPlayer.create(context, R.raw.maid);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);
    if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
    {
        mMediaPlayer.reset();
        mMediaPlayer.stop();
    }
    mMediaPlayer.start();
    break;
case R.id.radioButton3:
    mMediaPlayer = MediaPlayer.create(context, R.raw.sleep_away);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);
    if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
    {
        mMediaPlayer.reset();
        mMediaPlayer.stop();
    }
    mMediaPlayer.start();
    break;
}

答案 1 :(得分:0)

您需要重置播放器。做这样的事情:

//global
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);

//switch statement somewhere else
switch (Global.id)
{
case R.id.radioButton1:
    otherSong(R.raw.kalimba);
    break;

case R.id.radioButton2:
    otherSong(R.raw.maid);
    break;

case R.id.radioButton3:
    otherSong(R.raw.sleep_away);
    break;
}

//method for changing song
private void otherSong(int song) {
    if(mMediaPlayer.isPlaying()) {          
            mMediaPlayer.stop();
    }
    mMediaPlayer.reset();
    mMediaPlayer.setDataSource(song);
    mMediaPlayer.prepare();
    mMediaPlayer.start();
}

答案 2 :(得分:0)

if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
{
     mMediaPlayer.reset();
   //  mMediaPlayer.stop(); // reset its more than enought here im afraid you migth end up here whith      illegal exception when you call stop right after reset without creating a new media 

}

提示:调用reset()会将媒体播放器置于空闲状态,因此无需调用stop

编辑:所以它应该是那样的

switch (Global.id)
{
case R.id.radioButton1:
      if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
    {
        mMediaPlayer.reset();
       // mMediaPlayer.stop();
    }
    mMediaPlayer = MediaPlayer.create(context, R.raw.kalimba);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);


    mMediaPlayer.start();
    break;
case R.id.radioButton2:
       if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
    {
        mMediaPlayer.reset();
       // mMediaPlayer.stop();
    }
    mMediaPlayer = MediaPlayer.create(context, R.raw.maid);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);

    mMediaPlayer.start();
    break;
case R.id.radioButton3:
    if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
    {
        mMediaPlayer.reset();
       // mMediaPlayer.stop();
    }
    mMediaPlayer = MediaPlayer.create(context, R.raw.sleep_away);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);

    mMediaPlayer.start();
    break;
}

至少在我的经验中没有必要打电话准备我打电话准备后有一些崩溃 编辑:我建议你在创建mMediaPlayer之前检查它是否正在播放。