我有一个class
,其中我创建了一个graphview
,其中包含从一首歌曲中获取的单词以及一个mediaplayer
,以便在按下按钮时播放song
。 graph
一次显示1秒,因此如果我coordinates
每100ms
更改0.1。我应该graphview
移动速度与song
一样快。
只有按下按钮时,audio
play
graph
move
position
才应application
,发布后再返回原来的reset
。现在使用我的代码,graphview
正在做得很好。
唯一的问题是,一旦释放按钮后position
,歌曲graph
就会从第一次出现的同一mediaplayer plays audio later
开始。 {1}}开始移动速度更快或button
并且我不确定为什么会这样,以及每次 public class SongPlayer extends AppCompatActivity {
private Viewport graphViewPort;
private Handler handler;
private Runnable runnable;
private GraphView graph;
private DataPoint[] points;
private LineGraphSeries<DataPoint> series;
private int seriesNr;
final static double graphStartingX = -0.8;
final static double graphEndingX = 0.4;
private double orderNr = graphStartingX;
private ImageButton playRecordButton;
private Button backToSongList;
private TextView songTitleTextView;
private List<Pitch> pitchesList;
List<List<Pitch>> wordsList;
ArrayList<Audio> audioList;
private int songIndex;
private MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song_player);
Intent intent = getIntent();
String songTitle = intent.getExtras().getString("TITLE");
songIndex = intent.getExtras().getInt("INDEX");
audioList = intent.getExtras().getParcelableArrayList("AUDIOLIST");
//Get Textview and Button
playRecordButton = (ImageButton) findViewById(R.id.playRecord);
songTitleTextView = (TextView) findViewById(R.id.songTitle);
backToSongList = (Button) findViewById(R.id.backToSongList);
songTitleTextView.setText(audioList.get(songIndex).getTitle());
//Initialize pitches and words
MakePitchesList listOfPithes = new MakePitchesList();
MakeWordsList listOfWords = new MakeWordsList();
pitchesList = listOfPithes.getListOfPitches();
wordsList = listOfWords.getWordsList(pitchesList);
//Initialize graph
graph = (GraphView) findViewById(R.id.graph);
initGraph();
//ViewPort
graphViewPort = graph.getViewport();
//Handler
handler = new Handler();
playRecordButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionevent) {
final int action = motionevent.getAction();
//If button is pressed
if (action == MotionEvent.ACTION_DOWN) {
Log.i("repeatBtn", "MotionEvent.ACTION_DOWN");
//Start playing audio
playMedia(songIndex);
//Start moving graph
drawAndMoveGraph();
//If button is released, stop audio and set everything back to starting position
} else if (action == MotionEvent.ACTION_UP) {
Log.i("repeatBtn", "MotionEvent.ACTION_UP");
handler.removeCallbacks(runnable);
graphViewPort.setMinX(graphStartingX);
graphViewPort.setMaxX(graphEndingX);
graph.invalidate();
orderNr = graphEndingX;
stopAudio();
}//end else
return false;
} //end onTouch
}); //end b my button
backToSongList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SongPlayer.this, MainActivity.class);
startActivity(intent);
}
});
// StrictMode.enableDefaults();
}
//Move the graph by 0.1 every 100ms
private void drawAndMoveGraph(){
runnable = new Runnable() {
public void run() {
System.out.println(orderNr);
graphViewPort.setMinX(orderNr);
graphViewPort.setMaxX(orderNr + 1);
graph.invalidate();
if(pitchesList.size() != orderNr){
orderNr = orderNr + 0.1;
}
handler.postDelayed(this, 100);
}
};
runnable.run();
}
//Initialize graph
private void initGraph(){
for (int i = 0; i < wordsList.size(); i++) {
seriesNr = 0;
points = new DataPoint[wordsList.get(i).size()];
for (Pitch pitch: wordsList.get(i)) {
points[seriesNr] = new DataPoint(pitch.getOccuranceTime(), pitch.getPitch());
seriesNr++;
}
series = new LineGraphSeries<>(points);
series.setThickness(15);
graph.addSeries(series);
}
//VocalTestPoints
PointsGraphSeries<DataPoint> series = new PointsGraphSeries<>(new DataPoint[] {
new DataPoint(0, 50),
new DataPoint(0, 75),
new DataPoint(0, 100),
new DataPoint(0, 125),
new DataPoint(0, 150),
new DataPoint(0, 175),
new DataPoint(0, 200),
new DataPoint(0, 225),
new DataPoint(0, 250),
new DataPoint(0, 275),
});
series.setSize(5);
series.setColor(Color.YELLOW);
graph.addSeries(series);
// set manual X bounds
graph.getViewport().setYAxisBoundsManual(true);
graph.getViewport().setMinY(-50);
graph.getViewport().setMaxY(400);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(graphStartingX);
graph.getViewport().setMaxX(graphEndingX); //mitu korraga näeb
graph.getViewport().setScrollable(true);
}
//Play Audio
private void playMedia(int songIndex){
StorageUtil storage = new StorageUtil(getApplicationContext());
storage.storeAudio(audioList);
storage.storeAudioIndex(songIndex);
mediaPlayer = new MediaPlayer();
//Reset so that the MediaPlayer is not pointing to another data source
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(storage.loadAudio().get(storage.loadAudioIndex()).getData());
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
}
private void stopAudio(){
mediaPlayer.stop();
mediaPlayer.release();
}
}
被按下时我应该怎么做才能同时运行。
threads, runnables and async's
我不知道从哪里开始。我一直在尝试不同的graphview
,但结果总是一样的。
只是为了澄清{{1}}看起来像这样: