14

シャウトキャスト ストリームのストリーミングに使用しているコードを参照してください。一方の 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 以降では動作しません

ありがとう

4

5 に答える 5

7

Android2.2以降からのShoutcastmp3ストリーミングはネイティブでサポートされています。

2.2より前では、Android OSは、 NPRと同じように、ストリームをキャプチャしてオーディオプレーヤーに渡すために、ストリーム側のプロキシまたはデバイスのストリームプロキシクラスを使用せずに、shoutcastストリームをネイティブに再生できません。

audio/aacpストリーミングは直接サポートされていません。このために、ffmpeg、opencore、またはfaad2ライブラリを使用してPCMにデコードし、audiotrackを使用して再生できます。参照

于 2012-01-11T18:54:18.920 に答える
1

問題は audio/aacp でのエンコードです

問題は、aacp が Android Media Player によって適切にレンダリングされないことです。他のコーデックを試すか、独自のデコーダーを作成する必要があります。

于 2012-01-06T16:32:39.477 に答える
1

MediaplayerAndroid 開発者向けドキュメントに記載されているこれらの形式のみをサポートします。

一部のストリームはこれを満たしている可能性がありますが、そうでないストリームもあります

于 2012-01-10T16:48:27.393 に答える
0

最後に、シンプルなシャウトキャスト ストリーミング アプリを手に入れました。これを試してください。StreamService.javaのストリーミング URL を変更するだけです。

于 2016-01-22T10:36:51.427 に答える