从URL播放声音的问题

时间:2011-08-12 07:59:21

标签: android

我想从网址播放声音:: 但我无法做到这一点,我实现了这个代码

package com.TestAudio;


import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;

public class TestAudio extends Activity {
    /** Called when the activity is first created. */
    private MediaPlayer xmPlayer2, xmPlayer3, xmPlayer4, xmPlayer5, xmPlayer6,
    xmPlayer7,xmPlayer8,xmPlayer9,xmPlayer10,xmPlayer11;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);




        prepareforaudio(xmPlayer2,"bike_route.mp3");
        prepareforaudio(xmPlayer3,"closed.mp3");
        prepareforaudio(xmPlayer4,"exit.mp3");
        prepareforaudio(xmPlayer5,"no_parking.mp3");
        prepareforaudio(xmPlayer6,"one_way.mp3");
        prepareforaudio(xmPlayer7,"phone.mp3");
        prepareforaudio(xmPlayer8,"rail_road_crossing.mp3");
        prepareforaudio(xmPlayer9,"sale.mp3");
        prepareforaudio(xmPlayer10,"stop.mp3");
        prepareforaudio(xmPlayer11,"turn_off_tap.mp3");



    }
    public void prepareforaudio(MediaPlayer player,String music1)
    {
        try
        {
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDataSource("http://xyz/MRESC/images/test/"+music1);
        player.prepare(); 
        player.start();
        }catch (Exception e) {
            // TODO: handle exception
        }
    }
}

许可:

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

1 个答案:

答案 0 :(得分:1)

将您的媒体播放器对象初始化为MediaPlayer mplayer=new MediaPlayer();