2

我计划通过流式传输几个本地互联网收音机来开发一个 Android 应用程序。为此,我正在使用 Android 的MediaPlayer对象并使用setDataSource方法。但是,我失败了,因为它没有像我预期的那样流式传输。但是,如果我将其设置setDataSource为真正的 mp3 资源,它就可以工作。

仅供参考,我还在uses-permissionAndroidManifest.xml 文件上设置如下,

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

这是我在此应用程序MainActivity.java中使用的代码,

package com.example.saralfm;

import java.io.IOException;    
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.app.Activity;    
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    

        final MediaPlayer mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

        try {
            //Its working
            //mp.setDataSource("http://download.tamiltunes.com/songs/__P_T_By_Movies/Saguni%20%282012%29/Manasellam%20Mazhaiye%20-%20TamilWire.com.mp3"); 
            //Its not working
            mp.setDataSource("http://www.onlineradios.in/tamil-radio-from-india/#aahaa-91_9-fm-chennai/");

        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException 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();
        }

        mp.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer arg0) {
            // TODO Auto-generated method stub
            mp.start();
         }});

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

我对 Android 操作系统很陌生。请帮帮我。

4

0 回答 0