开发者

Android Audio Streaming

开发者 https://www.devze.com 2023-03-04 14:14 出处:网络
Does any one have code they know will run android audio stream from a URL, that will work in android OS 2.2. I have looked on the developer site and there code doesn\'t seem to work for me. Some of th

Does any one have code they know will run android audio stream from a URL, that will work in android OS 2.2. I have looked on the developer site and there code doesn't seem to work for me. Some of the other code or tutorials are for older versions of android.

This was the code from the code i ve been using(won't work):

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Radio extends Activity {

    @Override
    protect开发者_开发技巧ed void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio);
        final MediaPlayer mp = new MediaPlayer();
        try {
            mp.setDataSource(URL);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
                // This is code for a button that starts the stream when clicked
        Button bRadio = (Button) findViewById(R.id.button_stream);
        bRadio.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View V) {



                mp.start(); 
    }






        });





   }
}       


As you are setting datasource to URL, use mp.prepareAsync() instead of mp.prepare().


if you are ok with JNI, there is some sample code here:

the creating JNI URI player is liking this:

// configure audio source  
SLDataLocator_URI loc_uri = {SL_DATALOCATOR_URI, (SLchar *) uri_utf8};  
SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED};  
SLDataSource audioSrc = {&loc_uri, &format_mime};  

// configure audio sink  
SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};  
SLDataSink audioSnk = {&loc_outmix, NULL};

// create audio player
const SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_MUTESOLO, SL_IID_VOLUME};
const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
(*engineEngine)->CreateAudioPlayer(engineEngine, &uriPlayerObject, &audioSrc,&audioSnk, 3, ids, req);  
(*uriPlayerObject)->Realize(uriPlayerObject, SL_BOOLEAN_FALSE);  
(*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_PLAY, &uriPlayerPlay);  

... get other interfaces...

then you could play:

(*uriPlayerPlay)->SetPlayState(uriPlayerPlay,SL_PLAYSTATE_PLAYING);  
0

精彩评论

暂无评论...
验证码 换一张
取 消