首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android按顺序播放声音

Android按顺序播放声音
EN

Stack Overflow用户
提问于 2011-06-22 04:19:58
回答 1查看 2K关注 0票数 0

我正在开发一个消防应用程序,它可以让我从列表视图中选择不同的音调,如果你愿意,可以将它们添加到队列中,然后按照按下播放按钮时选择的顺序播放它们。我看过一些关于使用mediaplayer数组的东西,但是我不确定如何将声音文件或引用in添加到数组中,以便它们可以从索引0中的第一个选定声音开始播放到最后一个索引中的最后一个声音。任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2011-06-22 04:31:26

像这样的东西?

代码语言:javascript
复制
//
import android.media.AudioManager;
import android.media.SoundPool;
import android.app.Activity;
//
import java.util.HashMap;
//
import us.kristjansson.android.R;

public class CxMediaPlayer
{
private SoundPool mShortPlayer= null;
private HashMap mSounds = new HashMap();

// Constructor
public CxMediaPlayer( Activity pContext )
{
// setup Soundpool
this.mShortPlayer = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);

// 0-9 Buttons
mSounds.put( R.raw.button_1, this.mShortPlayer.load(pContext, R.raw.button_1, 1) );
mSounds.put( R.raw.button_2, this.mShortPlayer.load(pContext, R.raw.button_2, 1) );
mSounds.put( R.raw.button_3, this.mShortPlayer.load(pContext, R.raw.button_3, 1) );
mSounds.put( R.raw.button_4, this.mShortPlayer.load(pContext, R.raw.button_4, 1) );
mSounds.put( R.raw.button_5, this.mShortPlayer.load(pContext, R.raw.button_5, 1) );
mSounds.put( R.raw.button_6, this.mShortPlayer.load(pContext, R.raw.button_6, 1) );
mSounds.put( R.raw.button_7, this.mShortPlayer.load(pContext, R.raw.button_7, 1) );

// Others
mSounds.put( R.raw.delete_5, this.mShortPlayer.load(pContext, R.raw.correct_answer, 1) );
mSounds.put( R.raw.delete_5, this.mShortPlayer.load(pContext, R.raw.wrong_answer, 1) );
}

// Plays the passed preloaded resource
public void playShortResource( int piResource )
{
int iSoundId = mSounds.get( piResource );
this.mShortPlayer.play( iSoundId, 0.99f, 0.99f, 0, 0, 1 );
}

// Cleanup
public void Release()
{
// Cleanup
this.mShortPlayer.release();
this.mShortPlayer = null;
}
}

然后,在您的活动中需要做的就是启动player类,并在需要播放声音时调用playShortResource。您的资源应该位于res/raw目录中。

代码语言:javascript
复制
// The media player – OnCreate
mxMediaPlayer = new CxMediaPlayer( this );
// Play the desired sound – OnClick
mxMediaPlayer.playShortResource(  R.raw.button_1 );
// Make sure to release resources when done – OnDestroy
mxMediaPlayer.Release();

在您的示例中,将它们添加到数组中,然后循环播放

代码语言:javascript
复制
toPlay.Add( R.raw.button_1 ) 
toPlay.Add( R.raw.button_3 ) 
toPlay.Add( R.raw.button_7 ); 

Foreach( item in toPlay list ) 
  mxMediaPlayer.playShortResource( item ) 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6431479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档