在Android应用中使用MediaPlayer的正确方法 [英] Correct method to use MediaPlayer in android app
问题描述
对不起,我的英语,但是我来自巴西,我使用了谷歌翻译器.
sorry my english, but I'm from Brazil and I used the google translator.
好吧,我正在尝试在此应用程序中尝试使流式传输成为在线广播,在2.2版中可以正常工作,但4.0版不起作用.没有错误发生,根本行不通.下面是我的代码.
Well, I'm struggling in this application where I am trying to make a streaming an online radio, works fine in version 2.2, but version 4.0 does not work. No error occurs, simply does not work. Below is my code.
感谢您的帮助.
package com.radiomiriam;
import java.io.IOException;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class Main extends Activity {
public String url = "http://69.64.48.96:9880";
public MediaPlayer player = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play();
}
@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;
}
public void stop(){
Toast.makeText(getApplicationContext(), "Finalizando...", Toast.LENGTH_SHORT).show();
if(player.isPlaying()){
player.stop();
}
}
public void play(){
if(player.isPlaying()){
Toast.makeText(getApplicationContext(), "Já está em execução!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Preparando...", Toast.LENGTH_SHORT).show();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
try {
player.setDataSource(url);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.start();
}
}
public void onClick(View v) {
final int id = v.getId();
switch (id) {
case R.id.btnPlay:
play();
break;
case R.id.btnStop:
stop();
break;
}
}
}
您好,请原谅,但我仍未找到解决问题的方法,也不知道如何解决,我再次寻求您的帮助来解决此问题.
Hello, excuse my insistence but I still have not found the solution to my problem and do not know how to solve, I have once again asking for your help to solve this problem.
该应用程序在2.x和3.x版本中运行,但在4.x版本中未运行.以下是下载该项目的链接,您可以查看该链接并为我提供帮助.
The application runs in versions 2.x and 3.x but not in version 4.x. Below is a link to download the project, to which you can take a look and help me.
http://nsi.inf.br/RadioMiriam.rar (已删除) >
http://nsi.inf.br/RadioMiriam.rar (removed)
推荐答案
我下载了您的项目,但实际上返回了错误代码: E/MediaPlayer(1976):错误(-38,0)
i downloaded your project and it actually returns error Code: E/MediaPlayer( 1976): error (-38, 0)
这是由于在准备之前启动Mediaplayer引起的.
It is caused by starting mediaplayer before it is prepared.
我进行了很少的更改,并且开始工作.您应该:
I have made few changes and it started to work. You should :
-
在MediaPlayer上设置onPrepared侦听器(即在onCreate中)
Set onPrepared listener on MediaPlayer (i.e. in onCreate)
player.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
从play()方法中删除player.start()
Remove player.start() from your play() method
以这种方式设置数据源(在您的play()方法中)
Set datasource in this way (in your play() method)
player.setDataSource(this, Uri.parse("http://69.64.48.96:9880/"));
它适用于Android 4.0.3的HTC Sensation
It works on my HTC Sensation with Android 4.0.3
如果您需要任何帮助,请告诉我.
If you need any assistance please let me know.
我忘记了,我也删除了
player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
这是多余的
这篇关于在Android应用中使用MediaPlayer的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!