Android音频流播放器应用程序

时间:2013-03-12 10:33:01

标签: android audio-streaming

您好我正在Android中开发一个应用程序,它将从网址流式传输并相应地播放该歌曲,但显示错误

request time failed: java.net.SocketException: Address family not supported by protocol.

这是一个无线电流媒体,我想建立。我不知道我在哪里出错了android音频流的部分。请帮忙。

以下是我的班级文件:
    StreamMusic.java

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.ProgressDialog;
import android.app.Service;
import android.content.Context;
import android.content.Intent;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import android.util.Log;

public class StreamMusic extends Service 
{
    private static final String APP = "StreamMusic";

    private static MediaPlayer mediaPlayer = null;
    private static String      STREAM      = "";
    HttpURLConnection connection = null;
    private final Binder binder = new LocalBinder();
    private static String TAG = "TAG";
    private ProgressDialog pd;

    @Override public IBinder onBind(Intent intent) 
    {
        return(binder);
    }

    @Override public void onDestroy() 
    {
        // stop the music
        mediaPlayer.stop();
        mediaPlayer = null;

        super.onDestroy();
    }

    public class LocalBinder extends Binder
    {
        StreamMusic getService() 
        {
            return (StreamMusic.this);
        }
    }

    // called from the main activity
    synchronized public void playPause(Context _c , String _s) 
    {
        if (mediaPlayer == null) 
        {
            // show progress dialog
            pd = ProgressDialog.show( _c , "Raw Audio", "connecting to stream..." , true , true);
            Log.d("mediaplayer synchronized playPause(Context _c, String _s)--->" , "SAMPLE");

            // not playing -> load stream
            STREAM = _s;
            loadStream(STREAM);
        }
        else if (mediaPlayer.isPlaying())
        {
            // playing -> pause
            mediaPlayer.pause();
        } 
        else 
        {
            // paused -> resume
            mediaPlayer.start();
        }
    }

    // load the stream in another thread
    private void loadStream(final String stream) 
    {
        // run the stream in its own thread
        Runnable r = new Runnable() 
        {
            public void run()
            {
                try 
                {

                Log.d("mediaplayer loadStream(String STREAM)--->" , "SAMPLE");
                    mediaPlayer = new MediaPlayer();
                    //mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mediaPlayer.setDataSource(stream);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    Log.d("mediaplayer AFTER START loadStream(String STREAM)--->" , "SAMPLE");
                    // dismiss the dialog
                    handler.sendEmptyMessage(0);
                }
                catch (IllegalStateException e) 
                {
                    Log.e(APP, "error loading stream " + STREAM + ".", e);
                    return;
                } catch (IllegalArgumentException e) 
                {
                    e.printStackTrace();
                } catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }
        };

        new Thread(r).start();
    }

    // a handler to dismiss the dialog once mp starts
    private Handler handler = new Handler()
    {
        @Override public void handleMessage(Message msg)
        {
            pd.dismiss();
        }
    };
}

and second is RawAudio.java

import android.app.Activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;

import android.os.Bundle;
import android.os.IBinder;

import android.util.Log;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class RawAudio extends Activity implements OnClickListener 
{
    private static final String APP = "RawAudio";

    // our background player service
    private StreamMusic streamMusic = null;

    @Override public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        //streamMusic = new StreamMusic();

        // setup ui
        setContentView(R.layout.main);

        // setup play/pause button
        Button ppButton = (Button)findViewById(R.id.playpause);
        ppButton.setOnClickListener(this);

        // connect/create our service
        connectToService();
    }

    @Override public void onDestroy() 
    {
        // disconnect from our service
        unbindService(onService);

        while (streamMusic != null) 
        {
            try {
                Thread.sleep(250);
            }
            catch (InterruptedException e)
            {
                Log.e(APP, "sleep command interrupted", e);
            }
        }

        super.onDestroy();
    }

    public void onClick(View v) 
    {
        if (streamMusic != null) 
        {
            EditText t = (EditText)findViewById(R.id.entry);
            String   s = t.getText().toString().trim();

            // send play/pause command
            streamMusic.playPause(this, s);
        } 
        else 
        {
            Log.d(APP, "service not bound yet.");
        }
    }

    // establish the connection
    private void connectToService() 
    {
        try 
        {
            bindService(new Intent(this, StreamMusic.class), onService, BIND_AUTO_CREATE);
        }
        catch (Exception e)
        {
            Log.e(APP, "error binding to service.", e);
        }
    }

    // once connected, set up the interface object
    private ServiceConnection onService = new ServiceConnection() 
    {
        public void onServiceConnected(ComponentName className, IBinder iBinder)
        {
            streamMusic = ((StreamMusic.LocalBinder)iBinder).getService();
        }

        public void onServiceDisconnected(ComponentName className) 
        {
            streamMusic = null;
        }
    };
}

0 个答案:

没有答案
相关问题