在Android处理中使用MP3声音(需要停止)

时间:2013-06-30 03:44:00

标签: android audio mp3 processing

我正在使用Processing 2.0为Android设备开发应用程序,现在我有playMP3();功能但需要使用stopMP3();停止声音。我尝试了一切,但是加载mp3和播放/停止使用处理的最佳方法是什么?,snd.stop();在stopMP3函数内不起作用......

import android.media.*;
import android.content.res.*;

...

MediaPlayer snd;

...

    void setup()
{
MediaPlayer snd = new MediaPlayer();
}

...

void playMP3()
    { 
try {
    AssetManager assets = this.getAssets();
    AssetFileDescriptor fd = assets.openFd("loop1.mp3");
    snd.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
    snd.prepare();
    snd.start();
  snd.setLooping(true);
  }
  catch (IllegalArgumentException e) {
    e.printStackTrace();
  }
  catch (IllegalStateException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }

}

void stopMP3(){
????
}

1 个答案:

答案 0 :(得分:0)

这是媒体播放器的简单示例

private MediaPlayer   m_Player;

//Start Play Function
private void startPlaying() {
    m_Player = new MediaPlayer();
    m_Player.setOnCompletionListener(onComplete);
    try {
        if(m_Player.isPlaying())
            stopPlaying();

        m_Player.setDataSource(m_FileName);
        m_Player.prepare();
        m_Player.start();

    } catch (IOException e) {

    }
}

//Stop Play Function
private void stopPlaying() {
    m_Player.stop();
    m_Player.reset();
    m_Player = null;
}

//and this is onComplete Listener
MediaPlayer.OnCompletionListener onComplete = new OnCompletionListener()
{

    @Override
    public void onCompletion(MediaPlayer mp)
    {
        stopPlaying();
    }
};

我希望它能帮到你