这里的时候,移动当前的时间位置是java文件:如何从0.00秒打出
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class AudioPlayer extends AppCompatActivity {
ImageView mIvPlayControl;
TextView mTvTotalDuration;
TextView mTvCurrentPosition;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_player2);
mIvPlayControl= (ImageView) findViewById(R.id.iv_play);
mTvTotalDuration= (TextView) findViewById(R.id.tv_total);
mTvCurrentPosition= (TextView) findViewById(R.id.tv_progress);
mp = MediaPlayer.create(AudioPlayer.this, R.raw.sleep_away);
int mCurrentPosition=mp.getCurrentPosition();
int duration=mp.getDuration(); // miliseconds time
//convert into seconds
duration=duration/1000;
mCurrentPosition=mp.getCurrentPosition()/1000;
Log.e("value","value: "+duration);
mTvTotalDuration.setText(String.valueOf(duration));
mTvCurrentPosition.setText(String.valueOf(mCurrentPosition));
mIvPlayControl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp.isPlaying()) {
mp.pause();
mIvPlayControl.setImageResource(R.mipmap.play_small);
} else {
mp.start();
mIvPlayControl.setImageResource(R.mipmap.pause_small);
}
}
});
这里是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:gravity="center"
>
<TextView
android:id="@+id/tv_progress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0.00"
/>
<ImageView
android:id="@+id/iv_play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/play_small"
/>
<TextView
android:id="@+id/tv_total"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="0.00"
/>
</LinearLayout>
</LinearLayout>
如何移动的开始时间0当玩过?我必须添加搜索栏才能获得移动时间吗?有人可以帮忙吗?歌曲可以播放,但定时器不起作用。更多细节或问题在评论中。
你想创建搜索栏和MediaPlayer的之间的“连接”? –
不,我只是想移动它的当前位置,就像我按0.00播放它的停留时间,但歌曲仍在播放。所以我想知道如何从0.00开始改变时间和以后的时间。 –
我想这个答案会帮助你: [http://stackoverflow.com/a/17171025/6862286](http://stackoverflow.com/a/17171025/6862286) –