gpt4 book ai didi

android - 尝试从远程 MP3 文件 Android 检索元数据时出错

转载 作者:行者123 更新时间:2023-11-30 03:43:19 24 4
gpt4 key购买 nike

我正在为 Android 开发 Radio Streming 应用程序。MediaPlayer 类很好地接收和播放了音乐,但问题是当我尝试检索元数据时,应用程序会阻塞并返回错误。这是代码:

package com.tests.mediaplayer1;
import java.io.IOException;
import android.media.AudioManager;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

private Button btn_prev;
private Button btn_pause;
private Button btn_play;
private Button btn_next;
private TextView txt_song_title;
private SeekBar seekBar_volume;

private MediaPlayer mediaPlayer;
private MediaMetadataRetriever retriever;
private String url = "http://support.k-designed.net/test-z/music/Accordossie.mp3";
private AudioManager audioManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn_prev = (Button) findViewById(R.id.btn_prev);
btn_pause = (Button) findViewById(R.id.btn_pause);
btn_play = (Button) findViewById(R.id.btn_play);
btn_next = (Button) findViewById(R.id.btn_next);
txt_song_title = (TextView) findViewById(R.id.txt_song_title);
seekBar_volume = (SeekBar) findViewById(R.id.seekBar_volume);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

seekBar_volume.setMax(maxVolume);
seekBar_volume.setProgress(curVolume);

seekBar_volume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);

}
});

btn_play.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
try {
mediaPlayer.setDataSource(url);
retriever.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
txt_song_title.setText(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));
} catch(Exception e) {
e.printStackTrace();
}
}
});

btn_pause.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
mediaPlayer.pause();

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

这是我得到的错误:

 java.lang.IllegalStateException
at android.media.MediaPlayer.setDataSource(Native Method)
at com.tests.mediaplayer1.MainActivity$2.onClick(MainActivity.java:84)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

从错误码中,我们可以观察到报错如下

java.lang.IllegalStateException
at android.media.MediaPlayer.setDataSource(Native Method)

如果您引用 MediaPlayernative 实现,当 MediaPlayer 对象为 null 时,将抛出 IllegalStateException

根据您的来源,如果我们检查 retriever,我们可以观察到您已经声明了这个变量,但还没有创建对象。在您的源代码中,请在创建 mediaPlayer 对象之前尝试以下代码。

retreiver = new MediaMetadataRetriever();

通过此更改,将创建 retriever 对象,并且您对 setDataSource 的调用即 retriever.setDataSource(url); 将成功。

关于android - 尝试从远程 MP3 文件 Android 检索元数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15413038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com