Android 2.2 MediaPlayer 在一个 SHOUTcast URL 上工作正常,但在另一个 URL 上工作不正常
请参阅我用于流式shoutcast流的代码,它适用于一个URL,但不适用于另一个URL。
这个有效:
Uri myUri = Uri.parse("http://fr3.ah.fm:9000/");
但不适用于这个:
Uri myUri = Uri.parse("http://someOtherURL");
SimpleMusicStream.java
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class SimpleMusicStream extends Activity implements
MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener {
private String TAG = getClass().getSimpleName();
private MediaPlayer mp = null;
private Button play;
private Button pause;
private Button stop;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
stop();
}
});
}
private void play() {
Uri myUri = Uri.parse("http://fr3.ah.fm:9000/");
try {
if (mp == null) {
this.mp = new MediaPlayer();
} else {
mp.stop();
mp.reset();
}
mp.setDataSource(this, myUri); // Go to Initialized state
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnErrorListener(this);
mp.prepareAsync();
Log.d(TAG, "LoadClip Done");
} catch (Throwable t) {
Log.d(TAG, t.toString());
}
}
@Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Stream is prepared");
mp.start();
}
private void pause() {
mp.pause();
}
private void stop() {
mp.stop();
}
@Override
public void onDestroy() {
super.onDestroy();
stop();
}
public void onCompletion(MediaPlayer mp) {
stop();
}
public boolean onError(MediaPlayer mp, int what, int extra) {
StringBuilder sb = new StringBuilder();
sb.append("Media Player Error: ");
switch (what) {
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
sb.append("Not Valid for Progressive Playback");
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
sb.append("Server Died");
break;
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
sb.append("Unknown");
break;
default:
sb.append(" Non standard (");
sb.append(what);
sb.append(")");
}
sb.append(" (" + what + ") ");
sb.append(extra);
Log.e(TAG, sb.toString());
return true;
}
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Play"
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="Pause"
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="Stop"
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
Log-cat 显示错误:
NuHTTPDataSource(33): Server did not give us the content length!
Media Player Error: Unknown (1) -2147483648
Media Player Error: Unknown (1) -1002
有人可以帮我修复它吗?
编辑:
只是与大家分享,我们的当前代码适用于 Android 2.1 &次要版本,但不适用于 Android 2.2 或更高版本。
谢谢
Please see the code i am using for streaming shoutcast stream, it works with one URL but NOT with the other one.
This one works:
Uri myUri = Uri.parse("http://fr3.ah.fm:9000/");
But not with this one:
Uri myUri = Uri.parse("http://someOtherURL");
SimpleMusicStream.java
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class SimpleMusicStream extends Activity implements
MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener {
private String TAG = getClass().getSimpleName();
private MediaPlayer mp = null;
private Button play;
private Button pause;
private Button stop;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
stop();
}
});
}
private void play() {
Uri myUri = Uri.parse("http://fr3.ah.fm:9000/");
try {
if (mp == null) {
this.mp = new MediaPlayer();
} else {
mp.stop();
mp.reset();
}
mp.setDataSource(this, myUri); // Go to Initialized state
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnErrorListener(this);
mp.prepareAsync();
Log.d(TAG, "LoadClip Done");
} catch (Throwable t) {
Log.d(TAG, t.toString());
}
}
@Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Stream is prepared");
mp.start();
}
private void pause() {
mp.pause();
}
private void stop() {
mp.stop();
}
@Override
public void onDestroy() {
super.onDestroy();
stop();
}
public void onCompletion(MediaPlayer mp) {
stop();
}
public boolean onError(MediaPlayer mp, int what, int extra) {
StringBuilder sb = new StringBuilder();
sb.append("Media Player Error: ");
switch (what) {
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
sb.append("Not Valid for Progressive Playback");
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
sb.append("Server Died");
break;
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
sb.append("Unknown");
break;
default:
sb.append(" Non standard (");
sb.append(what);
sb.append(")");
}
sb.append(" (" + what + ") ");
sb.append(extra);
Log.e(TAG, sb.toString());
return true;
}
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Play"
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="Pause"
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="Stop"
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
The Log-cat shows errors:
NuHTTPDataSource(33): Server did not give us the content length!
Media Player Error: Unknown (1) -2147483648
Media Player Error: Unknown (1) -1002
Can someone help me to fix it?
EDIT:
Just to share with you people, that our current code works with Android 2.1 & minor versions, but not works with Android 2.2 or higher.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从 Android 2.2 开始,原生支持 Shoutcast mp3 流媒体。
在 2.2 以下,Android 操作系统无法在不使用流端代理或设备上的流代理类来捕获流并将其传递给音频播放器的情况下本地播放 Shoutcast 流,就像 NPR 确实如此。
不直接支持音频/aacp 流。为此,您可以使用 ffmpeg、opencore 或 faad2 库将其解码为 PCM 并使用 audiotrack 进行播放。参考
Shoutcast mp3 streaming from Android 2.2 onwards is supported natively .
Below 2.2 the Android OS cannot play shoutcast streams natively without using a proxy on the stream end or a stream proxy class on the device to capture the stream and pass it to the audioplayer just like NPR does.
audio/aacp streaming is not supported directly . For this you can use ffmpeg, opencore or faad2 library to decode it into PCM and play using audiotrack .Reference
问题是audio/aacp 中的编码
问题是Android Media Player 无法正确渲染aacp。您将不得不尝试其他编解码器或编写自己的解码器。
problem is encoding in audio/aacp
Problem is that aacp is not rendered by Android Media Player properly. You will have to try some other codec or write your own decoder.
Mediaplayer
仅支持这些格式,如下所示Android 开发者文档。有些流可能满足这一点,但有些则不能
Mediaplayer
supports only these formats as given in the android developer documents.Some streams may be satisfying this,but some will not
有一个适用于 Android 的免费软件高级音频 (AAC) 解码器。您可以使用它来获取帮助和指导,或者使用其中的 Multiplayer 类来播放 aac 音频。其中有一个示例应用程序和库可以帮助您理解。我已经使用这个库来播放 Shoutcast 在线 Streams。我使用库中提供的 Multiplayer 类播放 AAC 和 MP3
希望这有帮助
There is a Freeware Advanced Audio (AAC) Decoder for Android. You can use this for help and Guidance or use Multiplayer class in it to playback aac audio. There is a sample app and library in it to help you understand. I have used this library to play Shoutcast online Streams. I played both AAC and MP3 using Multiplayer class provided in the library
Hope this helps
最后,我得到了简单的shoutcast流媒体应用程序。
尝试这个。只需更改 StreamService.java 中的流媒体 url。
Finally, i got simple shoutcast streaming app.
Try this. Just change streaming url in StreamService.java.