我正在为我的 Mediaplayer程序制作用于Android的元数据检索器功能。我一直在尝试为setDataSource
的{{1}}函数获取Uri。但是,当选择了媒体文件时,我从filechooser活动中获得的Uri消失了,并且在运行元数据检索器功能时得到了空对象引用。我尝试在audioFileUri文件位置运行一个while循环,以验证媒体文件Uri在整个程序生命周期中是否存储在内存中。我很困惑为什么它返回一个空对象引用。
到目前为止,我已经尝试过:
将Uri转换为字符串。
更改元检索器源以接受String数据源。
记录audioFileUri的输出以确保它是媒体文件的值Uri地址。
从sdcard而非Google驱动器播放声音。
使用data.getDataString()以字符串形式获取媒体文件位置。
代码如下:
主要活动:
MediaMetadataRetriever(
这是我的元数据检索器类:
public class MainActivity extends AppCompatActivity {
final int REQUEST_OPEN_FILE = 1;
MediaPlayer mediaPlayer = new MediaPlayer();
Uri audioFileUri;
SeekBar seekBar;
ImageView album_art;
TextView album, artist, genre;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = findViewById(R.id.seekbar);
final Button playButton = findViewById(R.id.play);
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
play();
}
});
final Button pauseButton = findViewById(R.id.pause);
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pause();
}
});
final Button stopButton = findViewById(R.id.stop);
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stop();
}
});
seekBar.setVisibility(View.INVISIBLE);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.open_file_action) {
Intent audiofile_chooser_intent;
audiofile_chooser_intent = new Intent();
audiofile_chooser_intent.setAction(Intent.ACTION_GET_CONTENT);
audiofile_chooser_intent.setType("audio/*");
startActivityForResult(Intent.createChooser(audiofile_chooser_intent,
getString(R.string.select_audio_file_title)), REQUEST_OPEN_FILE);
Toast.makeText(this, "File Chooser initiated..", Toast.LENGTH_SHORT).show();
return true;
} else if (id == R.id.second_item) {
quit();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_OPEN_FILE) {
if (resultCode == RESULT_OK) {
audioFileUri = data.getData();
GetMetaData getMetaData = new GetMetaData();
getMetaData.metaRetriver();
}
}
startMediaPlayer();
}
public void startMediaPlayer() {
try {
if (mediaPlayer.isPlaying() || !mediaPlayer.isPlaying()) {
mediaPlayer.reset();
mediaPlayer.setDataSource(this, audioFileUri);
mediaPlayer.prepare();
mediaPlayer.start();
seekBar.setProgress(0);
seekBar.setMax(mediaPlayer.getDuration()); // Set the Maximum range of the
seekBar.setVisibility(View.VISIBLE);
} else {
mediaPlayer.setDataSource(this, audioFileUri);
mediaPlayer.prepare();
mediaPlayer.start();
seekBar.setProgress(0);
seekBar.setMax(mediaPlayer.getDuration()); // Set the Maximum range of the
seekBar.getVisibility();
seekBar.setVisibility(View.VISIBLE);
}
} catch (Exception e) {
e.printStackTrace();
}
int mediaPos = mediaPlayer.getCurrentPosition();
int mediaMax = mediaPlayer.getDuration();
seekBar.setMax(mediaMax); // Set the Maximum range of the
seekBar.setProgress(mediaPos);// set current progress to song's
handler.removeCallbacks(moveSeekBarThread);
handler.postDelayed(moveSeekBarThread, 300);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
seekBar.setProgress(progress);
mediaPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void quit() {
mediaPlayer.stop();
mediaPlayer.release();
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
System.exit(0);
}
public void play() {
if (!mediaPlayer.isPlaying() || mediaPlayer == null) {
mediaPlayer.start();
} else {
Log.i("DEBUG_TAG", "No File loaded");
}
}
public void pause() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
return;
}
}
public void stop() {
if (mediaPlayer != null || mediaPlayer.isPlaying() || mediaPlayer == null) {
seekBar.setProgress(0);
mediaPlayer.pause();
mediaPlayer.seekTo(0);
}
}
/*public void onDestroy() {
super.onDestroy();
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
}*/
private Runnable moveSeekBarThread = new Runnable() {
public void run() {
if (mediaPlayer.isPlaying()) {
int mediaPos_new = mediaPlayer.getCurrentPosition();
int mediaMax_new = mediaPlayer.getDuration();
seekBar.setMax(mediaMax_new);
seekBar.setProgress(mediaPos_new);
handler.postDelayed(this, 300); //Looping the thread after 0.1 second
// seconds
}
}
};
}
调试:
public class GetMetaData extends MainActivity {
ImageView album_art;
TextView album, artist, genre;
MediaMetadataRetriever metaRetriever;
byte[] art;
Uri audioMeta = audioFileUri;
public void metaRetriver() {
/*super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);*/
getInit(); // Ablum_art retrieval code
metaRetriever = new MediaMetadataRetriever();
if (audioMeta == null) {
return;
} else {
metaRetriever.setDataSource(this, audioMeta);
}
try {
art = metaRetriever.getEmbeddedPicture();
Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length);
album_art.setImageBitmap(songImage);
album.setText(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
artist.setText(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
genre.setText(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE));
} catch (Exception e) {
album_art.setBackgroundColor(Color.GRAY);
album.setText("Unknown Album");
artist.setText("Unknown Artist");
genre.setText("Unknown Genre");
}
}
public void getInit() {
album_art = (ImageView) findViewById(R.id.album_art);
album = (TextView) findViewById(R.id.Album);
artist = (TextView) findViewById(R.id.artist_name);
genre = (TextView) findViewById(R.id.genre);
}