【问题标题】:Play Sound OnClick Android播放声音 OnClick Android
【发布时间】:2017-02-13 17:01:55
【问题描述】:

我只是想在 android studio 中单击一个按钮时播放一个 mp3 文件。

我的问题是,我使用 2 种方法播放了声音,但由于某种原因,我听到它就像一个极度失真的超级慢动作声音。而如果我正常打开文件就可以了。

!!!!!!方法1:

我声明:

SoundPool mySound;
int soungId;

我初始化:

mySound  = new SoundPool(1, AudioManager.STREAM_MUSIC,0);
soungId = mySound.load(this, R.raw.asd,1);

然后是使用动作监听器:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        mySound.play(soungId,1,1,1,0,1);
    }
});

!!!!!!方法2:

我声明:

MediaPlayer mp = null;

我使用监听器:

button.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View view) {
        managerOfSound();
    }
});

我的 managerOfSound 方法:

public void managerOfSound() {
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, R.raw.www);
mp.reset();
mp.start();
}

【问题讨论】:

    标签: android android-studio audio onclick playsound


    【解决方案1】:

    可能是因为MaxStreams最大数(最大并发流数)的Soundpool实例太低了

    SoundPool mySound;
    mySound  = new SoundPool(10, AudioManager.STREAM_MUSIC,0);
    

    【讨论】:

    • 我只是想流式传输 1,我试过了,不幸的是它没有用
    【解决方案2】:

    试试这个,

    MediaPlayer mp = new MediaPlayer();
    
    // Set data source -
    setDataSource("/sdcard/path_to_song");
    
    // Play audio
    mp.start();
    
    // Pause audio
    mp.pause();
    
    // Reset mediaplayer
    mp.reset();
    
    // Get song length duration - in milliseconds
    mp.getDuration();
    
    // Get current duration - in milliseconds
    mp.getCurrentDuration();
    
    // Move song to particular second - used for Forward or Backward
    mp.seekTo(positon); // position in milliseconds
    
    // Check if song is playing or not
    mp.isPlaying(); // returns true or false
    

    http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/

    【讨论】:

    • 我遵循了它,它仍然像描述的那样运行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多