好的,所以我第一次使用SHOUTCast流,并且我被告知Android已经改变了它与ICS后音频的工作方式
所以,我尝试了一些在2.3和2.2上运行良好的代码,但是当我尝试在4.2.2上运行它时它没有播放,
这是我的MainActivity.java
package com.mooo.ajbiz11.ponyfeather;
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 MainActivity 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.activity_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://radio.ponyvillelive.com:8026/stream");
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 + "%");
}
}
我的activity_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">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Pause"
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_row="3"
android:layout_column="9"
android:layout_marginBottom="124dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"></Button>
<Button
android:text="Stop"
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/AppTheme"
android:layout_gravity="center_horizontal|bottom"
android:layout_row="2"
android:layout_column="15"
android:layout_alignTop="@+id/pause"
android:layout_toRightOf="@+id/pause"></Button>
<Button
android:text="Play"
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_row="5"
android:layout_column="11"
android:layout_alignTop="@+id/pause"
android:layout_toLeftOf="@+id/pause"></Button>
</RelativeLayout>
</LinearLayout>
这在Android 2.3中完美运行,并且是为2.2制作的,但我似乎无法理解4.0+中的新音频内容所以当我运行已编译的APK(我在Dropbox中签名时,签名我一无所获......
我对Android编程非常陌生,任何帮助/见解都会令人惊叹,但要保持温和......我对Java的理解只是基本的...
另外,我想从三个按钮切换到MediaController,但找不到文档
答案 0 :(得分:0)
Pfft ...数字......在发布时运行了一个LogCat,注意到我错过了互联网许可......
代码完美无缺,只需要一个许可,它就像我的平板电脑上的黄油一样