目前,我正在Android Studio上开发简单的媒体音频播放器,并且尝试播放URL中的声音,但遇到错误,请检查以下代码:
public class MainActivity extends AppCompatActivity {
public MediaPlayer mediaPlayer;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
initAudio(getApplicationContext(), "http://ntcdn.stream/audio/teste.ogg");
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 1000);
}
public void initAudio(final Context context, String url) {
if (mediaPlayer == null) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer = MediaPlayer.create(context, Uri.parse(url));
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Falha ao iniciar o áudio", Toast.LENGTH_LONG).show();
killMediaPlayer();
}
}
}
public void killMediaPlayer() {
if (mediaPlayer != null) {
try {
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
我想创建一个循环以始终播放音频,但是我从logcat得到了这个错误:
11-22 03:12:24.051 29582-29582/etes.giuseppe.databringer V/MediaPlayer-JNI: setParameter: key 1400
11-22 03:12:24.051 29582-29582/etes.giuseppe.databringer V/MediaPlayer: MediaPlayer::setParameter(1400)
11-22 03:12:24.051 29582-29582/etes.giuseppe.databringer V/MediaPlayer-JNI: setAudioStreamType: 3
11-22 03:12:24.051 29582-29582/etes.giuseppe.databringer V/MediaPlayer: MediaPlayer::setAudioStreamType
11-22 03:12:24.051 29582-29582/etes.giuseppe.databringer V/MediaPlayer-JNI: set_session_id(): 8270
11-22 03:12:24.051 29582-29582/etes.giuseppe.databringer V/MediaPlayer: MediaPlayer::setAudioSessionId(8270)
11-22 03:12:24.051 29582-29582/etes.giuseppe.databringer W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: http://ntcdn.stream/audio/teste.ogg
11-22 03:12:24.051 29582-29582/etes.giuseppe.databringer D/RingtoneManager: Can't get current user. return default user
11-22 03:12:24.051 29582-29582/etes.giuseppe.databringer D/MediaPlayer: setDataSource IOException | SecurityException happend :
java.io.FileNotFoundException: No content provider: http://ntcdn.stream/audio/teste.ogg
你能帮我吗?我真的不知道该怎么办才能解决此问题。
答案 0 :(得分:0)
使用此:
private void startPlaying() {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(YOUR_URL);
mediaPlayer.prepare();
} catch (IOException ignored) {
}
mediaPlayer.start();
}