簡體   English   中英

Android在線廣播流媒體應用

[英]Online radio streaming app for Android

我正在構建一個Android應用程序,它將從拉丁國家流式傳輸幾個廣播電台,我知道可以在android中播放10個電台,我從他們那里獲得了URL,並且實際上使用本教程使其工作(鏈接已刪除,因為它已經死了)但我遇到的問題是它會播放幾秒鍾然后停止它繼續加載但不重啟流式傳輸,我的問題是:

  • 如果有人使用過這個教程可以解釋我如何讓它不斷流動而不停止。
  • 是否有更簡單的方式來傳輸無線電音頻? 這個教程似乎有點陳舊,是否有更新的教程或更新的代碼示例來研究或使用?
  • 有人能以正確的方式送我嗎?

所以我找到了這個樣本,它對我有用,如果你遇到同樣的問題:

在myMain.java中

import android.app.Activity;
import android.os.Bundle;

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class myMain extends Activity implements OnClickListener {

    private ProgressBar playSeekBar;

    private Button buttonPlay;

    private Button buttonStopPlay;

    private MediaPlayer player;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initializeUIElements();

        initializeMediaPlayer();
    }

    private void initializeUIElements() {

        playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
        playSeekBar.setMax(100);
        playSeekBar.setVisibility(View.INVISIBLE);

        buttonPlay = (Button) findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(this);

        buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
        buttonStopPlay.setEnabled(false);
        buttonStopPlay.setOnClickListener(this);

    }

    public void onClick(View v) {
        if (v == buttonPlay) {
            startPlaying();
        } else if (v == buttonStopPlay) {
            stopPlaying();
        }
    }

    private void startPlaying() {
        buttonStopPlay.setEnabled(true);
        buttonPlay.setEnabled(false);

        playSeekBar.setVisibility(View.VISIBLE);

        player.prepareAsync();

        player.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                player.start();
            }
        });

    }

    private void stopPlaying() {
        if (player.isPlaying()) {
            player.stop();
            player.release();
            initializeMediaPlayer();
        }

        buttonPlay.setEnabled(true);
        buttonStopPlay.setEnabled(false);
        playSeekBar.setVisibility(View.INVISIBLE);
    }

    private void initializeMediaPlayer() {
        player = new MediaPlayer();
        try {
            player.setDataSource("http://usa8-vn.mixstream.net:8138");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setSecondaryProgress(percent);
                Log.i("Buffering", "" + percent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player.isPlaying()) {
            player.stop();
        }
    }
}

在XML(main.xml)代碼中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Source: (Radio La Chevere)"
        android:layout_marginTop="10dip" android:gravity="center" />
<ProgressBar android:id="@+id/progressBar1"
        android:indeterminateOnly="false" android:progressDrawable="@android:drawable/progress_horizontal"
        android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
        android:minHeight="20dip" android:maxHeight="20dip"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_marginLeft="20dip" android:layout_marginRight="20dip"
        android:layout_marginTop="10dip"></ProgressBar>
<LinearLayout android:id="@+id/linearLayout1"
        android:layout_height="wrap_content" android:layout_width="match_parent"
        android:layout_marginTop="20dip" android:gravity="center">
        <Button android:text="Play" android:id="@+id/buttonPlay"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="Stop" android:id="@+id/buttonStopPlay"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>

和android清單:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="package.your.RadioStream"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".myMain"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

好吧,如果你期望一個進度條,你會得到令人失望的,這是一個流,默認情況下沒有時間結束是無止境的。 關於URL,您可以使用shoutcast2 / icecast服務器中的掛載點和shoutcast1中的/。

在onResume或wrvr你想要的! 粘貼此代碼。

    String url = "http://server2.crearradio.com:8371"; // your URL here
    final MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mediaPlayer.prepare(); // might take long! (for buffering, etc)
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();
        }
    });

在manifest.xml中添加Internet權限。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM