Using SFML in C++. Trying to play a wav file on a loop, but it only plays the first second or so

时间:2015-05-12 22:43:26

标签: c++ audio sfml

I am trying to run music in a game, but for whatever reason, the file refuses to play entirely. It's a 7 second track, but only the first second or so plays and then I'm given silence. I also programmed this in such a way that it should loop, but it does not do so. Can anyone tell me what I'm doing wrong? Again, the file DOES play, but only the first second and only once.

public class myArrayList extends Activity {
int number, count = 0;
EditText edittext;
ListView listview;
Globals test = (Globals)getApplication();

String[] text = test.getArray();

int textlength = 0;

ArrayList<String> text_sort = new ArrayList<String>();

public void onCreate(Bundle savedInstanceState) {

1 个答案:

答案 0 :(得分:1)

只要 sf :: Music 对象保留在范围内,音乐就会播放。如果在方法中声明 sf :: Music 对象,则在该方法结束时对其进行破坏,音乐也将停止。

给你的sf :: Music实例类范围;它将继续播放,直到课程超出范围或 .stop()/。pause()被调用。

所以而不是:

function MyCtrl($scope) {
    $scope.array= ["40,78","6,27","0,00",null,null,null,null,null,null,null,null,null,"35,87",null,null]
    for(var i=0; i<$scope.array.length; i++) {
      if ($scope.array[i] !== null) {
            $scope.array[i] = parseFloat($scope.array[i].replace(',','.'))
      }
    }
}

尝试:

class MyClass {
public:
    void playMahJam() {
        sf::Music jam; // Will be destructed at end of method

        if (!music.openFromFile("data/sounds/music.wav")){
            std::cout << "Error..." << std::endl;
        }
        else{
            music.setLoop(true);
            music.setVolume(50);
            music.play();
        }
    } // end playMahJam()
};
相关问题