对于我正在创建的应用程序,它不允许我指定如何处理按钮,而是给我错误。该应用程序有助于在三首音乐歌曲之间切换。
代码不正确
package com.example.atvan.babysleeper;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import com.example.atvan.babysleep.R;
public class MainActivity extends AppCompatActivity {
Button playBtn;
Button one;
Button two;
Button three;
SeekBar positionBar;
SeekBar volumeBar;
TextView elapsedTimeLabel;
TextView remainingTimelabel;
MediaPlayer mp;
int totalTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn = (Button) findViewById(R.id.playBtn);
elapsedTimeLabel = (TextView) findViewById(R.id.elapsedTimeLabel);
remainingTimelabel = (TextView) findViewById(R.id.remainingTimeLabel);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.three);
//Media player
public void setOne(View view){
mp = MediaPlayer.create(this, R.raw.music3);
}
public void setTwo(View view){
mp = MediaPlayer.create(this, R.raw.music2);
}
public void setThree(View view){
mp = MediaPlayer.create(this, R.raw.music1);
}
mp.setLooping(true);
mp.seekTo(0);
mp.setVolume(0.5f,0.5f);
totalTime = mp.getDuration();
//position Bar
positionBar = (SeekBar) findViewById(R.id.positionBar);
positionBar.setMax(totalTime);
positionBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mp.seekTo(progress);
positionBar.setProgress(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
//Volume Bar
volumeBar = (SeekBar) findViewById(R.id.volumeBar);
volumeBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float volumeNum = progress/100f;
mp.setVolume(volumeNum, volumeNum);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
//thread (update positionBar amd timeLabel)
new Thread(new Runnable() {
@Override
public void run() {
while (mp != null) {
try{
Message msg = new Message();
msg.what = mp.getCurrentPosition();
handler.sendMessage(msg);
Thread.sleep(1000);
}catch (InterruptedException e){}
}
}
}).start();
}
private Handler handler;
{
handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
int currentPosition = msg.what;
// Update positionBar.
positionBar.setProgress(currentPosition);
// Update Labels.
String elapsedTime = createTimeLabel(currentPosition);
elapsedTimeLabel.setText(elapsedTime);
String remainingTime = "- " + createTimeLabel(totalTime - currentPosition);
remainingTimelabel.setText(remainingTime);
return true;
}
});
}
public String createTimeLabel(int time) {
String timeLabel = "";
int min = time / 1000 / 60;
int sec = time /1000 % 60;
timeLabel = min + ":";
if (sec<10) timeLabel += "0";
timeLabel += sec;
return timeLabel;
}
public void playBtnClick(View view) {
if (!mp.isPlaying()){
//stopping
mp.start();
playBtn.setBackgroundResource(R.drawable.stop);
} else {
//playing
mp.pause();
playBtn.setBackgroundResource(R.drawable.play);
}
}
它给出的错误是我的媒体播放器部分中的表达式开始非法。它也表示给予;那里的某个地方。当放置在代码末尾时。它没有给出任何错误,但该应用程序无法运行
错误在这里
答案 0 :(得分:0)
您不能只在函数中间定义函数。它们必须在onCreate之外的类范围内。
答案 1 :(得分:0)
您在onCreate()中定义了所有3种方法。将它们放在onCreate之外,然后从任何地方调用它们。如果默认情况下需要setOne(),则只需在onCreate()中调用setOne()。
//媒体播放器
public void setOne(View view){
mp = MediaPlayer.create(this, R.raw.music3);
}
public void setTwo(View view){
mp = MediaPlayer.create(this, R.raw.music2);
}
public void setThree(View view){
mp = MediaPlayer.create(this, R.raw.music1);
}
类似
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn = (Button) findViewById(R.id.playBtn);
elapsedTimeLabel = (TextView) findViewById(R.id.elapsedTimeLabel);
remainingTimelabel = (TextView) findViewById(R.id.remainingTimeLabel);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.three);
//call setOne or setTwo or setThree based on what you want here.
答案 2 :(得分:0)
这里很少有逻辑错误。首先,您不能在另一个方法内编写方法,因此请从onCreate()
中删除以下代码行,并将其写在外面:
public void setOne(View view){ mp = MediaPlayer.create(this, R.raw.music3); }
public void setTwo(View view){ mp = MediaPlayer.create(this, R.raw.music2); }
public void setThree(View view){ mp = MediaPlayer.create(this, R.raw.music1); }
第二件事是您正在这些方法中初始化mediaPlayer
,因此您应该首先调用什么
setOne()
中的onCreate()
方法,并在当前播放的歌曲结束后调用其他方法。
请仔细阅读this教程,尽管该教程显示“在服务中构建媒体播放器”,但是您可以在活动中完成它,而对于您来说,则要进行“ 第三步–构建MediaPlayer < / strong>”很重要。