这是我的代码附加媒体播放器但不播放声音
public class MainActivity extends AppCompatActivity {
ImageView choose;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;
private ImageView b1, b2, b3, b4;
private ImageView iv;
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;
Uri uri;
String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
choose = (ImageView) findViewById(R.id.choose);
b1 = (ImageView) findViewById(R.id.button);
b2 = (ImageView) findViewById(R.id.button2);
b3 = (ImageView) findViewById(R.id.button3);
b4 = (ImageView) findViewById(R.id.button4);
iv = (ImageView) findViewById(R.id.imageView);
tx1 = (TextView) findViewById(R.id.textView2);
tx2 = (TextView) findViewById(R.id.textView3);
tx3 = (TextView) findViewById(R.id.textView4);
seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.setClickable(false);
b2.setEnabled(false);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Playing sound", Toast.LENGTH_SHORT).show();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mMediaPlayer.setDataSource(MainActivity.this, Uri.parse(path));
// Toast.makeText(MainActivity.this, mMediaPlayer.setDataSource(getApplicationContext(), contentUri), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mMediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.start();
finalTime = mMediaPlayer.getDuration();
startTime = mMediaPlayer.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);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Pausing sound", Toast.LENGTH_SHORT).show();
mMediaPlayer.pause();
b2.setEnabled(false);
b3.setEnabled(true);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int) startTime;
if ((temp + forwardTime) <= finalTime) {
startTime = startTime + forwardTime;
mMediaPlayer.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();
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int) startTime;
if ((temp - backwardTime) > 0) {
startTime = startTime - backwardTime;
mMediaPlayer.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();
}
}
});
choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent_upload = new Intent();
intent_upload.setType("audio/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent_upload, 1);
}
});
}
private Runnable UpdateSongTime = new Runnable() {
public void run() {
startTime = mMediaPlayer.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) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
uri = data.getData();
Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
path = uri.getPath();
Toast.makeText(MainActivity.this, path, Toast.LENGTH_LONG).show();
// song.setText(returnCursor.getString(nameIndex));
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
此代码选择按钮选择音频文件并选择音频歌曲
传递uri媒体播放器后解析uri附加网址但不播放声音
我看到并使用了许多代码,但没有使用此
请帮帮我 感谢前进
答案 0 :(得分:1)
public class MainActivity extends AppCompatActivity {
private static final int SOUND_LOAD_REQUEST = 1;
ImageView choose;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;
private ImageView b1, b2, b3, b4;
private ImageView iv;
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;
Uri uri;
String path;
String ringtoneTitle;
Uri soundUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
choose = (ImageView) findViewById(R.id.choose);
b1 = (ImageView) findViewById(R.id.button);
b2 = (ImageView) findViewById(R.id.button2);
b3 = (ImageView) findViewById(R.id.button3);
b4 = (ImageView) findViewById(R.id.button4);
iv = (ImageView) findViewById(R.id.imageView);
tx1 = (TextView) findViewById(R.id.textView2);
tx2 = (TextView) findViewById(R.id.textView3);
tx3 = (TextView) findViewById(R.id.textView4);
mMediaPlayer = new MediaPlayer();
seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.setClickable(false);
b2.setEnabled(false);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Pausing sound", Toast.LENGTH_SHORT).show();
mMediaPlayer.pause();
b2.setEnabled(false);
b3.setEnabled(true);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int) startTime;
if ((temp + forwardTime) <= finalTime) {
startTime = startTime + forwardTime;
mMediaPlayer.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();
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int) startTime;
if ((temp - backwardTime) > 0) {
startTime = startTime - backwardTime;
mMediaPlayer.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();
}
}
});
choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent loadIntent = new Intent();
loadIntent.setAction(Intent.ACTION_GET_CONTENT);
loadIntent.setType("audio/*");
startActivityForResult(createChooser(loadIntent, "Select Audio"), SOUND_LOAD_REQUEST);
}
});
}
private Runnable UpdateSongTime = new Runnable() {
public void run() {
startTime = mMediaPlayer.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) {
if (requestCode == SOUND_LOAD_REQUEST)
if (resultCode == RESULT_OK) {
soundUri = data.getData();
try {
mMediaPlayer.setDataSource(this, soundUri);
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMediaPlayer.start();
Toast.makeText(getApplicationContext(), "Playing sound", Toast.LENGTH_SHORT).show();
finalTime = mMediaPlayer.getDuration();
startTime = mMediaPlayer.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);
}
});
}
}
}
此代码终于正常工作