1

我正在使用 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(){
????
}
4

1 回答 1

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();
    }
};

我希望它对你有帮助

于 2013-07-01T05:07:19.510 回答