Я делаю приложение Musi c Player. Я испытываю некоторые проблемы в отношении Seekbar. Seekbar показывает 2 превью. Один движется вместе с песней, а другой движется туда, куда мы нажимаем на панели. Я хочу иметь один большой палец, который перемещается вместе с песней, и когда щелкает панель, большой палец перемещается в эту позицию, и песня переходит от желаемой (нажатой) позиции.
Изображение музыки c игрок:
Код: MainActivity. java
'' '
package com.example.music_app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;
public class MainActivity extends Activity {
private Button b1, b2, b3, b4,btnopen;
private ImageView iv;
private MediaPlayer mediaPlayer;
private double startTime = 0;
private double finalTime = 0;
private Handler myHandler = new Handler();
private int forwardTime = 5000;
private int backwardTime = 5000;
private SeekBar seekbar;
private TextView tx1, tx2, tx3;
public static int oneTimeOnly = 0;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
btnopen = (Button)findViewById(R.id.button_open);
//volume buttons
Button upButton = (Button) findViewById(R.id.upButton);
Button downButton = (Button) findViewById(R.id.downButton);
final AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
iv = (ImageView) findViewById(R.id.imageView);
tx1 = (TextView) findViewById(R.id.textView2);
tx2 = (TextView) findViewById(R.id.textView3);
tx3 = (TextView) findViewById(R.id.textView4);
tx3.setText("Select a song");
mediaPlayer = MediaPlayer.create(this, R.raw.song);
seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.setClickable(false);
seekbar.setSaveEnabled(true);
b2.setEnabled(false);
b3.setEnabled(false);
//open file button
btnopen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, 4);
}
});
//play button
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Playing sound", Toast.LENGTH_SHORT).show();
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
if (oneTimeOnly == 0) {
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
}
tx2.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) finalTime)))
);
tx1.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) startTime)))
);
seekbar.setProgress((int) startTime);
myHandler.postDelayed( UpdateSongTime, 100);
b2.setEnabled(true);
b3.setEnabled(false);
}
});
//pause button
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Pausing sound", Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
b2.setEnabled(false);
b3.setEnabled(true);
}
});
//forward button
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int) startTime;
if ((temp + forwardTime) <= finalTime) {
startTime = startTime + forwardTime;
mediaPlayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(), "You have Jumped forward 5 seconds", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Cannot jump forward 5 seconds", Toast.LENGTH_SHORT).show();
}
}
});
//backward button
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int) startTime;
if ((temp - backwardTime) > 0) {
startTime = startTime - backwardTime;
mediaPlayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(), "You have Jumped backward 5 seconds", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Cannot jump backward 5 seconds", Toast.LENGTH_SHORT).show();
}
}
});
//handle volume
upButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//To increase media player volume
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
}
});
downButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//To decrease media player volume
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
}
});
//when seekbar is moved
/*
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (mediaPlayer != null && mediaPlayer.isPlaying() ) {
mediaPlayer.pause();
mediaPlayer.seekTo(seekBar.getProgress());
Toast.makeText(MainActivity.this,"Progress value(onStart):" + Integer.toString( seekBar.getProgress() ),Toast.LENGTH_SHORT).show();
mediaPlayer.start();
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
*/
}
private Runnable UpdateSongTime = new Runnable() {
public void run() {
startTime = mediaPlayer.getCurrentPosition();
tx1.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekbar.setProgress((int) startTime);
myHandler.postDelayed(this, 100);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
try {
Uri returnUri = data.getData();
switch (requestCode) {
case 4: //open file
if (resultCode == RESULT_OK) {
String PathHolder = returnUri.getPath();
if (PathHolder.contains("audio")) {
Cursor cursor = null;
String displayName = null;
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
byte[] rawArt;
Bitmap art;
BitmapFactory.Options bfo = new BitmapFactory.Options();
mmr.setDataSource(getApplicationContext(), returnUri);
rawArt = mmr.getEmbeddedPicture();
if (rawArt != null) {
Toast.makeText(MainActivity.this, rawArt.toString(), Toast.LENGTH_LONG).show();
art = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
iv.setImageBitmap(Bitmap.createScaledBitmap(art, iv.getWidth(), iv.getHeight(), false));
} else {
Toast.makeText(MainActivity.this, "not-exists-thumbnail", Toast.LENGTH_LONG).show();
iv.setBackgroundResource(R.drawable.music);
}
try {
cursor = getApplicationContext().getContentResolver().query(returnUri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
//Toast.makeText(MainActivity.this, displayName , Toast.LENGTH_LONG).show();
}
} finally {
cursor.close();
}
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
mediaPlayer = MediaPlayer.create(this, returnUri);
//mediaPlayer.start();
String songName = displayName.substring(displayName.lastIndexOf("/") + 1);
if (songName.length() > 10)
songName = songName.substring(0, 10);
tx3.setText(songName);
Toast.makeText(MainActivity.this, "audio file", Toast.LENGTH_LONG).show();
//b2.setEnabled(false);
//b3.setEnabled(true);
b3.performClick();
} else //not-audio file
{
Toast.makeText(MainActivity.this, "Not an audio file", Toast.LENGTH_LONG).show();
}
}
break;
}
}//try
catch (Exception e)
{
Toast.makeText(getApplicationContext(), "Error opening file!", Toast.LENGTH_SHORT).show();
}
}
}
' ''
Макет: Activity_main. xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Music Player"
android:textSize="35dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Media player"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:src="@drawable/music"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/button2"
android:layout_alignTop="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_toEndOf="@+id/button3"
android:text="@string/rewind" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pause"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_toEndOf="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="@string/play" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="@string/forward" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_alignStart="@+id/textview"
android:layout_alignLeft="@+id/textview"
android:layout_alignEnd="@+id/textview"
android:layout_alignRight="@+id/textview" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/seekBar"
android:layout_toStartOf="@+id/textView"
android:layout_toLeftOf="@+id/textView4"
android:text="0 min, 0 sec"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView4"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/seekBar"
android:layout_alignEnd="@+id/button4"
android:layout_alignParentRight="true"
android:text="0 min, 0 sec"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/downButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/textView2"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal|center_vertical"
android:text="-" />
<Button
android:id="@+id/upButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/textView3"
android:layout_alignParentRight="true"
android:layout_marginBottom="4dp"
android:gravity="center_horizontal|center_vertical"
android:text="+" />
<Button
android:id="@+id/button_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/downButton"
android:text="open file ♪♫" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_alignStart="@+id/textview"
android:layout_alignLeft="@+id/textview"
android:layout_alignEnd="@+id/textview"
android:layout_alignRight="@+id/textview" />